Last active
December 1, 2015 03:37
-
-
Save iokiwi/496ca2a05431679dd59b to your computer and use it in GitHub Desktop.
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
| ones = { | |
| 0 : "", 1 : "one", | |
| 2 : "two", 3 : "three", | |
| 4 : "four", 5 : "five", | |
| 6 : "six", 7 : "seven", | |
| 8 : "eight", 9 : "nine" | |
| } | |
| tens = { | |
| 0 : "", 1 : "", | |
| 2 : "twenty", 3 : "thirty", | |
| 4 : "fourty", 5 : "fifty", | |
| 6 : "sixty", 7 : "seventy", | |
| 8 : "eighty", 9 : "ninety" | |
| } | |
| teens = { | |
| 0 : "ten", 1 : "eleven", | |
| 2 : "twelve", 3 : "thirteen", | |
| 4 : "fourteen", 5 : "fifteen", | |
| 6 : "sixteen", 7 : "seventeen", | |
| 8 : "eighteen", 9 : "nineteen" | |
| } | |
| magnitudes = { | |
| 0 : "", 3 : "thousand", | |
| 6 : "million", 9 : "billion", | |
| 12 : "trillion", 15 : "quadrillion", | |
| 18 : "quintillion", 21 : "sextillion", | |
| 24 : "septillion", 27 : "octillion", | |
| 30 : "nonillion", 33 : "decillion", | |
| 36 : "undecillion", 39 : "duodecillion", | |
| 42 : "tredecillion", 45 : "quattuordecillion", | |
| 48 : "quindecillion", 51 : "sexdecillion", | |
| 54 : "septendecillion", 57 : "octodecillion", | |
| 60 : "novemdecillion", 63 : "vigintillion", | |
| 66 : "unvigintillion", 69 : "duovigintillion", | |
| 72 : "trevigintillion", 75 : "quattuorvigintillion", | |
| 78 : "quinvigintillion", 81 : "sexvigintillion", | |
| 84 : "septenvigintillion", 87 : "octovigintillion", | |
| 90 : "novemvigintillion", 93 : "trigintillion", | |
| 96 : "untrigintillion", 99 : "duotrigintillion" | |
| } | |
| def keep_least_significant_digits(number, n): | |
| """ Keeps the least significant digits in a number and | |
| discards the rest. Accepts an integer and the number of | |
| least significant digits to keep. | |
| >>> keep_least_significant_digits(123,0) | |
| 0 | |
| >>> keep_least_significant_digits(123,2) | |
| 23 | |
| >>> keep_least_significant_digits(123,4) | |
| 123 | |
| """ | |
| return number % 10**n | |
| def remove_least_significant_digits(number, n): | |
| """ Truncates the least significant digits in a number and | |
| keeps the rest. Accepts an integer and the number of | |
| least significant digits to truncate. | |
| >>> remove_least_significant_digits(123,0) | |
| 123 | |
| >>> remove_least_significant_digits(123,1) | |
| 12 | |
| >>> remove_least_significant_digits(123,2) | |
| 1 | |
| >>> remove_least_significant_digits(123,3) | |
| 0 | |
| """ | |
| return (number - number % 10**n) // 10**n | |
| def get_nth_place_digit(number, n): | |
| """ Gets the nth least significant digit in an integer. | |
| Accepts an integer and the placement of the least | |
| significant digit to return. | |
| >>> get_nth_place_digit(123,0) | |
| 3 | |
| >>> get_nth_place_digit(123,1) | |
| 2 | |
| >>> get_nth_place_digit(123,2) | |
| 1 | |
| >>> get_nth_place_digit(123,3) | |
| -1 | |
| """ | |
| if 10**n > number and n != 0: | |
| return -1 | |
| number = remove_least_significant_digits(number, n) | |
| number = keep_least_significant_digits(number, 1) | |
| return number | |
| def expand_int_2_digit(number): | |
| """ Gives the full text expansion of a double digit integer | |
| >>> expand_int_2_digit(0) | |
| '' | |
| >>> expand_int_2_digit(10) | |
| 'ten' | |
| >>> expand_int_2_digit(99) | |
| 'ninety nine' | |
| """ | |
| if number > 99: | |
| return expand_int_3_digit(number) | |
| output = "" | |
| ones_digit = get_nth_place_digit(number, 0) | |
| tens_digit = get_nth_place_digit(number, 1) | |
| if tens_digit <= 0: | |
| output = ones[ones_digit] | |
| elif tens_digit == 1: | |
| output = teens[ones_digit] | |
| else: | |
| output = tens[tens_digit] + " " + ones[ones_digit] | |
| return output | |
| def expand_int_3_digit (number): | |
| """ Gives the full text expansion of a triple digit integer | |
| >>> expand_int_3_digit(101) | |
| 'one hundred and one' | |
| >>> expand_int_3_digit(999) | |
| 'nine hundred and ninety nine' | |
| """ | |
| if number > 999: | |
| return expand_int(number) | |
| output = "" | |
| hundreds_digit = get_nth_place_digit(number, 2) | |
| tens_and_ones = keep_least_significant_digits(number,2) | |
| if hundreds_digit > 0: | |
| output += ones[hundreds_digit] + " hundred" | |
| if tens_and_ones > 0: | |
| output += " and " | |
| output += expand_int_2_digit(tens_and_ones) | |
| return output | |
| def expand_magnitude (magnitude): | |
| """ Blah | |
| >>> expand_magnitude(1) | |
| '' | |
| >>> expand_magnitude(2) | |
| 'hundred' | |
| >>> expand_magnitude(3) | |
| 'thousand' | |
| >>> expand_magnitude(5) | |
| 'hundred thousand' | |
| >>> expand_magnitude(99) | |
| 'duotrigintillion' | |
| """ | |
| output = "" | |
| if magnitude % 3 == 2: | |
| output += "hundred" | |
| sub_magnitude = expand_magnitude(magnitude-2) | |
| if sub_magnitude != "": | |
| output += " " + sub_magnitude | |
| if magnitude % 3 == 0: | |
| output = magnitudes[magnitude] | |
| return output | |
| def expand_int_block (block, magnitude): | |
| """ Takes a integer of 3 or less digits, and a order of magnitude and gives the | |
| full text expansion of the integer including the expanded order of magnitude | |
| >>> expand_int_block(123,3) | |
| 'one hundred and twenty three thousand' | |
| >>> expand_int_block(23,6) | |
| 'twenty three million' | |
| >>> expand_int_block(23,98) | |
| 'twenty three hundred untrigintillion' | |
| >>> expand_int_block(23,97) | |
| 'twenty three' | |
| """ | |
| if block > 999: | |
| expand_int(block) | |
| output = expand_int_3_digit(block) | |
| expanded_magnitude = expand_magnitude(magnitude) | |
| if output != "" and expanded_magnitude != "": | |
| output += " " + expand_magnitude(magnitude) | |
| return output | |
| def expand_int(number, k=0): | |
| """ Takes an integer [0, n(10^99)] and returns the full english expansion of the word | |
| >>> expand_int(1001) | |
| 'one thousand, and one.' | |
| >>> expand_int(1000) | |
| 'one thousand.' | |
| >>> expand_int(10000000000) | |
| 'ten billion.' | |
| """ | |
| output = "" | |
| if number > 999: | |
| output += expand_int( | |
| remove_least_significant_digits(number,3), k+1) | |
| expanded_int_block = expand_int_block( | |
| keep_least_significant_digits(number, 3), k*3) | |
| if expanded_int_block != "": | |
| output += ", " | |
| if ( | |
| k == 0 | |
| and number > 999 | |
| and keep_least_significant_digits(number, 2) > 0 | |
| and get_nth_place_digit(number, 2) == 0 | |
| ): | |
| output += "and " | |
| output += expanded_int_block | |
| if k == 0: | |
| if number == 0: | |
| output = "zero" | |
| output = output.strip(", ") + "." | |
| return output | |
| def main(): | |
| while True: | |
| command = input("Input a number or type 'q' to quit: ") | |
| if command.isnumeric(): | |
| print(expand_int(int(command))) | |
| else: | |
| command = command.lower() | |
| if command == "q" or command == "quit": | |
| break | |
| if __name__ == "__main__": | |
| DEBUG = True | |
| if DEBUG: | |
| import doctest | |
| doctest.testmod() | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment