Skip to content

Instantly share code, notes, and snippets.

@enmyj
Last active December 18, 2017 05:24
Show Gist options
  • Save enmyj/731158a522dc239045b5e8b390f4d9eb to your computer and use it in GitHub Desktop.
Save enmyj/731158a522dc239045b5e8b390f4d9eb to your computer and use it in GitHub Desktop.
Attempted Solution to Five Thirty Eight Riddler Express from 20171215
#!/usr/bin/env python3
"""
This script attempts to solve the Riddler express problem from
20171215: https://fivethirtyeight.com/features/please-help-me-i-have-a-number-problem/
This script is super brute force and not really useful, although it does work
Potential improvement would be to target numbers that are likely to have
longer words like "seven", "three" or a teen number
"""
num2words = {
1: 'One',
2: 'Two',
3: 'Three',
4: 'Four',
5: 'Five',
6: 'Six',
7: 'Seven',
8: 'Eight',
9: 'Nine',
10: 'Ten',
11: 'Eleven',
12: 'Twelve',
13: 'Thirteen',
14: 'Fourteen',
15: 'Fifteen',
16: 'Sixteen',
17: 'Seventeen',
18: 'Eighteen',
19: 'Nineteen',
20: 'Twenty',
30: 'Thirty',
40: 'Forty',
50: 'Fifty',
60: 'Sixty',
70: 'Seventy',
80: 'Eighty',
90: 'Ninety',
0: 'Zero'}
zero_words = {
0:'',
1:" Thousand ",
2:" Million ",
3:" Billion ",
4:" Trillion ",
5:" Quadrillion ",
6:" Quintillion ",
7:" Sextillion ",
8:" Septillion ",
9:" Octillion ",
10:" Nonillion ",
11:" Decillion ",
12:" Undecillion ",
13:" Duodecillion ",
14:" Tredecillion ",
15:" Quattuordecillion "}
# convert number to "count speak" when number is less than 20
def less_than_twenty(n):
return num2words[n]
# convert number to "count speak" when number is between 20 and 100
def twenty_to_hundred(n):
tens = (n // 10) * 10
ones = n - tens
if ones > 0:
return num2words[tens] + ' ' + num2words[ones]
else:
return num2words[tens]
# convert number to "count speak" when number between 100 and 1000
def hundred_to_thousand(n):
return num2words[(n // 100)] + ' Hundred '
# convert any number between 0 and 1000
def under_thousand(n):
if n < 20:
return less_than_twenty(n)
elif n >= 20 and n < 100:
return twenty_to_hundred(n)
else:
tens_ones = int(str(n)[1:])
if tens_ones < 20:
return hundred_to_thousand(n) + less_than_twenty(tens_ones)
else:
return hundred_to_thousand(n) + twenty_to_hundred(tens_ones)
# convert a number of any length to words (as said by count from Sesame Street)
def number_to_words(n):
nstr = str(n)
# fancy list comprehension to write number n like [1,234,567]
nums_as_list = [int(nstr[::-1][i:i+3][::-1]) for i in range(0, len(nstr), 3)][::-1]
result = []
for i, s in enumerate(nums_as_list):
ri = len(nums_as_list) - i - 1
result.append(under_thousand(s) + zero_words[ri])
return ''.join(result)
# find the longest number The Count could "tweet" (including the exclamation point)
def longest_twitter_number(limit):
n = 111337337333737373737377 # random guess at starting number
len_n = 3
while len_n < limit:
n += 1
len_n = len(number_to_words(n))
return n - 1
if __name__ == '__main__':
limit = 280
print(longest_twitter_number(limit))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment