Created
August 11, 2015 16:39
-
-
Save konrness/4825a6f520d389183684 to your computer and use it in GitHub Desktop.
Nerdery PHP - Challenge Yourselph - Issue 015: Longest Word
This file contains 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
<?php | |
/** | |
* Challenge Yourselph - 015 | |
* | |
* Longest Word | |
* | |
* Usage: php kness.php [input] | |
* Example: php kness.php "I like cheese a lot" | |
* | |
* If [input] is omitted, tests are run. | |
* | |
* @author Konr Ness <konr.ness@gmail.com> | |
*/ | |
$input = isset($argv[1]) ? $argv[1] : false; | |
/** | |
* Returns the longest word in a string. If there are two or more words with the same length, | |
* the first occurrence of the longest word is returned. Punctuation is ignored. | |
* | |
* @param string $input | |
* @return string | |
*/ | |
function findLongestWord($input) | |
{ | |
$ignoredCharacters = ['~', '`', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', '[', '{', ']', '}', ';', ':', '"', '\'', '<', ',', '>', '.', '?', '/', '|', '\\']; | |
$input = str_replace($ignoredCharacters, ' ', $input); | |
$words = explode(' ', $input); | |
// create array with the position in the sentence and the word, so we can compare by both | |
$wordsAndPositions = []; | |
foreach ($words as $position => $word) { | |
$wordsAndPositions[] = [$position, $word]; | |
} | |
usort($wordsAndPositions, function ($a, $b) { | |
if (strlen($b[1]) == strlen($a[1])) { | |
// lengths are the same, compare by position in sentence | |
return $a[0] - $b[0]; | |
} | |
// lengths are different, compare by length of word | |
return strlen($b[1]) - strlen($a[1]); | |
}); | |
return $wordsAndPositions[0][1]; | |
} | |
if ($input) { | |
echo "The longest word is: " . findLongestWord($input) . PHP_EOL; | |
} else { | |
// test cases | |
$tests = [ | |
'cheese' => 'I like cheese a lot', | |
'What' => 'What fun!!%', | |
'know' => "I know if a word of the same size will work okay.", | |
'This' => "This list from word part that Konr made this week.", | |
'returns' => "Create a method that takes one input (a string) and returns the largest word in the string.", | |
'length' => 'If there are two or more words of the same length, return the first word from the string with that length.', | |
'letter' => 'A six letter word is "really" long.' | |
]; | |
$failed = []; | |
foreach ($tests as $expected => $input) { | |
switch (($actual = findLongestWord($input)) == $expected) { | |
case true: | |
echo "."; | |
break; | |
case false: | |
echo "F"; | |
$failed[] = sprintf( | |
"\nExpected: %s \nFound: %s", | |
print_r($expected, true), | |
print_r($actual, true) | |
); | |
break; | |
} | |
} | |
if (empty($failed)) { | |
echo "\033[1;30m\033[42mOK\033[0m" . PHP_EOL; | |
} else { | |
echo implode(PHP_EOL, $failed) . PHP_EOL; | |
echo PHP_EOL . "\033[1;37m\033[41mFail!\033[0m" . PHP_EOL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment