-
-
Save marcoscoelho/1134206 to your computer and use it in GitHub Desktop.
Full script for the Google Developer Day Quiz (http://developerquiz.appspot.com) in 39 lines (max: 80 char/line)
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 isset($argv[1]) OR die('Input file should be the first arg on cmd line.'); | |
const G_FOO = 'bjdrf'; //Googlon special letters | |
const G_INV = 'c'; //Preposition invalidator | |
const G_PREP = 3; //Preposition size | |
const G_VERB = 6; //Verb size | |
const G_ORDER = 'gkvrbjwtnlphzfmdxcqs'; //Letter ordering | |
const G_NUM_MOD = 5; //Pretty number divisor | |
const G_NUM_MAX = 860533; //Pretty number minimum | |
$a = array('array_filter', 'array_unique'); | |
$text = explode(' ', file_get_contents($argv[1])); | |
echo 'Prepositions: ' . count($a[0]($text, 'is_prep')) . PHP_EOL . | |
'Verbs: ' . count($a[0]($text, 'is_verb')) . PHP_EOL . | |
'First Person: ' . count($a[0]($text, 'is_fp_verb')) . PHP_EOL . | |
'Pretty Numbers: ' . count($a[0]($a[1]($text), 'is_nice')) . PHP_EOL . | |
'Vocabulary: ' . implode(' ', gsort($a[1]($text))) . PHP_EOL; | |
function is_prep($str) { | |
return G_PREP == strlen($str) && false === stripos(G_FOO, substr($str, -1)) | |
&& false === stripos($str, G_INV); | |
} | |
function is_verb($str) { | |
return strlen($str) >= G_VERB && false === stripos(G_FOO, substr($str, -1)); | |
} | |
function is_fp_verb($str) { | |
return is_verb($str) && false === stripos(G_FOO, $str[0]); | |
} | |
function is_nice($str) { | |
$gnumber = base_convert(strrev(strmask($str)), 20, 10); | |
return (int) bcmod($gnumber, G_NUM_MOD) === 0 && $gnumber >= G_NUM_MAX; | |
} | |
function strmask($str) { | |
return strtr($str, G_ORDER, '0123456789abcdefghij'); | |
} | |
function gsort(array $arr) { | |
usort($arr,function($a,$b) { return strcasecmp(strmask($a),strmask($b)); }); | |
return $arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
39Lx80 👍