Created
August 8, 2011 20:41
-
-
Save alganet/1132686 to your computer and use it in GitHub Desktop.
Full script for the Google Developer Day Quiz (with tests)
This file contains hidden or 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 | |
| const FOO_LETTERS = 'aeiou'; //Letras especiais do Googlon | |
| const PREP_INV = 'z'; //Letra que invalida preposições | |
| const PREP_SIZE = 3; //Tamanho das preposições | |
| const VERB_SIZE = 8; //Tamanho do verbo | |
| const LETTER_ORDERING = 'qnbczxjtklmvhrwfsdgp'; //Ordem das letras | |
| const GOOGLON_NUMBER_MOD = 4; //Divisor do número bonito | |
| const GOOGLON_NUMBER_MAX = 526593; //Menor número bonito possível | |
| $textA = ''; | |
| $textB = ''; | |
| $textAOrdered = ''; | |
| $textB = explode(' ', $textB); | |
| $textA = explode(' ', $textA); | |
| $textAOrdered = explode(' ', $textAOrdered); | |
| $textAPreps = 62; | |
| $textAVerbs = 18; | |
| $textAFirstPersonVerbs = 4; | |
| $googlonNumber = 'xgjkxcj'; | |
| $decimalNumber = '394410765'; | |
| $textANiceNumbers = 125; | |
| $textBPreps = count(array_filter($textB, 'googlon_is_prep')); | |
| $textBVerbs = count(array_filter($textB, 'googlon_is_verb')); | |
| $textBFirstPersonVerbs = count(array_filter($textB, 'googlon_is_first_person_verb')); | |
| $textBNiceNumbers = count(array_unique(array_filter($textB, 'googlon_is_nice_number'))); | |
| $textBVocab = array_unique($textB); | |
| googlon_sort($textBVocab); | |
| $textBVocab = implode(' ', $textBVocab); | |
| $test = array_unique($textA); | |
| googlon_sort($test); | |
| assert($textAOrdered === $test); | |
| assert($decimalNumber === googlon_number($googlonNumber)); | |
| assert($textAPreps === count(array_filter($textA, 'googlon_is_prep'))); | |
| assert($textAVerbs === count(array_filter($textA, 'googlon_is_verb'))); | |
| assert($textAFirstPersonVerbs === count(array_filter($textA, 'googlon_is_first_person_verb'))); | |
| assert($textANiceNumbers === count(array_unique(array_filter($textA, 'googlon_is_nice_number')))); | |
| print_r(compact('textBPreps', 'textBVerbs', 'textBFirstPersonVerbs', 'textBNiceNumbers', 'textBVocab')); | |
| function googlon_is_prep($word) { | |
| return PREP_SIZE === strlen($word) | |
| && false === stripos(FOO_LETTERS, substr($word, -1, 1)) | |
| && false === stripos($word, PREP_INV); | |
| } | |
| function googlon_is_verb($word) { | |
| return strlen($word) >= VERB_SIZE && false !== stripos(FOO_LETTERS, substr($word, -1, 1)); | |
| } | |
| function googlon_is_first_person_verb($word) { | |
| return googlon_is_verb($word) && false !== stripos(FOO_LETTERS, substr($word, 0, 1)); | |
| } | |
| function googlon_hack($word) { | |
| return strtr($word, LETTER_ORDERING, '0123456789abcdefghij'); | |
| } | |
| function googlon_sort(array &$words) { | |
| return usort($words, function($a, $b) { | |
| return strcasecmp(googlon_hack($a), googlon_hack($b)); | |
| }); | |
| } | |
| function googlon_number($number) { | |
| return base_convert(strrev(googlon_hack($number)), 20, 10); | |
| } | |
| function googlon_is_nice_number($number) { | |
| $number = googlon_number($number); | |
| return (int) bcmod($number, GOOGLON_NUMBER_MOD) === 0 && $number >= GOOGLON_NUMBER_MAX; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment