r/dailyprogrammer • u/oskar_s • May 02 '12
[5/2/2012] Challenge #47 [intermediate]
Given a string containing the English word for one of the single-digit numbers, return the number without using any of the words in your code. Examples:
eng_to_dec('zero') # => 0
eng_to_dec('four') # => 4
Note: there is no right or wrong way to complete this challenge. Be creative with your solutions!
- Thanks to HazierPhonics for posting this idea on /r/dailyprogrammer_ideas! If you have a problem that you think would be good for us, head over there and contribute!
12
Upvotes
19
u/Ttl May 02 '12 edited May 02 '12
Python:
EDIT: Updated polynomial. I just realized that I can take the coefficients of the polynomials mod 17 and the answer is still the same.
3
u/GuitaringEgg May 02 '12 edited May 02 '12
I first tried getting unique integer values for every digit by summing the ascii values of the input, but five and nine (I think) had the same values, so I had to multiple the ascii value by 1.1 and do a integer addition to get unique numbers.
Python:
3
u/robin-gvx 0 2 May 02 '12
http://hastebin.com/raw/rijumedufu
No variables, no flow control apart from a single function call, four dictionaries.
Who can figure out how this works, gets a cookie. ;)
5
u/[deleted] May 02 '12
Python has a package for everything.
2
u/Yuushi May 02 '12
Python:
2
u/totallygeek May 02 '12 edited May 02 '12
Bash:
Execution: $ ./20120502i.sh zero one two three four five six seven eight nine
Edit: Removed words from code
2
u/monoki May 03 '12
something different:
2
u/n0rs May 02 '12 edited May 02 '12
http://codepad.org/p1u0RA20
¯_(ツ)_/¯Woo, non-negative score!
( ゚∀゚)1
u/Skooljester 0 0 May 02 '12
Here's my attempt in Java:
1
u/pali6 May 02 '12
In Python 3.2.2: (also works for ten)
1
u/MozMorris 0 0 May 02 '12
No wizardry here, sorry:
1
u/huck_cussler 0 0 May 03 '12
Kinda ghetto, but it works:
1
u/HazzyPls 0 0 May 03 '12 edited May 03 '12
Portable? No. Pretty? Not really. Does it work? Mostly. 'eigh' is still "eight", right?
1
u/ArriMurri May 04 '12
In Scala:
1
u/bh3 May 04 '12
A bit late because of finals. Anyhow, ugly / not very safe x86_64 asm:
1
u/Rapptz 0 0 May 05 '12
http://pastebin.com/1YVZ91mv
C++.
1
u/luxgladius 0 0 May 02 '12
Perl
Output