Skip to content

Instantly share code, notes, and snippets.

@mauriciofs
Created July 8, 2017 12:42
Show Gist options
  • Save mauriciofs/edfd087bf0e18937383a33ed7ede2386 to your computer and use it in GitHub Desktop.
Save mauriciofs/edfd087bf0e18937383a33ed7ede2386 to your computer and use it in GitHub Desktop.
Palindrome problem solution.
declare(strict_types=1);
/**
* Resolution to the problem:
* Five Dwarves ( Gimli Fili Ilif Ilmig and Mark) met at the Prancing Pony and played
* a word game to determine which combinations of their names resulted in a palindrome.
* Write a program in that prints out all of those combinations
* */
//Class to pipe functions for better readability
class Piper {
private $flow = [];
protected $param;
public function __construct($param = null){
$this->param = $param;
}
//Pipe functions to be called
public function pipe(callable $function) : Piper
{
$this->flow[] = $function;
return $this;
}
//Saves the first param to use in the first function chain
public function setParam($param){
$this->param = $param;
return $this;
}
//Call the chained functions and return the last result
public function execute()
{
foreach ($this->flow as $flow) {
$return = $flow($return ?? $this->param);
}
//Reset flow
$this->flow = [];
return $return;
}
}
/**
* Find palindrome pair function, receives a array with names and returns
* an associative array with the pairs
* @param array $names - Names to find the palindrome pair
**/
function findPalindrome(array $names = []) : array
{
//Array to save names and reversed names
$aNames = [];
//Auxialiary array to save the found pairs
$combinations = [];
$arrSize = count($names);
$pipe = new Piper();
//Loop through names to find the pairs
for ($i=0; $i < $arrSize; $i++){
$name = strtolower($names[$i]);
//$reversedName = strtolower(join("", array_reverse(str_split($name))));
//Created the Pipe class just for better readability
$reversedName = $pipe->setParam($name)->pipe('str_split')->pipe('array_reverse')->pipe('join')->pipe('strtolower')->execute();
//If the actual name and the reversed name are not in the aNames push it
//Using array and isset because its much faster than a linear search
if( (!isset($aNames[$name]) && !isset($aNames[$reversedName]))){
$aNames[$name] = $reversedName;
}else{
//Found the reversed name in the array which means the pair forms a palindrome
//push name and the reversed name to the result array
$combinations[$name] = $reversedName;
}
}
return $combinations;
}
$names = ['Gimli', 'Fili', 'Ilif', 'Ilmig', 'Mark'];
print_r(findPalindrome($names));
//Testing performance
/*
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
for($i=0; $i< 200000; $i++){
$names[] = generateRandomString(5);
}
$start = microtime(true);
echo $start . PHP_EOL;
print_r(findPalindrome($names));
echo (microtime(true) - $start) . PHP_EOL;*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment