Created
February 17, 2015 11:14
-
-
Save awhitty/a2141dc94b8abe3c9f10 to your computer and use it in GitHub Desktop.
Four is magic!!!!!!!
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
import math | |
NUMBER_WORDS = { | |
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" | |
} | |
# from http://codereview.stackexchange.com/questions/39183/python-converter-number-to-english-project-euler-17 | |
def int_to_english(n): | |
english_parts = [] | |
ones = n % 10 | |
tens = n % 100 | |
hundreds = math.floor(n / 100) % 10 | |
thousands = math.floor(n / 1000) | |
if thousands: | |
english_parts.append(int_to_english(thousands)) | |
english_parts.append('thousand') | |
if not hundreds and tens: | |
english_parts.append('and') | |
if hundreds: | |
english_parts.append(NUMBER_WORDS[hundreds]) | |
english_parts.append('hundred') | |
if tens: | |
english_parts.append('and') | |
if tens: | |
if tens < 20 or ones == 0: | |
english_parts.append(NUMBER_WORDS[tens]) | |
else: | |
english_parts.append(NUMBER_WORDS[tens - ones]) | |
english_parts.append(NUMBER_WORDS[ones]) | |
return ' '.join(english_parts) | |
def reduce_int(n): | |
if (n > 999999): | |
print "n should be less than one million" | |
english = int_to_english(n) | |
if n == len(english): | |
print "done: %d" % len(english) | |
return len(english) | |
print "step: %d -> %d" % (n, len(english)) | |
return reduce_int(len(english)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment