Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active September 26, 2020 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbvf50mobile/a02de0aca4563ef11d05b49a16321c62 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/a02de0aca4563ef11d05b49a16321c62 to your computer and use it in GitHub Desktop.
Just PHP FUN 013.
<?php
#
function average_string(string $s): string {
$meaning = [
"zero" => 0,
"one" => 1,
"two" => 2,
"three" => 3,
"four" => 4,
"five" => 5,
"six" => 6,
"seven" => 7,
"eight" => 8,
"nine" => 9,
];
$mean2 = array_flip($meaning);
$n = "n/a";
echo "$s = $size \n";
$sum = 0;
if(0 == strlen($s)) return $n;
$arr = explode(" ",$s);
$size = count($arr);
foreach($arr as $x){
if(!isset($meaning[$x])) return $n;
echo "$x => {$meaning[$x]}\n";
$sum += $meaning[$x];
}
return $mean2[floor($sum/$size)];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment