Skip to content

Instantly share code, notes, and snippets.

@marcoscoelho
Forked from alganet/developerquiz.php
Created August 9, 2011 14:33
Show Gist options
  • Save marcoscoelho/1134206 to your computer and use it in GitHub Desktop.
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)
<?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;
}
@marcoscoelho
Copy link
Author

39Lx80 👍

@alganet
Copy link

alganet commented Aug 10, 2011

Boa!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment