Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

shit challenge: numerics to words

Name: Anonymous 2010-07-25 3:01

Weekly Challenge: Programmatically convert any1 string of digits, such as "1234567" to word form, "one million, two hundred thirty-four thousand, five hundred sixty-seven"

Here is mine: http://codepad.org/gyqbaKxx

Criticize my code.  I know it sucks anus.  I would like to make it better.  Submit your own versions as well.

Use whatever language you like[i]!/i]

_____________________________________
1. Up to 18 significant digits is fine.

Name: Anonymous 2010-07-25 3:40


import org.4chan.dis.Prog;

class MySolution {
    public static void main(String[] args) {
        Prog.newInstance().getSolution();
    }
}

Name: Anonymous 2010-07-25 3:59

I thought I had done this about a year ago, lo and behold.
http://dis.4chan.org/read/prog/1242279244/11

Name: Anonymous 2010-07-25 4:21

In Common Lisp:

CL-USER> (format t "~r" 1234567)
one million two hundred thirty-four thousand five hundred sixty-seven

Name: Anonymous 2010-07-25 5:59

we already did this, over two years ago:
http://hotaru.thinkindifferent.net/code/number.c

Name: Anonymous 2010-07-25 6:28

>>5
if(!num){
   puts("zero.");
   return 0;
  }
f(num < 0){
   num *= -1;
   printf("negative ");
  }

Stopped reading right there.

Name: Anonymous 2010-07-25 8:55

Use a real language (i.e. British English) where there are some ands in your huge fucking number string.

Name: Anonymous 2010-07-26 13:56

>>7
I don't have a British English compiler!

Name: Anonymous 2010-07-26 14:16

>>7
"and" goes between the integral and fractional parts of a number. There should never be an "and" anywhere else in a number string.

Name: Anonymous 2010-07-26 14:28

>integral
U MEAN INTEGER

Name: Anonymous 2010-07-26 14:34

>>10
Nah. >>9 is alright. Even Feynman would use "integral" in that sense.

Name: Anonymous 2010-07-26 14:35

>>10
http://en.wikipedia.org/wiki/Decimal#Decimal_fractions
The integer [sic] part or integral part of a decimal number is the part to the left of the decimal separator (see also floor function).

Name: Anonymous 2010-07-26 14:38

>>10
You're an idiot.

Name: Anonymous 2010-07-26 15:04

>>13
*your and idort

Name: Anonymous 2010-07-26 15:06

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
一万四千三百十九

Name: Anonymous 2010-07-26 15:08

Forgot my monospace.


$ 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
一万四千三百十九

Name: !XarnWayDP. 2010-07-26 16:12

Your challenge is boring, so I did it the other way around.

#!/usr/bin/python

import sys, re

def parse_number(s):
    def parse_ones(s):
        ones = { "":      0
               , "zero":  0
               , "a":     1
               , "one":   1
               , "two":   2
               , "three": 3
               , "four":  4
               , "five":  5
               , "six":   6
               , "seven": 7
               , "eight": 8
               , "nine":  9
               }

        return ones[s]

    def parse_tens(s):
        teens = { "ten":       10
                , "eleven":    11
                , "twelve":    12
                , "thirteen":  13
                , "fourteen":  14
                , "fifteen":   15
                , "sixteen":   16
                , "seventeen": 17
                , "eighteen":  18
                , "nineteen":  19
                }

        if s in teens:
            return teens[s]
   
        n = 0

        tens = { "twenty":  20
               , "thirty":  30
               , "forty":   40
               , "fifty":   50
               , "sixty":   60
               , "seventy": 70
               , "eighty":  80
               , "ninety":  90
               }

        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]

    else:
        print "NUMBERS, GODDAMMIT"

Name: Anonymous 2010-07-26 16:24

>>9
You're the kind of person who thinks MM/DD/YYYY is a good idea, right?

Name: Anonymous 2010-07-26 16:46

[b]x@www:/prog/num$[/b] python n.py
NUMBERS, GODDAMMIT
[b]x@www:/prog/num$[/b] python n.py 420
0
[b]x@www:/prog/num$[/b] python n.py -420
0
[b]x@www:/prog/num$[/b] python n.py -a 420
1
[b]x@www:/prog/num$[/b] python n.py -b 420
Not a number: -b 420
[b]x@www:/prog/num$[/b] python n.py -c 420
Not a number: -c 420

Name: Anonymous 2010-07-26 16:58

>>19
Maybe you should learn to read.

$ ./parse.py four hundred twenty
420

Name: Anonymous 2010-07-26 17:18

[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?

Name: Anonymous 2010-07-26 17:19

NUMBERS, GODDAMMIT

Name: Anonymous 2010-07-26 17:30


#!/usr/bin/python
number = raw_input("What number: ")
if (number == 1):
    Print "One."
else:
    Print "Can you please put a different number?"

Name: Anonymous 2010-07-26 17:40

>>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.

Name: Anonymous 2010-07-26 17:43

>>24
it is better

Name: Anonymous 2010-07-26 17:44

>>24
I'm sorry, but DD/MM/YYYY is surely better than MM/DD/YYYY -- the endianess is sane, at least.

Name: Anonymous 2010-07-26 17:55

>>24
Well, it translates directly to speech rather well. So, yes.

Name: Anonymous 2010-07-26 18:01

>>26
DD/MM/YYYY HH:MM:SS is not sane endianness.

Name: Anonymous 2010-07-26 18:03

>>28
Agreed, but we were talking date formats, not datetime formats

Name: Anonymous 2010-07-26 18:18

>>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.''

Name: Anonymous 2010-07-26 18:28

>>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.

Name: Anonymous 2010-07-26 18:32

>>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).

Name: Anonymous 2010-07-26 19:20

>>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.

Name: Anonymous 2010-07-26 19:39

>>30

My brother's argument for MM/DD/YYYY is that putting the month first gives you a better feel for the time of year it is.

He is an engineer.

Name: Anonymous 2010-07-26 19:40

>>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: Anonymous 2010-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.

Name: Anonymous 2010-07-26 19:55

>>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.

Name: Anonymous 2010-07-26 20:15

You wouldn't say my/hax/anus.

Name: Anonymous 2010-07-26 20:33

perl -MLingua::EN::Numbers -e 'print Lingua::EN::Numbers->num2en(shift(@ARGV)) . "\n"'

Have a nice day.

Name: Anonymous 2010-07-26 20:53

>>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.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List