Last active
June 9, 2021 14:35
-
-
Save EZLiang/7b625316681bc036740c540389eec9ef to your computer and use it in GitHub Desktop.
A number synthesizer
This file contains 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
suffixes = ["", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "quatturodecillion", "quindecillion", "sexdecillion", "septendecillion", "octodecillion", "novemdecillion", "vingtillion"] | |
numbers = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] | |
teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"] | |
tens = ["zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] | |
def synth2(n): | |
if n == 0: | |
return "" | |
elif n < 10: | |
return numbers[n] | |
elif n < 20: | |
return teens[n - 10] | |
elif n % 10 == 0: | |
return tens[int(n / 10)] | |
else: | |
u = n % 10 | |
t = int((n - u) / 10) | |
return "{} {}".format(tens[t], numbers[u]) | |
def synth3(n): | |
if n == 0: | |
return "" | |
elif n < 100: | |
return synth2(n) | |
elif n % 100 == 0: | |
return "{} hundred".format(numbers[int(n / 100)]) | |
else: | |
return "{} hundred {}".format(numbers[int((n - (n % 100)) / 100)], synth2(n % 100)) | |
def expand(n): | |
a = [] | |
while n: | |
a.append(n % 1000) | |
n -= a[-1] | |
n = int(n / 1000) | |
return a | |
def synthesis(n): | |
l = expand(n) | |
r = [] | |
for i in range(len(l)): | |
if synth3(l[i]): | |
r.reverse() | |
if i: | |
r.append(suffixes[i]) | |
r.append(synth3(l[i])) | |
r.reverse() | |
return " ".join(r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test suite:
@EZLiang (Me!) fixed a typo in "forty".
๐
๐