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

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #30 on: February 13, 2013, 05:43:12 PM »
Wow ... once I saw your code I though: How simple! (read as compliment  ;) ) I could've kicked myself for not getting to that half-functional / half-imperative but iterative method!

Thanks Irne!  :-)

I'm now thinking, what about other more convoluted numbering systems? Say the Babilonian system?

Nice idea! - here is my Babylonian function (assuming I have understood the system correctly):
Code: [Select]
(defun LM:int->babylonian ( n )
    (if (< n 60)
        (strcat (substr "<<<<<" 1 (/ n 10)) (substr "TTTTTTTTT" 1 (rem n 10)))
        (strcat (LM:int->babylonian (/ n 60)) " " (LM:int->babylonian (rem n 60)))
    )
)
Code: [Select]
_$ (LM:int->babylonian 424000)
"T <<<<<TTTTTTT <<<<TTTTTT <<<<"
« Last Edit: February 13, 2013, 06:08:18 PM by Lee Mac »

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #31 on: February 13, 2013, 05:59:48 PM »
Good one! Slight correction though:
Code - Auto/Visual Lisp: [Select]
  1. (defun LM:int->babylonian ( n )
  2.     (if (< n 60)
  3.         (strcat (substr "<<<<<" 1 (/ n 10)) (substr "TTTTTTTTT" 1 (rem n 10)))
  4.         (strcat (LM:int->babylonian  (/ n 60)) " " (LM:int->babylonian  (rem n 60)))
  5.     )
  6. )
But it works!

I'm just in 2 minds about the use of spaces. It's a bit difficult to figure out if "T " (60) or "T  " (3600) is the same thing.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #32 on: February 13, 2013, 06:07:24 PM »
Good one! Slight correction though  < ... >

Oops! That'll teach me to modify code in the forum post box... code updated above.
Thanks Irne!

I'm just in 2 minds about the use of spaces. It's a bit difficult to figure out if "T " (60) or "T  " (3600) is the same thing.

True - I saw that point noted in the article but wanted to emulate the Babylonian system as they described.  :-)

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #33 on: February 13, 2013, 06:17:25 PM »
Actually again, your code's inspired me to get to a much more succinct algorithm (the iterative version of yours):
Code - Auto/Visual Lisp: [Select]
  1. (defun IB:Int->Babylonian (num / result)
  2.   (setq result "")
  3.   (while (> num 0) (setq result (strcat " " (substr "<<<<<" 1 (fix (/ num 10)))
  4.                                         (substr "TTTTTTTTT" 1 (fix (rem num 10))) result)
  5.                          num (/ num 60)))
  6.   (vl-string-left-trim " " result))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #34 on: February 13, 2013, 06:24:44 PM »
Actually again, your code's inspired me to get to a much more succinct algorithm (the iterative version of yours)

Careful Irne...
Code: [Select]
_$ (IB:int->babylonian 60)
"T <<<<<"
_$ (IB:int->babylonian 70)
"T <<<<<"
_$ (IB:int->babylonian 80)
"T <<<<<"

I would write the iterative version as:
Code: [Select]
(defun LM:int->babylonian-i ( n / d r )
    (setq r "")
    (while (< 0 n)
        (setq d (rem n 60)
              r (strcat " " (substr "<<<<<" 1 (/ d 10)) (substr "TTTTTTTTT" 1 (rem d 10)) r)
              n (/ n 60)
        )
    )
    (vl-string-left-trim " " r)
)
« Last Edit: February 13, 2013, 06:41:49 PM by Lee Mac »

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #35 on: February 13, 2013, 06:56:50 PM »
How about Mayan  :lol:

Code: [Select]
(defun LM:int->mayan ( n )
    (if (zerop n) "0"
        (if (< n 20)
            (strcat (substr "" 1 (rem n 5)) (substr "|||" 1 (/ n 5)))
            (strcat (LM:int->mayan (/ n 20)) " " (LM:int->mayan (rem n 20)))
        )
    )
)

Code: [Select]
_$ (LM:int->mayan 5125)
"|| ||| |"

[ Read with head tipped sideways ]

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #36 on: February 13, 2013, 09:02:55 PM »
lol

pBe

  • Bull Frog
  • Posts: 377
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #37 on: February 14, 2013, 01:53:48 AM »
I applaud each and everyone who participated on this challenge. even the "klingon" inspired conversion.
Hats off to you guys
I learned a lot.


Thank you very much.  :)

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #38 on: February 14, 2013, 03:09:48 AM »
Careful Irne...
Good catch! Thanks Lee.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #39 on: February 14, 2013, 09:45:56 AM »
Careful Irne...
Good catch! Thanks Lee.
No worries!  :-)

Another, for fun: Morse
Code: [Select]
(defun LM:int->morse ( n )
    (if (< n 10)
        (substr "----------" (- 11 n) 5)
        (strcat (LM:int->morse (/ n 10)) " " (LM:int->morse (rem n 10)))
    )
)
or:
Code: [Select]
(defun LM:int->morse ( n )
    (substr
        (apply 'strcat
            (mapcar '(lambda ( n ) (strcat " " (substr "----------" (- 59 n) 5)))
                (vl-string->list (itoa n))
            )
        )
        2
    )
)
Code: [Select]
_$ (LM:int->morse 2013)
"--- ----- ---- --"

pBe

  • Bull Frog
  • Posts: 377
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #40 on: February 14, 2013, 10:58:28 AM »

Another, for fun: Morse

you should add "mind-reading" on your set of skills Lee, as that would have been my next challenge. <decrypting  morse code>

Good times LM

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #41 on: February 14, 2013, 11:48:21 AM »

Another, for fun: Morse

you should add "mind-reading" on your set of skills Lee, as that would have been my next challenge. <decrypting  morse code>

Good times LM

:lol:

Though, unfortunately general alphabetical Morse code isn't as interesting, since it doesn't follow a pattern like the Morse code for the integers...

Here are two very quickly written conversion functions:
Code: [Select]
(defun words->morse ( s )
    (substr
        (apply 'strcat
            (mapcar
                (function
                    (lambda ( a )
                        (if (setq a (cadr (assoc a morsetable)))
                            (strcat " " a)
                            ""
                        )
                    )
                )
                (vl-string->list (strcase s))
            )
        )
        2
    )
)

(defun morse->words ( s / f )
    (defun f ( s l / c p )
        (cond
            (   (wcmatch s "   *")
                (cons 32 (f (substr s 4) l))
            )
            (   (setq p (vl-string-position 32 s))
                (if (setq c (cadr (assoc (substr s 1 p) l)))
                    (cons c (f (substr s (+ 2 p)) l))
                    (f (substr s (+ 2 p)) l)
                )
            )
            (   (setq c (cadr (assoc s l)))
                (list c)
            )
            (   ""   )
        )
    )
    (vl-list->string (f s (mapcar 'reverse morsetable)))
)

(setq morsetable
   '(
        (32 "  ")
        (65 "-")
        (66 "-")
        (67 "--")
        (68 "-")
        (69 "")
        (70 "-")
        (71 "--")
        (72 "")
        (73 "")
        (74 "---")
        (75 "--")
        (76 "-")
        (77 "--")
        (78 "-")
        (79 "---")
        (80 "--")
        (81 "---")
        (82 "-")
        (83 "")
        (84 "-")
        (85 "-")
        (86 "-")
        (87 "--")
        (88 "--")
        (89 "---")
        (90 "--")
    )
)

Code: [Select]
_$ (words->morse "THE SWAMP")
"-     -- - -- --"
_$ (morse->words (words->morse "THE SWAMP"))
"THE SWAMP"

StykFacE

  • Guest
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #42 on: February 14, 2013, 12:19:14 PM »
I have a mathematics degree...   :-)
THAT explains it!!  :kewl:

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England

Dilan

  • Newt
  • Posts: 23
Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #44 on: December 14, 2018, 11:38:36 PM »
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  :-)
Hello!
And how to make words-> int?
From "twenty one thousand five hundred thirty seven" in 21537.
For example.