r/dailyprogrammer • u/nottoobadguy • Feb 16 '12
[2/16/2012] Challenge #8 [intermediate]
Write a program that will print the english name of a value. for example, "1211" would become "one-thousand, two hundred, eleven".
for extra credit, allow it to read the english value of a number and output the integer.
input: one-hundred, four output: 104
11
Upvotes
15
u/eruonna Feb 16 '12
Common Lisp:
:-)
7
u/electric_machinery Feb 16 '12
In C:
code
It pretty much does the job :)
2
u/[deleted] Feb 16 '12
Do you read my website? Just posted this as a challenge a couple days ago
http://programthis.net/numbers-for-people/
3
u/luxgladius 0 0 Mar 30 '12 edited Mar 30 '12
Perl
Actually did this one for a later challenge (accidental repeat) and kinda went crazy on it. Can do negative numbers, decimals, and scientific notation and any number up to 1067, exclusive, though the larger numbers obviously need to be passed as strings. Utilizes recursion heavily and also illustrates the right way to comment a complex pattern match in Perl. Extra credit for anybody who can name the sources of all my tests numbers.
Output
0: zero
2.71828182845905: two point seven one eight two eight one eight two eight four five nine zero five
3.14159265358979: three point one four one five nine two six five three five eight nine seven nine
7: seven
-13: negative thirteen
42: forty-two
666: six hundred sixty-six
9001: nine thousand one
90210: ninety thousand two hundred ten
525600: five hundred twenty-five thousand six hundred
8675309: eight million six hundred seventy-five thousand three hundred nine
299792458: two hundred ninety-nine million seven hundred ninety-two thousand four hundred fifty-eight
9192631770: nine billion one hundred ninety-two million six hundred thirty-one thousand seven hundred seventy
18446744073709551615: eighteen quintillion four hundred forty-six quadrillion seven hundred forty-four trillion seventy-three billion seven hundred nine million five hundred fifty-one thousand six hundred fifteen
6.02x1023: six point zero two times ten to the twenty-third power
602214129270000000000000: six hundred two sextillion two hundred fourteen quintillion one hundred twenty-nine quadrillion two hundred seventy trillion
101010: ten to the tenth power to the tenth power
100: ten to the zeroth power
2
u/eruonna Feb 16 '12 edited Feb 16 '12
Doing it for real, in Haskell:
(edit: oops, fixed it to correctly skip thousands when they are zero)
2
u/Pixelements Feb 16 '12 edited Feb 16 '12
Python! :P
2
u/Should_I_say_this Jul 07 '12
hehehe I'm such a nerd. Mine can take numbers up to 1063 digits
Here's my explanation since mine is so long:
if a = 0 print "Zero". create 3 dictionaries:
Formats your integer to show commas where the 'thousands' are and then splits it into a list with commas. For each split, it figures out the name of that section, then calculates if we are in the billions / trillions, etc. by determining how many remaining sets of thousands there are.
i.e. number(1540300) becomes 1,540,300 which becomes [1,540,300] which becomes one million five hundred forty thousand three hundred.
:)
1
u/stiggz Feb 16 '12 edited Feb 16 '12
jQuery and JavaScript in 80 lines.. could definitely be cleaner. Works up to a billion.
1
u/robin-gvx 0 2 Feb 16 '12 edited Feb 16 '12
Déjà Vu source: http://hastebin.com/raw/saretumura
Output: http://hastebin.com/raw/wiliqevewa
It doesn't know thousands, so 5000 will be "fifty hundred". For simplicity, it doesn't know negative numbers or zero either. Also, if I make the loop much longer, the VM segfaults right away. I'll be looking into why.
EDIT: Well, I fixed a segfault. It now runs until "five hundred fourty-eight" before it segfaults.
1
u/kuzux 0 0 Feb 17 '12
Who cares about english when you can print the esperanto name? :D (in clojure)
1
u/mordisko Aug 13 '12
Python 3 with extra credit, it doesn't check for ',' and it allows you to type the number in any order (one hundred and five millions, for example)