Weekly Challenge: Programmatically convert any1string of digits, such as "1234567" to word form, "one million, two hundred thirty-four thousand, five hundred sixty-seven"
for l in set(map(len, tens)):
if s[:l] in tens:
n += tens[s[:l]]
s = s[l:]
n += parse_ones(s)
return n
def parse_hundreds(s):
if not "hundred" in s:
return parse_tens(s)
n = 0
hundreds, tens = s.split("hundred")
n += 100 * parse_ones(hundreds) or 100
n += 0 if tens == '' else parse_tens(tens)
return n
def parse_thousands(s):
if not "thous" in s:
return parse_hundreds(s)
n = 0
thousands, hundreds = s.split("thous")
n += 1000 * parse_hundreds(thousands) or 1000
n += 0 if hundreds == '' else parse_hundreds(hundreds)
return n
def parse_millions(s):
if not "million" in s:
return parse_thousands(s)
n = 0
millions, thousands = s.split("million")
n += 1000000 * parse_hundreds(millions) or 1000000
n += 0 if thousands == '' else parse_thousands(thousands)
return n
def parse_billions(s):
if not "billion" in s:
return parse_millions(s)
n = 0
billions, millions = s.split("billion")
n += 1000000000 * parse_hundreds(billions) or 1000000000
n += 0 if millions == '' else parse_millions(millions)
return n
s = re.sub("[^a-z]", "", s.lower())
s = re.sub("and", "", s)
m = 1
if s[:5] == "minus":
s = s[5:]
m = -1
elif s[:8] == "negative":
s = s[8:]
m = -1
try:
return parse_billions(s) * m
except:
return None
if __name__ == '__main__':
if len(sys.argv) > 1:
num = " ".join(sys.argv[1:])
n = parse_number(num)
print n if n is not None \
else "\033[1mNot a number: %s\033[0m" % num
elif not sys.stdin.isatty():
for num in sys.stdin.readlines():
n = parse_number(num)
print n if n is not None \
else "\033[1mNot a number: %s\033[0m" % num[:-1]