47
\$\begingroup\$

Winner: professorfish's bash answer! An entire 9 bytes! Wow!

You may continue to submit your answer, however you can no longer win. Original post kept for posterity:


Your goal is to convert a whole number between 1-9 into the word it represents.

  • You will not need to worry about decimals
  • The user will input a number. Assume that they will never enter anything 10 or higher
  • The user must type the number at some point, however the method the program reads it does not matter. It can be with stdin, reading a text file, etc, however the user must press the 9 button on their keyboard (for example) at some point
  • It is not case sensitive (ie, "one", "One", "oNe", "OnE", etc are all acceptable)
  • HTTP/etc requests are allowed, however any code executed by the server the request is made to counts towards the byte count of your final code (e.g. if I had a C++ program make a HTTP request, the PHP code used in the HTTP request counts)
  • Anything that can compile and run is acceptable

  • This contest has ended on June 27th, 2014 (7 days from posting).
  • This is a , so the shortest code wins
\$\endgroup\$
  • 2
    \$\begingroup\$ Is trailing whitespace (i.e. one ) acceptable? \$\endgroup\$ – grc Jun 21 '14 at 5:13
  • 1
    \$\begingroup\$ @grc Yes, as long as the program outputs the word. \$\endgroup\$ – Jon Jun 21 '14 at 5:14
  • 1
    \$\begingroup\$ you should specify that only the given number may be printed and not the other numbers. \$\endgroup\$ – Pinna_be Jun 21 '14 at 7:04
  • 1
    \$\begingroup\$ @Pinna_be for example, if I input 3, you can't output one two three four five six seven eight nine even though you technically output three. Similarly, you can't output three seven, etc. \$\endgroup\$ – Jon Jun 21 '14 at 7:06
  • 1
    \$\begingroup\$ This question was quite nice. But I don't like the adding of the date limit. Especially, the date limit has been added just before the date limit, not 7 days before. \$\endgroup\$ – Nicolas Barbulesco Jun 26 '14 at 18:12

70 Answers 70

0
\$\begingroup\$

PHP 5.5 (62)

<?=[0,one,two,three,four,five,six,seven,eight,nine][$argv[1]];

If you read from standard input it goes up to 66 characters.

<?=[0,one,two,three,four,five,six,seven,eight,nine][fgetc(STDIN)];
\$\endgroup\$
0
\$\begingroup\$

Bash, 64

d=(z one two three four five six seven eight nine)
echo ${d[$1]}

Takes the first argument as a number and uses it as an array index to print the corresponding word.

\$\endgroup\$
0
\$\begingroup\$

Python

here is a simple answer. In python, if you have a key-val mapping pair, you use a dict. this program waits for an input, (0-9) and converts it

n={
1:'one',2:'two',3:'three',4:'four',5:'five',6:'six',7:'seven',8:'eight',9:'nine',0:'zero'}
print n.get(int(raw_input()))
\$\endgroup\$
0
\$\begingroup\$

Python 34 / 28

For this code to work, you have to install num2words module. Also thanks to @undergroundmonorail for his suggestion.

import num2words
num2words(input())

If input() is not necessary , and user is allowed to enter number directly into code, then

import num2words
num2words(3)

will also work.

\$\endgroup\$
  • \$\begingroup\$ Why not num2words(raw_input())? Or even input(), since it's guaranteed to be a number? \$\endgroup\$ – undergroundmonorail Jun 25 '14 at 8:32
  • 1
    \$\begingroup\$ @undergroundmonorail Thanks. didn't knew about it. I'm not too good in python. \$\endgroup\$ – Registered User Jun 26 '14 at 4:23
0
\$\begingroup\$

In AppleScript ; 53 chars.

say(display dialog""default answer"")'s text returned

Here is a nicer version :

set n to text returned of (display dialog "?" default answer "")
say n

This script takes the input in a dialog. Then it tells the output to the user. The user does not even have to read ! This solution works even for people who are unable to read.

\$\endgroup\$
0
\$\begingroup\$

swipl: 104 bytes

get_char(C),number_codes(X,[C]),nth0(X,[o,one,two,three,four,five,six,seven,eight,nine,ten],Y),write(Y).

Brainfuck: 672 bytes

