Created
July 29, 2014 06:44
-
-
Save waneka/7aa11ea2e8074e89747f to your computer and use it in GitHub Desktop.
python code kata
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
BELOW_TWENTY = { | |
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" | |
} | |
TENS_DIGITS = { | |
2: "twenty", | |
3: "thirty", | |
4: "fourty", | |
5: "fifty", | |
6: "sixty", | |
7: "seventy", | |
8: "eighty", | |
9: "ninety" | |
} | |
def num_in_words(number): | |
words = [] | |
if number < 20: return BELOW_TWENTY[number] | |
if number < 100: return TENS_DIGITS[number / 10] + " " + num_in_words(number % 10).rstrip() | |
if number < 1000: return BELOW_TWENTY[number / 100] + " hundred and " + num_in_words(number % 100) | |
if number < 1000000: return num_in_words(number / 1000) + " thousand " + num_in_words(number % 1000) | |
if number < 1000000000: return num_in_words(number / 1000000) + " million " + num_in_words(number % 1000000) | |
if number < 1000000000000: return num_in_words(number / 1000000000) + " billion " + num_in_words(number % 1000000000) | |
assert(num_in_words(1) == "one") | |
assert(num_in_words(2) == "two") | |
assert(num_in_words(9) == "nine") | |
assert(num_in_words(10) == "ten") | |
assert(num_in_words(13) == "thirteen") | |
assert(num_in_words(21) == "twenty one") | |
assert(num_in_words(99) == "ninety nine") | |
assert(num_in_words(101) == "one hundred and one") | |
assert(num_in_words(567) == "five hundred and sixty seven") | |
assert(num_in_words(5672) == "five thousand six hundred and seventy two") | |
assert(num_in_words(3935612) == "three million nine hundred and thirty five thousand six hundred and twelve") | |
assert(num_in_words(3935411612) == "three billion nine hundred and thirty five million four hundred and eleven thousand six hundred and twelve") | |
print "Mathematical!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment