Author Topic: < Challenge > Integers into words / Ordinal Numbers into words  (Read 15532 times)

0 Members and 1 Guest are viewing this topic.

pBe

• Bull Frog
• Posts: 377
< Challenge > Integers into words / Ordinal Numbers into words
« on: February 08, 2013, 10:19:32 AM »
(Int2Words 1234)
"One Thousand Two Hundred Thirty-Four"

(Int2Words 34567)
"Thirty-Four Thousand Five Hundred Sixty-Seven"

AND

(Ord2Words 17)
"Seventeenth"

(Ord2Words 345)
"Three Hundred Forty-Fifth"

irneb

• Water Moccasin
• Posts: 1794
• ACad R9-2016, Revit Arch 6-2016
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #1 on: February 08, 2013, 10:39:17 AM »
Perhaps something like the int->word or int->wordth functions in the attached?

Wanted to link to my online version, though for some reason SourceForge seems to have an issue:
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

pBe

• Bull Frog
• Posts: 377
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #2 on: February 08, 2013, 10:52:50 PM »
Perhaps something like the int->word or int->wordth functions in the attached?
....

Rats!! you already have one, i'll post my attempt later.

Cheers Irné

EDIT:
Code - Auto/Visual Lisp: [Select]
1. (defun Int2Words (num /  _set a b c d e f i p place lst v)
2.   (defun _zerop (l) (zerop (apply '+ l)))
3.   (Defun _set (w f / f)
4.     (if   (setq zero (_zerop w))
5.       (setq f f)
6.    (while (setq d (car w))
7.      (if (= (length w) 2)
8.        (setq f
9.           (if
10.             (> (setq g (read (apply 'strcat (mapcar 'itoa w)))) 19)
11.              (strcat f (nth d b) (nth (cadr w) a))
12.              (strcat f (nth g a))
13.              )
14.         w nil
15.         )
16.        (setq f (strcat f
17.              (nth d a)
18.              (if   (or zero (= d 0))
19.                ""
20.                (nth (1- (length w)) place)
21.                )
22.              )
23.         )
24.        )
25.      (setq w (cdr w))
26.      )
27.    )
28.       )
29.     f
30.     )
31.
32.
33.   (Setq   a     '(""         " One"       " Two"     " Three"
34.       " Four"         " Five"       " Six"     " Seven"
35.       " Eight"      " Nine"       " Ten"     " Eleven"
36.       " Twelve"     " Thirteen"   "Fourteen"     " Fifteen"
37.       " Sixteen"    " Seventeen"  " Eightteen"  " Nineteen"
38.       )
39.    b     '(""        " Ten"     " Twenty"    " Thirty"
40.       " Forty"     " Fifty"     " Sixty"     " Seventy"
41.       " Eighty"    " Ninety"
42.       )
43.    place '("" "" " Hundred" " Thousand" " Million"   " Billion"
44.       " Trillion")
45.    )
46.   (setq   f   ""
47.    lst nil
48.    v   (mapcar '(lambda (x) (- x 48)) (vl-string->list (rtos num 2 0)))
49.    i   (length v)
50.    )
51.   (while (>= i 3)
52.     (setq lst (cons (list
53.             (nth (- i 3) v)
54.             (nth (- i 2) v)
55.             (nth (1- i) v)
56.             )
57.           lst
58.           )
59.      i   (- i 3)
60.      )
61.     )
62.   (setq   lst
63.     (if (zerop i)
64.       lst
65.       (cons (if (= i 2)
66.          (list (car v) (cadr v))
67.          (list (car v))
68.          )
69.        lst
70.        )
71.       )
72.    )
73.   (foreach itm lst
74.     (setq f (_set itm f)
75.      )
76.     (if   (> (length lst) 1)
77.       (setq f   (strcat   f
78.          (if (or (_zerop itm) (zerop (car itm)))
79.            ""
80.            (nth (1+ (length lst)) place)
81.            )
82.          )
83.        lst   (cdr lst)
84.        )
85.       )
86.     )
87. (if (eq f "") "zero" F)
88.   )
89.
« Last Edit: February 09, 2013, 03:52:48 AM by pBe »

pBe

• Bull Frog
• Posts: 377
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #3 on: February 09, 2013, 04:22:40 AM »
Odd

(INT->WORD 12345678901234)
"twelve thousand three hundred fourty five billion six hundred seventy eight million nine hundred one thousand two hundred thirty four"
(INT2WORDS 12345678901234)
"Twelve Trillion Three Hundred Forty Five Billion Six Hundred Seventy Eight Million Nine Hundred One Thousand Two Hundred Thirty Four"

(_COMMA (RTos 12345678901234 2 0))
"12,345,678,901,234"

"twelve trillion three hundred forty-five billion six hundred seventy-eight million nine hundred one thousand two hundred thirty-four"

irneb

• Water Moccasin
• Posts: 1794
• ACad R9-2016, Revit Arch 6-2016
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #4 on: February 09, 2013, 04:33:08 AM »
Yeah! Mine is using the American "Short Scale" naming. Yours is actually using the Long Scale (more logical use though).

I just went with the most prevalent one throughout the world: http://en.wikipedia.org/wiki/Long_and_short_scales#Current_usage

Though one which is still making my head spin a bit (was working on it last night and just gave up for bed) is the reverse: Taking the string of words and reverting it into a number.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

pBe

• Bull Frog
• Posts: 377
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #5 on: February 09, 2013, 04:40:46 AM »

Though one which is still making my head spin a bit (was working on it last night and just gave up for bed) is the reverse: Taking the string of words and reverting it into a number.

For now I wouldn't go there either  , but i guess a play with wcmatch would do the trick taking into consideration typos and all.

Lee Mac

• Seagull
• Posts: 12514
• London, England
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #6 on: February 09, 2013, 07:14:01 AM »
Here is my version:

Code: [Select]
`(defun LM:int->words ( n / f1 f2 )    (defun f1 ( n )        (if (< n 20)            (nth (fix n) '("" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve" "thirteen" "fourteen" "fifteen" "sixteen" "seventeen" "eighteen" "nineteen"))            (strcat (nth (- (fix (/ n 10)) 2) '("twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty" "ninety")) " " (f1 (rem n 10)))        )    )    (defun f2 ( n l )        (cond            (   (null l) (f1 n))            (   (< n (caar l)) (f2 n (cdr l)))            (   (vl-string-right-trim " " (strcat (f2 (fix (/ n (caar l))) (cdr l)) " " (cadar l) " " (f2 (rem n (caar l)) (cdr l)))))        )    )    (if (zerop n)        "zero"        (vl-string-right-trim " "            (f2 n               '(                    (1e18 "quintillion")                    (1e15 "quadrillion")                    (1e12 "trillion")                    (1e09 "billion")                    (1e06 "million")                    (1e03 "thousand")                    (1e02 "hundred")                )            )        )    ))`
Nice challenge pBe

pBe

• Bull Frog
• Posts: 377
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #7 on: February 09, 2013, 08:36:42 AM »
Here is my version:

Code: [Select]
`(defun LM:int->words ( n / f1 f2 ).......            (f2 n               '(                    (1e18 "quintillion")                    (1e15 "quadrillion")                    (1e12 "trillion")                    (1e09 "billion")                    (1e06 "million")                    (1e03 "thousand")                    (1e02 "hundred")                )            )        )    ))`
Nice challenge pBe

It took me a a while to understand your approach.
I see that you have an uncanny understanding of numbers LM, i wouldnt have thought of that.

Clever

VovKa

• Swamp Rat
• Posts: 1370
• Ukraine
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #8 on: February 09, 2013, 09:14:54 AM »
one from me
no thinking involved
Code: [Select]
`(vl-load-com)(defun vk_PostRemoteText (URL Data / objHTTP Result)  (if (setq objHTTP (vlax-create-object "Msxml2.ServerXMLHTTP"))    (progn      (setq Result (vl-catch-all-apply      (function        (lambda () (vlax-invoke-method    objHTTP "Open" "POST" URL :vlax-false) (vlax-invoke-method    objHTTP    "setRequestHeader"    "Content-type"    "application/x-www-form-urlencoded" ) (vlax-invoke objHTTP "Send" Data) (vlax-get-property objHTTP "ResponseText")        )      )    )      )      (vlax-release-object objHTTP)      (if (not (vl-catch-all-error-p Result)) Result      )    )  ))(defun vk_test (Num Lng / Result)  (if (setq Result (vk_PostRemoteText      "http://www.tools4noobs.com/"      (strcat "action=ajax_number_spell_words&number="      (rtos Num 2 0)      "&type=0&locale="      Lng      )    )      )    (substr Result 19 (- (strlen Result) 24))  ));;;(vk_test 662423123123442 "en_US");;;(vk_test 662423123123442 "en_GB");;;(vk_test 662423123123442 "it")`sorry to disappoint you guys, but no Ukrainian language support

Lee Mac

• Seagull
• Posts: 12514
• London, England
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #9 on: February 09, 2013, 12:25:17 PM »
It took me a a while to understand your approach.
I see that you have an uncanny understanding of numbers LM, i wouldnt have thought of that.

Clever

Cheers pBe
I have a mathematics degree...

pBe

• Bull Frog
• Posts: 377
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #10 on: February 10, 2013, 07:01:26 AM »
one from me
no thinking involved
Code: [Select]
`(vl-load-com)(defun vk_PostRemoteText (URL Data / objHTTP Result) ..........);;;(vk_test 662423123123442 "en_US");;;(vk_test 662423123123442 "en_GB");;;(vk_test 662423123123442 "it")`

You took the fun out of thinking/debugging/brain freeze/nose bleed.. hang on, come to think of  it.. nobody deserves to go through all that for a challenge ...

Carry on VovKa

I have a mathematics degree...
Figured as much

Cheers Lee

Marc'Antonio Alessi

• Swamp Rat
• Posts: 1210
• Marco
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #11 on: February 11, 2013, 10:31:24 AM »
This is my version, needs to be optimized, accepts only integers...
I wanted start from my function ALE_Int->Roman (see attached) but it was better if I left from zero.
(ALE_Int->Roman 3886) => "MMMDCCCLXXXVI"

Code: [Select]
`(defun ALE_IntsToWords (IntVal / IntStr 1119Ls 000Lst TmpNum TmpDec OutVal FlgThs StrXxx StrLst)  (setq    IntStr (itoa IntVal)   OutVal ""    100Lst     '(((48 . ""     )(49 . "One"  )(50 . "Two"    )(51 . "Three" )(52 . "Four"  )        (53 . "Five" )(54 . "Six"  )(55 . "Seven"  )(56 . "Eight" )(57 . "Nine"  ))        ((48 . ""     )(49 . "Ten"  )(50 . "Twenty" )(51 . "Thirty")(52 . "Forty" )         (53 . "Fifty")(54 . "Sixty")(55 . "Seventy")(56 . "Eighty")(57 . "Ninety")       ))    1119Ls     '((11 . "Eleven"  )(12 . "Twelve" )(13 . "Thirteen" )(14 . "Fourteen")       (15 . "Fifteen" )(16 . "Sixteen")(17 . "Seventeen")(18 . "Eighteen")       (19 . "Nineteen")      )     000Lst '("Thousand" "Million" "Billion" "Trillion" ; "Quadrillion"             ; "Quintillion" "Sextillion" "Septillion" "Octillion" "Nonillion"            ; "Decillion" "Undecillion" "Deuodecillion" "Tredecillion"            ; "Quattuordecillion" "Quindecillion" "Sexdecillion" "Septendecillion"            ; "Octodecillion" "Novemdecillion" "Vigintillion"            )     StrLst (ALE_SplitThousand IntStr)         )  (While (setq StrXxx (car StrLst))    (setq StrLst (cdr StrLst)   TmpNum (atoi StrXxx))    (if TmpDec      (progn        (and          (or (and FlgThs (not (zerop TmpNum))) (not StrLst))          (setq OutVal (strcat " " (car 000Lst) " " OutVal))        )        (setq 000Lst (cdr 000Lst))      )    )    (setq TmpDec (substr StrXxx 2)   FlgThs T)    (cond      ( (zerop TmpNum) (setq FlgThs nil) )      ( (< 0 TmpNum 11) (setq OutVal (strcat (ALE_OneToHundred StrXxx 100Lst) OutVal)) )      ( (> 20 TmpNum 10)        (setq OutVal (strcat (cdr (assoc TmpNum 1119Ls)) OutVal))      )      ( (> 20 (atoi TmpDec) 10)        (setq OutVal (strcat (cdr (assoc (atoi TmpDec) 1119Ls)) OutVal))      )      ( (> TmpNum 99) (setq OutVal (strcat (ALE_OneToHundred TmpDec 100Lst) OutVal)) )      ( T             (setq OutVal (strcat (ALE_OneToHundred StrXxx 100Lst) OutVal)) )    )    (and      (> TmpNum 99)      (setq OutVal        (strcat (cdr (assoc (ascii (substr StrXxx 1 1)) (car 100Lst))) " Hundred " OutVal)      )    )  )  (if (zerop IntVal) "Zero" (vl-string-right-trim " " OutVal)))(defun ALE_OneToHundred (IntStr DatLst / OutStr)  (foreach ForElm (reverse (vl-string->list IntStr))    (setq OutStr      (cond        ( (eq 48 ForElm) OutStr )        ( OutStr (strcat (cdr (assoc ForElm (car DatLst))) "-" OutStr) )        ( T (cdr (assoc ForElm (car DatLst))) )      )    )    (setq DatLst (cdr DatLst))  )  (if OutStr OutStr ""))(defun ALE_SplitThousand (IntStr / TmpLen StrLng OutLst)  (setq StrLng (strlen IntStr)  TmpLen StrLng)  (if (= (setq TmpLen (rem TmpLen 3)) 0)(setq TmpLen 3))  (while (<= TmpLen StrLng)    (setq      OutLst (cons  (substr IntStr 1 TmpLen) OutLst)      IntStr (substr IntStr (1+ TmpLen))      StrLng (- StrLng TmpLen)      TmpLen 3    )  )  OutLst)`

pBe

• Bull Frog
• Posts: 377
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #12 on: February 12, 2013, 01:15:40 AM »

(ALE_Int->Roman 3886) => "MMMDCCCLXXXVI"

I like it

Thank you for participating on the challenge Marc.

Cheers

bchapman

• Guest
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #13 on: February 12, 2013, 02:16:17 AM »
Modified Lee's to be partially in Klingon lol:

Code: [Select]
`(defun LM:int->words ( n / f1 f2 )    (defun f1 ( n )        (if (< n 20)            (nth (fix n) '("" "wa'" "cha'" "wej" "loS" "vagh" "jav" "Soch" "chorgh" "Hut" "wa'maH" "wa’maH wa’" "wa’maH cha’" "wa’maH wej" "wa’maH loS" "wa’maH vagh" "wa’maH jav" "wa’maH Soch" "wa’maH chorgh" "wa’maH Hut"))            (strcat (nth (- (fix (/ n 10)) 2) '("cha'maH" "wejmaH" "IosMah" "vaghmaH" "javmaH" "SochmaH" "chorghmaH" "HutmaH")) " " (f1 (rem n 10)))        )    )    (defun f2 ( n l )        (cond            (   (null l) (f1 n))            (   (< n (caar l)) (f2 n (cdr l)))            (   (vl-string-right-trim " " (strcat (f2 (fix (/ n (caar l))) (cdr l)) " " (cadar l) " " (f2 (rem n (caar l)) (cdr l)))))        )    )    (if (zerop n)        "zero"        (vl-string-right-trim " "            (f2 n               '(                    (1e18 " ")                    (1e15 " ")                    (1e12 " ")                    (1e09 "wa'Sad uy")                    (1e06 "uy")                    (1e03 "wa’SaD")                    (1e02 "wa’vatlh")                )            )        )    ))`
Here is my version:

Code: [Select]
`(defun LM:int->words ( n / f1 f2 )    (defun f1 ( n )        (if (< n 20)            (nth (fix n) '("" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve" "thirteen" "fourteen" "fifteen" "sixteen" "seventeen" "eighteen" "nineteen"))            (strcat (nth (- (fix (/ n 10)) 2) '("twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty" "ninety")) " " (f1 (rem n 10)))        )    )    (defun f2 ( n l )        (cond            (   (null l) (f1 n))            (   (< n (caar l)) (f2 n (cdr l)))            (   (vl-string-right-trim " " (strcat (f2 (fix (/ n (caar l))) (cdr l)) " " (cadar l) " " (f2 (rem n (caar l)) (cdr l)))))        )    )    (if (zerop n)        "zero"        (vl-string-right-trim " "            (f2 n               '(                    (1e18 "quintillion")                    (1e15 "quadrillion")                    (1e12 "trillion")                    (1e09 "billion")                    (1e06 "million")                    (1e03 "thousand")                    (1e02 "hundred")                )            )        )    ))`
Nice challenge pBe
« Last Edit: February 12, 2013, 02:53:51 AM by BC_in_NV »

Marc'Antonio Alessi

• Swamp Rat
• Posts: 1210
• Marco
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #14 on: February 12, 2013, 05:02:02 AM »

Thank you for participating on the challenge Marc.

Thanks to you.

Here is my version:
Code: [Select]
`(defun LM:int->words ( n / f1 f2 )  ...  )`

Lee,

I was rewriting my version to accept reals:
Code: [Select]
`(defun ALE_SplitThousand (NumVal / TmpVal OutLst)  (while (not (zerop NumVal))    (if (zerop (setq TmpVal (fix (rem NumVal 1e03))))      (setq OutLst (cons "000" OutLst))      (setq OutLst (cons (itoa TmpVal) OutLst))    )    (setq NumVal (fix (/ NumVal 1e03)))  )  OutLst)`
and I discovered that:
(LM:int->words 12300120123010211)
"twelve quadrillion three hundred trillion one hundred twenty billion
one hundred twenty three million ten thousand two hundred ten"