# numberwords.py # -------------- # By MWC Contributors # Functions to print out a verbal representation of an integer. MAXIMUM = 1000000 DIGIT_NAMES = [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" ] TWEEN_AND_TEEN_NAMES = [ "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" ] TENS_NAMES = [ "", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" ] def int_under_1000000_to_str(number): number_by_places = {"100k": "", "10k": "", "1k": "", "100": "", "10": "", "1": ""} quotient100000, remainder10000 = divide_with_remainder(number, 100000) #123456 = 1, 23456 quotient10000, remainder1000 = divide_with_remainder(remainder10000, 10000) #23456 = 2, 3456 quotient1000, remainder100 = divide_with_remainder(remainder1000, 1000) #3456 = 3, 456 quotient100, remainder10 = divide_with_remainder(remainder100, 100) #456 = 4, 56 quotient10, remainder1 = divide_with_remainder(remainder10, 10) #56 = 5, 6 if number < 1000: int_under_1000_to_str(number) return int_under_1000_to_str(number) if number >= 1000: #100000s place if quotient100000 != 0: number_by_places["100k"] = DIGIT_NAMES[quotient100000] + " hundred" #10000s place if quotient10000 == 1: if quotient100000 != 0: number_by_places["10k"] = " and " + TWEEN_AND_TEEN_NAMES[quotient1000] + " thousand" if quotient100000 == 0: number_by_places["10k"] = TWEEN_AND_TEEN_NAMES[quotient1000] + " thousand" if quotient10000 != 1: if quotient10000 != 0: if quotient100000 != 0: number_by_places["10k"] = " and " + TENS_NAMES[quotient10000+1] if quotient100000 == 0: number_by_places["10k"] = TENS_NAMES[quotient10000+1] #1000s place if quotient1000 != 0: if quotient100 == 0 and quotient10 == 0: if quotient10000 == 0: number_by_places["1k"] = DIGIT_NAMES[quotient1000]+" thousand" if quotient10000 != 0: number_by_places["1k"] = "-" + DIGIT_NAMES[quotient1000]+" thousand" if quotient100 != 0: if quotient10000 == 0: number_by_places["1k"] = DIGIT_NAMES[quotient1000]+" thousand " if quotient10000 != 0: number_by_places["1k"] = "-" + DIGIT_NAMES[quotient1000]+" thousand " if quotient10 != 0: if quotient10000 == 0: number_by_places["1k"] = DIGIT_NAMES[quotient1000]+" thousand " if quotient10000 != 0: number_by_places["1k"] = "-" + DIGIT_NAMES[quotient1000]+" thousand " if remainder1 != 0: if quotient10000 == 0: number_by_places["1k"] = DIGIT_NAMES[quotient1000]+" thousand " if quotient10000 != 0: number_by_places["1k"] = "-" + DIGIT_NAMES[quotient1000]+" thousand " #hundreds place if quotient100 != 0: number_by_places["100"] = DIGIT_NAMES[quotient100] + " hundred" #tens place if quotient10 == 1: number_by_places["10"] = " and " + TWEEN_AND_TEEN_NAMES[remainder1] if quotient10 > 1: number_by_places["10"] = " and " + TENS_NAMES[quotient10] #ones place if remainder1 != 0: if quotient10 > 1: number_by_places["1"] = "-" + DIGIT_NAMES[remainder1] if quotient10 == 1: number_by_places["1"] = "" if quotient10 == 0: number_by_places["1"] = DIGIT_NAMES[remainder1] return number_by_places["100k"]+number_by_places["10k"]+number_by_places["1k"]+number_by_places["100"]+number_by_places["10"]+number_by_places["1"] def int_under_1000_to_str(number): number_by_places = {"100k": "", "10k": "", "1k": "", "100": "", "10": "", "1": ""} quotient100, remainder10 = divide_with_remainder(number, 100) #456 = 4, 56 quotient10, remainder1 = divide_with_remainder(remainder10, 10) #56 = 5, 6 if number >= 100: if quotient100 != 0: number_by_places["100"] = DIGIT_NAMES[quotient100] + " hundred" if quotient10 == 1: number_by_places["10"] = " and " + TWEEN_AND_TEEN_NAMES[remainder1] if quotient10 > 1: number_by_places["10"] = " and " + TENS_NAMES[quotient10] if remainder1 != 0 and quotient10 != 1: number_by_places["1"] = "-" + DIGIT_NAMES[remainder1] return number_by_places["100k"]+number_by_places["10k"]+number_by_places["1k"]+number_by_places["100"]+number_by_places["10"]+number_by_places["1"] if number < 100: int_under_100_to_str(number) return int_under_100_to_str(number) def int_under_100_to_str(number): number_by_places = {"100k": "", "10k": "", "1k": "", "100": "", "10": "", "1": ""} quotient10, remainder1 = divide_with_remainder(number, 10) #56 = 5, 6 if number >= 20: if quotient10 > 0 and quotient10 != 1: number_by_places["10"] = TENS_NAMES[quotient10] if remainder1 != 0 and quotient10 != 1: number_by_places["1"] = "-" + DIGIT_NAMES[remainder1] return number_by_places["100k"]+number_by_places["10k"]+number_by_places["1k"]+number_by_places["100"]+number_by_places["10"]+number_by_places["1"] if number < 20: int_under_20_to_str(number) return int_under_20_to_str(number) def int_under_20_to_str(number): number_by_places = {"100k": "", "10k": "", "1k": "", "100": "", "10": "", "1": ""} quotient10, remainder1 = divide_with_remainder(number, 10) if number >= 10: number_by_places["10"] = TWEEN_AND_TEEN_NAMES[remainder1] return number_by_places["100k"]+number_by_places["10k"]+number_by_places["1k"]+number_by_places["100"]+number_by_places["10"]+number_by_places["1"] if number < 10: number_by_places["1"] = DIGIT_NAMES[number] return number_by_places["100k"]+number_by_places["10k"]+number_by_places["1k"]+number_by_places["100"]+number_by_places["10"]+number_by_places["1"] def int_under_10_to_str(number): number_by_places = {"100k": "", "10k": "", "1k": "", "100": "", "10": "", "1": ""} number_by_places["1"] = DIGIT_NAMES[number] return number_by_places["100k"]+number_by_places["10k"]+number_by_places["1k"]+number_by_places["100"]+number_by_places["10"]+number_by_places["1"] def divide_with_remainder(dividend, divisor): """Divides one number by another, using whole-number division. Returns the quotient and the remainder. """ quotient = dividend // divisor remainder = dividend % divisor return quotient, remainder int_under_1000000_to_str(10000)