Created
August 10, 2011 14:23
-
-
Save jaysonsantos/1136898 to your computer and use it in GitHub Desktop.
Google developer quiz solver
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
# Google tricked us a bit, some logic is different, just check it | |
import sys | |
WORDS_A = open(sys.argv[1]).read().strip('\n').split(' ') | |
WORDS_B = open(sys.argv[2]).read().strip('\n').split(' ') | |
FOO_LETTERS = tuple('csmgj') | |
BLACKLIST = 'f' | |
ALPHABET_ORDER = tuple('cfjvqmhldtkrzwnbgxsp') | |
MAPPED_ORDER = tuple('0123456789abcdefghij') | |
PREP_SIZE = 3 | |
VERB_MIN_SIZE = 7 | |
MIN_PRETTY_NUMBER = 595161 | |
PRETTY_DIVISIBLE = 5 | |
def is_prep(word): | |
return len(word) == PREP_SIZE and word[-1] not in FOO_LETTERS and BLACKLIST not in word | |
def is_verb(word): | |
return (len(word) >= VERB_MIN_SIZE) and (word[-1] not in FOO_LETTERS) | |
def is_first_person(word): | |
return is_verb(word) and word[0] in FOO_LETTERS | |
def normalize(word): | |
return ''.join([MAPPED_ORDER[ALPHABET_ORDER.index(letter)] for letter in word]) | |
def sortit(a, b): | |
return cmp(normalize(a), normalize(b)) | |
def number(word): | |
a = list(word) | |
a.reverse() | |
return int(normalize(a), 20) | |
def is_pretty(number): | |
return number >= MIN_PRETTY_NUMBER and number % PRETTY_DIVISIBLE == 0 | |
preps = [word for word in WORDS_B if is_prep(word)] | |
print len(preps) | |
verbs = [word for word in WORDS_B if is_verb(word)] | |
print len(verbs) | |
first_persons = [word for word in WORDS_B if is_first_person(word)] | |
print len(first_persons) | |
sorted_list = list(set(WORDS_B)) | |
sorted_list.sort(cmp=sortit) | |
print ' '.join(sorted_list) | |
numbers = [number(n) for n in WORDS_B] | |
print numbers | |
pretty_numbers = [number for number in numbers if is_pretty(number)] | |
print len(pretty_numbers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment