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: !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"

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