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-26 22:33

>>40
Nah, Lingua::EN::Numbers actually exists and does the needful. I'd direct you to Acme::CPAN::InstallAndUse but that a) would provoke the same complaint and b) might not actually exist.

Name: >>17 2010-07-27 0:21

>>21
You know, I considered adding a British mode, but completely forgot about it before posting.

Name: Anonymous 2010-07-27 0:25

>>42
Why would you do that? British people aren't smart enough to use computers. Anyone who claims to be British on the internet is just trolling.

Name: Anonymous 2010-07-27 0:46

>>43
Anyone who claims to be British on the internet is just trolling.
trolling
0/10

Name: Anonymous 2010-07-27 1:31

middle endian is superior

Name: Anonymous 2010-07-27 2:41

Middle Earth is superior

Name: Anonymous 2010-07-27 4:23

middle finger is superior

Name: Anonymous 2010-07-27 7:11

>>42
Don't worry, it even converts strings like ``one hundred and two'', I was impressed

Name: Anonymous 2010-07-27 13:16

>>48
On the other hand, it also accepts ``zero thous a''. It's a beautiful mess.

Name: Anonymous 2010-07-27 13:41

>>49
And any otherwise-valid number string with absolutely no spaces in it.

Name: toekutr 2010-07-27 17:34

(require simply.scm)

(define (number-name num)
(name-triples (sub-in-names (make-triples num) namelist)))

(define (name-triples triples)
    (cond
      ((empty? triples) '())
      ((number? (first triples)) (se (name-triple (first triples)) (name-triples (bf triples))))
      (else (se (first triples) (name-triples (bf triples))))))

(define (make-triples num)
    (cond
      ((empty? num) '())
      ((= (length num) 2) num)
      ((= (length num) 1) num)
      (else
       (se (make-triples (bl (bl (bl num)))) (last-three num)))))

(define (length num)
    (if (empty? num)
        0
        (+ 1 (length (bf num)))))

(define (last-three wd)
    (word (last (bl (bl wd))) (last (bl wd)) (last wd)))

(define (sub-in-names triples names)
    (cond
      ((<= (length triples) 1) (se triples (first names)))
      ((> (length triples) 12) '(Overflow, try entering a smaller number))
      (else (if (all-zeros? (last triples))
            (se (sub-in-names (bl triples) (bf names)) (last triples))
            (se (sub-in-names (bl triples) (bf names)) (first names) (last triples))))))

(define (name-triple triple)
    (let ((fst (first triple)) (rest (bf triple)))
    (cond
      ((= (length triple) 3)
       (if (= fst 0)
           (se (name-triple rest))
           (se (name-single (first triple)) 'hundred (name-triple (bf triple)))))
      ((= (length triple) 2)
       (if (= fst 0)
           (se (name-single rest))
           (cond
             ((= fst 1) (cond
                          ((= triple 10) 'ten)
                          ((= triple 11) 'eleven)
                          ((= triple 12) 'twelve)
                          ((= triple 13) 'thirteen)
                          ((= triple 14) 'fourteen)
                          ((= triple 15) 'fifteen)
                          ((= triple 16) 'sixteen)
                          ((= triple 17) 'seventeen)
                          ((= triple 18) 'eighteen)
                          (else 'nineteen)))
             ((= fst 2) (se 'twenty (name-triple rest)))
             ((= fst 3) (se 'thirty (name-triple rest)))
             ((= fst 4) (se 'forty (name-triple rest)))
             ((= fst 5) (se 'fifty (name-triple rest)))
             ((= fst 6) (se 'sixty (name-triple rest)))
             ((= fst 7) (se 'seventy (name-triple rest)))
             ((= fst 8) (se 'eighty (name-triple rest)))
             ((= fst 9) (se 'ninety (name-triple rest)))
             (else (se (name-triple rest))))))
      (else
       (name-single triple)))))

(define (name-single num)
    (if empty? num) '(Whoops)
    (cond
      ((= num 0) '())
      ((= num 1) 'one)
      ((= num 2) 'two)
      ((= num 3) 'three)
      ((= num 4) 'four)
      ((= num 5) 'five)
      ((= num 6) 'six)
      ((= num 7) 'seven)
      ((= num 8) 'eight)
      (else 'eight)))

(define (name-triples triples)
    (cond
      ((empty? triples) '())
      ((number? (first triples)) (se (name-triple (first triples)) (name-triples (bf triples))))
      (else (se (first triples) (name-triples (bf triples))))))

(define namelist '(thousand million billion trillion quadrillion quintillion
  sextillion septillion octillion nonillion decillion))



This was made back a year or so ago, and I still suck at Scheme, but here ya go anyway.

 It goes up to about 10^33, and I'm using a few functions that aren't built into scheme (this was a textbook assignment, here's the list if you want to try this; http://www.cs.berkeley.edu/~bh/ssch27/appendix-simply.html). 



There's also a bug that I never bothered fixing


>(number-name 12000123)
'(twelve million thousand one hundred twenty three)

>(number-name 12010123)
'(twelve million ten thousand one hundred twenty three)

>(number-name 1231231352344135)
'(one quadrillion two hundred thirty one trillion two hundred thirty one billion three hundred fifty two
million three hundred forty four thousand one hundred thirty five)

As you can see, it has problems when all three digits of a
"triple" are zeros.

Name: toekutr 2010-07-27 17:36

ffffuuuck forgot code tags sorry.

http://paste.lisp.org/display/112882

Name: Anonymous 2010-07-27 17:45

>>52
Are you high?

Name: Anonymous 2010-07-27 18:14

>>53

not really no

Name: Anonymous 2010-07-27 18:15

>>54
By process of elimination, I have deduced that you are merely a fool.

Name: toekutr 2010-07-27 18:20

>>55

I clicked on the wrong tab when I pasted the code in the first time, and when I recopied it to paste into the correct tab, I forgot about the code tags. Nothing foolish, just a simple mistake.  Unless you're talking about the code, then sure, I know it sucks.

Name: Anonymous 2010-07-27 18:22

>>56
Oh, very well, it's just the 'fffuuuck' in the second mistake aroused my suspicions somewhat.

Name: Anonymous 2010-07-27 18:24

>>57

What? I can't type verbal obscenities into a text box now?

Name: Anonymous 2010-07-27 18:31

>>58
It was slow and clumsy, as if it were the end of the world for your marijuana-affected mind.

Name: Anonymous 2010-07-27 23:18

I decided to give this a go. I steered clear of looking at any of the other posts and came up with what follows. This could very easily be rewritten in C.


def num_to_name( num_str ):
    global exact, tens, powers
    # Convert to int and back, which fixes whitespace and leading zeroes.
    try:
        num_str = str(int(num_str))
    except ValueError:
        return "That was not a number!"
   
    if num_str == "0":
        return "zero"
   
    strlen = len(num_str)
   
    if strlen > 18 + 1 :
        return "That number is too large for my puny brain!"
   
    ans = [] # List to hold number words.
   
    i = -1
    while i < (strlen - 1):
        i += 1
       
        if num_str[i] != "0":
            m = (strlen - i - 1) % 3
       
            if m == 2:
                # Hundreds
                ans.append( exact[ num_str[i] ] )
                ans.append( "hundred " )
                continue
           
            elif m == 1:
                # Tens
                if num_str[i] == "1":
                    ans.append( exact[ num_str[i:i+2] ] )
                    i += 1
                else:
                    ans.append ( tens[ num_str[i] ] )
                    continue
            else:
                # Ones
                ans.append( exact[ num_str[i] ] )
       
        # Powers
        o = strlen - i - 1
               
        if o and not (o % 3) :
            ans.append( powers[o] )
                   
   
    return "".join(ans).rstrip(" ,")

Name: Anonymous 2010-07-27 23:20

>>60

Whoops, forgot my globals.



exact = { "1" : "one ", "2" : "two ", "3" : "three ", "4" : "four ", "5" : "five ", "6" : "six ", "7" : "seven ", "8" : "eight ", "9" : "nine ", "10" : "ten ", "11" : "eleven ", "12" : "twelve ", "13" : "thirteen ", "14" : "fourteen ", "15" : "fifteen ", "16" : "sixteen ", "17" : "seventeen ", "18" : "eighteen ", "19" : "nineteen " }

tens = { "2" : "twenty-", "3" : "thirty-", "4" : "fourty-", "5" : "fifty-", "6" : "sixty-", "7" : "seventy-", "8" : "eighty-", "9" : "ninety-" }

powers = { 3 : "thousand, ", 6 : "million, ",  9 : "billion, ", 12 : "trillion, ", 15 : "quadrillion, ", 18 : "quintillion, " }

Name: Anonymous 2010-07-27 23:41


package org.4chan.dis.read.prog._1280041293._62;

public class Numerics2Words
{ public static final String[] words = {"", "one", "two", "three", "four", "five",
                                        "six", "seven", "eight", "nine"};
 
  public static void main(String... args)
  { if (args.length < 1)
    { throw new IllegalArgumentException("Usage: java Numerics2Words ");
 
    final String numeric = args[0];
    final StringBuilder result = new StringBuilder();
    for (int i = 0; i < numeric.length; i++)
    { final char c = numeric.charAt(i);
      result.append(words[Integer.parseInt(c).intValue()]);   
    }
   
    System.out.println(result.toString());
  }
}

Name: Anonymous 2010-07-28 0:01

>>60

I came back and i noticed a few mistakes. I fixed them. Sorry about that.

exact = { "1" : "one ", "2" : "two ", "3" : "three ", "4" : "four ", "5" : "five ", "6" : "six ", "7" : "seven ", "8" : "eight ", "9" : "nine ", "10" : "ten ", "11" : "eleven ", "12" : "twelve ", "13" : "thirteen ", "14" : "fourteen ", "15" : "fifteen ", "16" : "sixteen ", "17" : "seventeen ", "18" : "eighteen ", "19" : "nineteen " }

tens = { "2" : "twenty-", "3" : "thirty-", "4" : "fourty-", "5" : "fifty-", "6" : "sixty-", "7" : "seventy-", "8" : "eighty-", "9" : "ninety-" }

powers = { 3 : "thousand, ", 6 : "million, ",  9 : "billion, ", 12 : "trillion, ", 15 : "quadrillion, ", 18 : "quintillion, " }

def num_to_name( num_str ):
    global exact, tens, powers
    # Convert to int and back, which fixes whitespace and leading zeroes.
    try:
        num_str = str(int(num_str))
    except ValueError:
        return "That was not a number!"
   
    if num_str == "0":
        return "zero"
   
    strlen = len(num_str)
   
    if strlen > 18 + 1 :
        return "That number is too large for my puny brain!"
   
    ans = [] # List to hold number words.
   
    i = -1
    while i < (strlen - 1):
        i += 1
       
        if num_str[i] != "0":
            m = (strlen - i - 1) % 3
       
            if m == 2:
                # Hundreds
                ans.append( exact[ num_str[i] ] )
                ans.append( "hundred " )
                continue
           
            elif m == 1:
                # Tens
                if num_str[i] == "1":
                    ans.append( exact[ num_str[i:i+2] ] )
                    i += 1
                else:
                    ans.append ( tens[ num_str[i] ] )
                   
                    if num_str[i + 1] == "0":
                        # Correct dashes for 20, 30, etc.
                        ans[-1] = ans[-1][:-1] + " "
                   
                    continue
            else:
                # Ones
                ans.append( exact[ num_str[i] ] )
       
        # Powers
        o = strlen - i - 1
       
        if o and not (o % 3) :
            ans.append( powers[o] )
       
        while i < (strlen - 1) and num_str[i + 1] == "0":
            # Correct for trailing zeroes.
            i += 1
   
    return "".join(ans).rstrip(" ,")

Name: Anonymous 2010-07-28 0:08

Name: Anonymous 2010-07-28 1:19

>>62,64
The indentation annoys me more than the choice of language. If K&R is too hard for you, at least stick to Sun's style guidelines.

Name: Anonymous 2010-07-28 1:43

hi,
how can convert the rupees into word.
Rupees could be more than million as well asd it may contain paise.

for example : 58234453434.50

with regards,
gopi

Name: Anonymous 2010-07-28 1:51

echo 12^2|bc
what?

Name: Anonymous 2010-07-28 2:21

When are the results announced?

Name: Anonymous 2010-07-28 3:15

>>68
What do you care? Xarn wins anyway.

Name: Anonymous 2010-07-28 3:17

>>69
Which one is Sarns?

Name: Anonymous 2010-07-28 5:21

>>66
I lol'd.

Name: Anonymous 2010-07-28 13:49

>>69
Xarn hasn't won most challenges in /prog/'s history. Still, if you're worried, these challenges could probably have one category for Xarn, and one for best of the rest.

>>70
>>17, I'm pretty sure.

Name: Anonymous 2010-07-28 13:58

>>72
Actually Xarn wouldn't post any code written the ``Xarn Way'' as he is not an egotist; the tripcode in >>17 is clearly someone else's attempt at satire. Xarn would just use his usual name and tripcode.

Name: Anonymous 2010-11-25 9:10

Name: Anonymous 2011-02-03 5:34

<

Name: Anonymous 2011-02-04 15:35

Name: Anonymous 2011-02-18 14:19

<-- check 'em dubzNewer Posts
Don't change these.
Name: Email:
Entire Thread Thread List