+>,-------------------------------------------------[-[-[-[-[-[-[-[[-]+
[------->++<]>.-----.+++++.---------.[-]<[-]>[-]]<[[-]--[----->+<]>-.++
++.--.+.++++++++++++.[-]<[-]>[-]][-]<[-]>[-]]<[[-]+[--------->++<]>+.++
++[->+++<]>.[--->+<]>-.+[->+++<]>.+++++++++.[-]<[-]>[-]][-]<[-]>[-]]<[[
-]+[--------->++<]>+.----------.-[--->+<]>.[-]<[-]>[-]][-]<[-]>[-]]<[[-
]--[----->+<]>.+++.[->++++++<]>.+[->+++<]>.[-]<[-]>[-]][-]<[-]>[-]]<[[-
]--[----->+<]>.+++++++++.++++++.---.[-]<[-]>[-]][-]<[-]>[-]]<[[-]------
--[-->+++<]>.------------.++++++++++.-------------..[-]<[-]>[-]][-]<[-]
>[-]]<[[-]--------[-->+++<]>.+++.--------.[-]<[-]>[-]][-]<[-]>[-]]<[[-]
+[------->++<]>+.-.---------.[-]]
\$\endgroup\$
  • \$\begingroup\$ IMHO and according to the rules the content of "swipl" should be counted, too. Technically it's a request to another program or am I wrong? \$\endgroup\$ – german_guy Jun 23 '14 at 12:40
  • 1
    \$\begingroup\$ Ups!. I'll change it. Thanks @german_guy \$\endgroup\$ – rpax Jun 23 '14 at 13:21
  • 2
    \$\begingroup\$ @german_guy changed! \$\endgroup\$ – rpax Jun 23 '14 at 13:29
  • \$\begingroup\$ @ProgramFOX I'm sorry I don't understand you. Now it is using only swipl. \$\endgroup\$ – rpax Jun 23 '14 at 13:55
  • \$\begingroup\$ @rpax Ah, now I see. I thought you were only using bash now. I'm sorry. \$\endgroup\$ – ProgramFOX Jun 23 '14 at 14:12
0
\$\begingroup\$

Sprects, 61 bytes

#<INPUT>#9nine#8eight#7seven#6six#5five#4four#3three#2two#1one#0zero

Replace <INPUT> with the number, because Sprects does not have any other form of input (bytecount is without the "input".)

\$\endgroup\$
0
\$\begingroup\$

///, 80 79 bytes

/_/\/\///0/zero_1/one_2/two_3/three_4/four_5/five_6/six_7/seven_8/eight_9/nine/

Append the number at the end of the code (this is ///'s input).

\$\endgroup\$
  • \$\begingroup\$ Nine // is just enough for /_/\/\// to save a byte. \$\endgroup\$ – Martin Ender Jul 21 '16 at 9:37
  • \$\begingroup\$ @MartinEnder As I told you in another comment, this won't work, because then _ will evaluate to \/\/, not //. If you want to prove it works, please comment a link to an example using this. \$\endgroup\$ – Erik the Outgolfer Jul 21 '16 at 9:39
  • \$\begingroup\$ slashes.tryitonline.net/… \$\endgroup\$ – Martin Ender Jul 21 '16 at 9:42
  • \$\begingroup\$ @MartinEnder So that is the reason I've had problems with forward slashes (/) in the past... \$\endgroup\$ – Erik the Outgolfer Jul 21 '16 at 9:44
0
\$\begingroup\$

C, 92 89 bytes Japanese(Hiragana , Katakana)

Hiragana (ひらがな)

h(n){char*h="いちに さんし ご ろくななはちきゅ";printf("%.6s%s",h+--n*6,n^9?"":"う");}

Katakana (カタカナ)

k(n){char*k="イチニ サンシ ゴ ロクシチハチキュ";printf("%.6s%s",k+--n*6,n^9?"":"ウ");}

http://sp.cis.iwate-u.ac.jp/sp/lesson/j/doc/numbers.html

ideone it!

\$\endgroup\$
  • \$\begingroup\$ Does this actually output in japanese? \$\endgroup\$ – Erik the Outgolfer Jul 20 '16 at 21:55
  • \$\begingroup\$ Yes. ideone.com test. @Eʀɪᴋ ᴛʜᴇ Gᴏʟғᴇʀ \$\endgroup\$ – o79y Jul 21 '16 at 3:34
0
\$\begingroup\$

Factor, 11 bytes

a builtin: number>text.

\$\endgroup\$

protected by Doorknob Jun 23 '14 at 19:41

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.