Skip to content

Instantly share code, notes, and snippets.

@Twoody
Last active September 5, 2019 18:00
Show Gist options
  • Save Twoody/cfad800db991dfe862a8d572efee2066 to your computer and use it in GitHub Desktop.
Save Twoody/cfad800db991dfe862a8d572efee2066 to your computer and use it in GitHub Desktop.
Code Wars Count the Smiley Faces Challenge done in PHP
<?php
function count_smileys($arr): int {
$r = 0;
$l = count($arr);
$validEyes = [":", ";"];
$validNoses = ["-", "~"];
$validMouths = [")", "D"];
for($i=0; $i<$l; $i++){
$t = str_split( $arr[$i] );
$onMouth = FALSE;
$didNose = FALSE;
$onEyes = TRUE;
for ($j=0; $j<count($t); $j++ ){
$c = $t[$j];
if( $onEyes ){
if( in_array($c, $validEyes) === TRUE ){
$onEyes = FALSE;
$didNose = FALSE;
$onMouth = TRUE;
}
else{
break;
}
}
elseif( $onMouth ){
if( $didNose === FALSE && in_array($c, $validNoses) ){
$didNose = TRUE;
continue;
}
else{
if( in_array($c, $validMouths) )
$r += 1;
$onMouth = FALSE;
$onEyes = TRUE;
$didNose = FALSE;
}
break;
}
else
echo "ERROR: broke out\n";
}//end j-for
}//end i-for
return $r;
}
$out1 = count_smileys( [] );
$out2 = count_smileys( [':)'] );
$out3 = count_smileys( [': )'] );
echo $out1 . " == 0\n";
echo $out2 . " == 1\n";
echo $out3 . " == 0\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment