Skip to content

Instantly share code, notes, and snippets.

@carlosbelisario
Last active April 13, 2018 14:58
Show Gist options
  • Save carlosbelisario/3cf311651b76e2906f97e572c1fe73e6 to your computer and use it in GitHub Desktop.
Save carlosbelisario/3cf311651b76e2906f97e572c1fe73e6 to your computer and use it in GitHub Desktop.
Casi Palindromes
<?php
define('NECESARY_CHANGES', 3);
/**
* @param string $string
* @return bool
*/
function palindrome($string) {
$revert = strrev($string);
$length = strlen($string);
return $length > 1 && (levenshtein($string, $revert) < NECESARY_CHANGES);
}
/**
* @test
*/
function testPalindrome(array $words)
{
foreach($words as $word) {
if (palindrome($word)) {
echo "it is almost palindrome \n";
} else {
echo "failure: the word: " . $word . " is not palindrome \n";
}
}
}
$words = ['reconocer', 'oso', 'arepera', 'osao', 'hola'];
testPalindrome($words);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment