Weekly Challenge: Programmatically convert any1string of digits, such as "1234567" to word form, "one million, two hundred thirty-four thousand, five hundred sixty-seven"
Now adapt you're code to deal with any spoken language.
$ say -lang en_US 14319
Fourteen thousand three hundred nineteen
$ say -lang en_GB 14319
Fourteen thousand and three hundred and nineteen
$ say -lang es_ES 14319
Catorce mil trescientos diecinueve
$ say -lang ja_JP 14319
一万四千三百十九
$ say -lang en_US 14319
Fourteen thousand three hundred nineteen
$ say -lang en_GB 14319
Fourteen thousand and three hundred and nineteen
$ say -lang es_ES 14319
Catorce mil trescientos diecinueve
$ say -lang ja_JP 14319
一万四千三百十九
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]
[0]user: python fucking-numbers one thousand million Not a number: one thousand million
[0]user: python fucking-numbers one milliard Not a number: one milliard
So... which is it?
>>18
You're the type of person who thinks the equally nonsensical DD/MM/YYYY is any better than MM/DD/YYYY, right? Sensible date format (YYYY/MM/DD) user here.
>>28
Stop changing the argument as you please. You know full-well that DD/MM/YYYY does what it means in an intuitive manner, and that MM/DD/YYYY is just nonsensical. You're only trying to boost your own ego by saying ``Aha! This similar, but irrelevant situation is insane! Thus I win.''
>>29-30
Using a different order for dates and datetimes does not make sense. I'll admit that only reversing endianness once is slightly better than reversing endianness twice, but both are a lot less sensible than just using the same endianness all the way through.
>>31
You're not thinking outside your Pentium II box. In the real world, nobody says “In the year 2010, the month of July, the twenty-sixth day”.
It's “The twenty-sixth of July, 2010” (or “July [the] twenty-sixth, 2010” but we've already ruled this out as ludicrous).
>>32
In normal speech, people don't usually say the year, and they don't say "The twenty-sixth of July". They just say "July twenty-sixth". That's a lot closer to 2010/07/26 than it is to 26/07/2010.
>>33
But we're clearly including the year here, you can't exclude it for one case just because it suits your point. Also, stop pretending countries other than your glorious homeland don't speak normally.
Name:
Anonymous2010-07-26 19:51
Saying "twenty-sixth of July" (which I often do) or "July twenty-sixth" isn't really as useful as it sounds since months don't have the same calendar organization. We should start a new shorthand date system that includes and starts with the day of the week.
"Monday, July 26th 2010" becomes 1/07/26/2010, for example.
>>36
Including the day of the week doesn't add any information that isn't already there. It actually takes longer to read that extra number and figure out what order the numbers are in than it does to just calculate the day of the week.
>>39 Can't locate Lingua/EN/Numbers.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl .).
BEGIN failed--compilation aborted.
Your solution is as effective as >>2's.