Skip to content

Instantly share code, notes, and snippets.

@yvan-sraka
Created October 21, 2016 10:01
Show Gist options
  • Save yvan-sraka/af06ca94f8a967a2c5c30183dcb55506 to your computer and use it in GitHub Desktop.
Save yvan-sraka/af06ca94f8a967a2c5c30183dcb55506 to your computer and use it in GitHub Desktop.
<?hh
// Exercice 1
$string = file_get_contents("dictionnaire.txt", FILE_USE_INCLUDE_PATH);
$dico = explode("\n", $string);
// How many words this dictionary contains?
echo count($dico) . "<br/>"; // answer: 336532
// How many words have exactly 15 characters?
$words_15 = array();
foreach ($dico as $word) {
if (strlen($word) == 15) {
array_push($words_15, $word);
}
}
echo count($words_15) . "<br/>"; // answer: 12298
echo $a;
// How many words containing the letter ‘w’?
$words_w = array();
foreach ($dico as $word) {
if (strpos($word, "w") !== false) {
array_push($words_w, $word);
}
}
echo count($words_w) . "<br/>"; // answer: 537
// How many words end with the letter ‘q’?
$words_q = array();
foreach ($dico as $word) {
if (substr($word, -1) == "q") {
array_push($words_q, $word);
}
}
echo count($words_q) . "<br/>"; // answer: 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment