Last active
August 29, 2015 14:24
-
-
Save gastonsoto/bfc69f6e14f350e35b75 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class Hackeala { | |
/* Every problem has an input value that determines the operation. Instantiate the class with the given input. */ | |
function __construct($input = ''){ | |
$this->input = $input; | |
} | |
/* Hexadecimal numbers are index starting from 1. Substract on odd keys and add on even keys. Find the ASCII equivalent to every single resuting hex number for the final asnwer. */ | |
public function resolveHex(){ | |
$hexa = $this->input; | |
$hexa = explode(' ',$hexa); | |
$hexa = array_combine(range(1, count($hexa)), array_values($hexa)); | |
foreach($hexa as $key=>$val){ | |
if($key % 2 == 0){ | |
$hexaux = hexdec($val) + 1; | |
$hexa[$key] = self::hex2str(dechex($hexaux)); | |
}else{ | |
$hexaux = hexdec($val) - 1; | |
$hexa[$key] = self::hex2str(dechex($hexaux)); | |
} | |
} | |
return implode('',$hexa); | |
} | |
public static function hex2str($hex){ | |
$str = ''; | |
for($i=0;$i<strlen($hex);$i+=2) $str .= chr(hexdec(substr($hex,$i,2))); | |
return $str; | |
} | |
/* Rank word using the following logic: | |
* Letters have points based on their position within the word multiplied by their position in the alphabet. | |
* Example input: 'acb' | |
* Example summatory output: (1*1) + (2*3) + (3*2) = 13 | |
* Give the final summatory result for the given input | |
*/ | |
function rankPhrase(){ | |
$string = $this->input; | |
if($string === ""){ | |
echo "Please enter a phrase"; | |
} | |
$total = 0; | |
$phrase = trim($string); | |
/* International Alphabet */ | |
$alphabet = range('a', 'z'); | |
$letters = implode('',$alphabet); | |
$strlen = strlen($phrase); | |
$tmparr = str_split($phrase); | |
for($i=0;$i < $strlen;$i++){ | |
$total += ((strpos($phrase,$tmparr[$i])+1) * (strpos($letters,$tmparr[$i])+1)); | |
} | |
return $total; | |
} | |
} | |
/* Hexadecimal Problem Example */ | |
$hexa = new Hackeala("0x4a 0x59 0x59 0x76 0x49 0x44 0x4d 0x77 0x57 0x4a 0x74 0x53 0x6b 0x63 0x6a 0x68 0x71 0x46 0x76 0x53 0x42 0x79 0x72 0x79 0x6e 0x4b 0x54 0x4a 0x54 0x66 0x56 0x72 0x62 0x4d 0x77 0x59 0x4f 0x44 0x52 0x63 0x4e 0x44 0x48 0x43 0x79 0x74 0x4a 0x64 0x59 0x49 0x79 0x54 0x52 0x64 0x71 0x55 0x71 0x4b 0x79 0x55 0x79 0x6c 0x63 0x59 0x59 0x55 0x66 0x42 0x72 0x6d 0x48 0x70 0x46 0x6e 0x4e 0x65 0x70 0x77 0x76 0x56 0x4b 0x79 0x6f 0x48 0x54 0x4f 0x77 0x78 0x57 0x56 0x50 0x4a 0x6d 0x6e 0x67 0x65"); | |
echo $hexa->resolveHex(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment