50
\$\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\$
23
  • 2
    \$\begingroup\$ Is trailing whitespace (i.e. one ) acceptable? \$\endgroup\$
    – grc
    Commented Jun 21, 2014 at 5:13
  • 1
    \$\begingroup\$ @grc Yes, as long as the program outputs the word. \$\endgroup\$
    – Jon
    Commented Jun 21, 2014 at 5:14
  • 1
    \$\begingroup\$ you should specify that only the given number may be printed and not the other numbers. \$\endgroup\$
    – Pinna_be
    Commented Jun 21, 2014 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
    Commented Jun 21, 2014 at 7:06
  • 2
    \$\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\$ Commented Jun 26, 2014 at 18:12

73 Answers 73

1 2
3
0
\$\begingroup\$

Ruby - 66

$><<%w[a one two three four five six seven eight nine][gets.to_i]
\$\endgroup\$
0
\$\begingroup\$

Zozotez Lisp: 122 78

((\(1 2 3 4 5 6 7 8 9)(p(e(r))))'one'two'three'four'five'six'seven'eight'nine)

Zozotez is a LISP-1 interpreter written in Extended BrainFuck. It's only data types are symbols and cons and supports first class functions and macros

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

Extended BrainFuck: 130

{z(-))}{b&z)<(-}+>,49-(-(-(-(-(-(-(-((-)<-|<nine<&z<(-|<eight<&b
|<seven<&b|<six<&b|<five<&z)<(-|<four<&b|<three<&b|<two<&b|<one<&z

EBF is a superset of BrainFuck but it has not it's own runtime. It compiles to pure BrainFuck.

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

Forth (gforth)

36 base ! nine eight seven six five four three two one 0

Usage:

1 pick . ONE  ok
2 pick . TWO  ok
3 pick . THREE  ok
4 pick . FOUR  ok
5 pick . FIVE  ok
6 pick . SIX  ok
7 pick . SEVEN  ok
8 pick . EIGHT  ok
9 pick . NINE  ok

where ok is printed by the interpreter. In fact, I do not know how to count characters in this. One might even say that this is not a program.

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

Haskell : 89

do x<-readLn;print$[[],"one","two","three","four","five","six","seven","eight","nine"]!!x
\$\endgroup\$
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\$
2
  • \$\begingroup\$ Why not num2words(raw_input())? Or even input(), since it's guaranteed to be a number? \$\endgroup\$ Commented Jun 25, 2014 at 8:32
  • 1
    \$\begingroup\$ @undergroundmonorail Thanks. didn't knew about it. I'm not too good in python. \$\endgroup\$ Commented Jun 26, 2014 at 4:23
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\$
5
  • \$\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
    Commented Jun 23, 2014 at 12:40
  • 1
    \$\begingroup\$ Ups!. I'll change it. Thanks @german_guy \$\endgroup\$
    – rpax
    Commented Jun 23, 2014 at 13:21
  • 2
    \$\begingroup\$ @german_guy changed! \$\endgroup\$
    – rpax
    Commented Jun 23, 2014 at 13:29
  • \$\begingroup\$ @ProgramFOX I'm sorry I don't understand you. Now it is using only swipl. \$\endgroup\$
    – rpax
    Commented Jun 23, 2014 at 13:55
  • \$\begingroup\$ @rpax Ah, now I see. I thought you were only using bash now. I'm sorry. \$\endgroup\$
    – ProgramFOX
    Commented Jun 23, 2014 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\$
4
  • \$\begingroup\$ Nine // is just enough for /_/\/\// to save a byte. \$\endgroup\$ Commented Jul 21, 2016 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\$ Commented Jul 21, 2016 at 9:39
  • \$\begingroup\$ slashes.tryitonline.net/… \$\endgroup\$ Commented Jul 21, 2016 at 9:42
  • \$\begingroup\$ @MartinEnder So that is the reason I've had problems with forward slashes (/) in the past... \$\endgroup\$ Commented Jul 21, 2016 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\$
2
  • \$\begingroup\$ Does this actually output in japanese? \$\endgroup\$ Commented Jul 20, 2016 at 21:55
  • \$\begingroup\$ Yes. ideone.com test. @Eʀɪᴋ ᴛʜᴇ Gᴏʟғᴇʀ \$\endgroup\$
    – user56344
    Commented Jul 21, 2016 at 3:34
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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