Created
August 8, 2011 20:28
-
-
Save alganet/1132654 to your computer and use it in GitHub Desktop.
Full script for the Google Developer Day Quiz (http://developerquiz.appspot.com) in 39 lines
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 //Run as command line. Input file as first argument. | |
const G_FOO = 'aeiou'; //Googlon special letters | |
const G_INV = 'z'; //Preposition invalidator | |
const G_PREP = 3; //Preposition size | |
const G_VERB = 8; //Verb size | |
const G_ORDER = 'qnbczxjtklmvhrwfsdgp'; //Letter ordering | |
const G_NUM_MOD = 4; //Pretty number divisor | |
const G_NUM_MAX = 526593; //Pretty number minimum | |
$textB = explode(' ', file_get_contents($argv[1])); | |
print 'Prepositions: ' . count(array_filter($textB, 'is_prep')) . PHP_EOL; | |
print 'Verbs: ' . count(array_filter($textB, 'is_verb')) . PHP_EOL; | |
print 'First Person: ' . count(array_filter($textB, 'is_first_person_verb')) . PHP_EOL; | |
print 'Pretty Numbers: ' . count(array_filter(array_unique($textB), 'is_nice')) . PHP_EOL; | |
print 'Vocabulary: ' . implode(' ', gsort(array_unique($textB))) . PHP_EOL; | |
function is_prep($word) { | |
return G_PREP == strlen($word) && false === stripos(G_FOO, substr($word, -1)) && false === stripos($word, G_INV); | |
} | |
function is_verb($word) { | |
return strlen($word) >= G_VERB && false === stripos(G_FOO, substr($word, -1)); | |
} | |
function is_first_person_verb($word) { | |
return is_verb($word) && false === stripos(G_FOO, $word[0]); | |
} | |
function is_nice($number) { | |
$gnumber = as_decimal($number); | |
return (int) bcmod($gnumber, G_NUM_MOD) === 0 && $gnumber >= G_NUM_MAX; | |
} | |
function normalize($word) { | |
return strtr($word, G_ORDER, '0123456789abcdefghij'); | |
} | |
function gsort(array $words) { | |
usort($words, function($a, $b) { return strcasecmp(normalize($a), normalize($b)); }); | |
return $words; | |
} | |
function as_decimal($number) { | |
return base_convert(strrev(normalize($number)), 20, 10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gaigalas, ta aí um código em python com 10 linhas a menos: https://gist.github.com/2424838
Pouco legível mas funciona ;)