Skip to content

Instantly share code, notes, and snippets.

@EZLiang
Last active June 9, 2021 14:35
Show Gist options
  • Save EZLiang/7b625316681bc036740c540389eec9ef to your computer and use it in GitHub Desktop.
Save EZLiang/7b625316681bc036740c540389eec9ef to your computer and use it in GitHub Desktop.
A number synthesizer
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)
@EZLiang
Copy link
Author

EZLiang commented Jan 12, 2020

Test suite:

from synthesis import synthesis

items = [86, 1, 10, 100, 1000666]
for i in range(5):
  fss = "{}: {} = {}"
  print(fss.format(i + 1, items[i], synthesis(items[i])))

@EZLiang (Me!) fixed a typo in "forty".

๐Ÿ‘€
๐Ÿ‘…

@Zachary-Summers
Copy link

This is cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment