# 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
• Is trailing whitespace (i.e. one ) acceptable? – grc Jun 21 '14 at 5:13
• @grc Yes, as long as the program outputs the word. – Jon Jun 21 '14 at 5:14
• you should specify that only the given number may be printed and not the other numbers. – Pinna_be Jun 21 '14 at 7:04
• @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. – Jon Jun 21 '14 at 7:06
• 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. – Nicolas Barbulesco Jun 26 '14 at 18:12

## Tcl, 61

Number entered as command line argument

lindex {- one two three four five six seven eight nine} $argv  # C++11 - 133 My answer in C++: #include<iostream> main(){char*n[]={"one","two","three","four","five","six","seven","eight","nine"};std::cout<<n[std::cin.get()-'1'];}  The total amount of bytes is 133. I know I'm not going to win but I just want to participe... C++11 172  #include <iostream> int main(){ auto a = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };printf("%s", *(a.begin() + getchar() - '0' - 1));}  Javascript - 79 - 69 68: (run on dev console) ["one", "two", "three", "four","five","six","seven","eight","nine"][prompt()-1] "one,two,three,four,five,six,seven,eight,nine".split(',')[prompt()-1] ",one,two,three,four,five,six,seven,eight,nine".split(',')[prompt()]  ## Objective-C - 154 bytes This one is kind of cheating a little bit because Objective-C has a built in class just for this but I don't see anything in the rules that says I can't do that so here it is! int i; scanf("%d",&i); NSNumberFormatter *f=[[NSNumberFormatter alloc] init]; [f setNumberStyle:5]; NSString *s=[f stringFromNumber:@(i)]; NSLog(@"%@",s);  Ruby 68 v=%w(one two three four five six seven eight nine);p v[gets.to_i-1]  Would love tips on how to get it down further! • you can eliminate the declaration of v which saves you 4 chars.. that is, p %w(...)[...] – Not that Charles Jun 23 '14 at 1:41 # Shell, 42 bytes cat$1


This relies on files in the current directory, one for each digit. The lengths of the files are included in the score:

ubuntu@ubuntu:~/n$ls -l total 40 -rw-rw-r-- 1 ubuntu ubuntu 3 Jun 22 11:39 1 -rw-rw-r-- 1 ubuntu ubuntu 3 Jun 22 11:40 2 -rw-rw-r-- 1 ubuntu ubuntu 5 Jun 22 11:40 3 -rw-rw-r-- 1 ubuntu ubuntu 4 Jun 22 11:40 4 -rw-rw-r-- 1 ubuntu ubuntu 4 Jun 22 11:41 5 -rw-rw-r-- 1 ubuntu ubuntu 3 Jun 22 11:41 6 -rw-rw-r-- 1 ubuntu ubuntu 5 Jun 22 11:41 7 -rw-rw-r-- 1 ubuntu ubuntu 5 Jun 22 11:41 8 -rw-rw-r-- 1 ubuntu ubuntu 4 Jun 22 11:41 9 -rwxrwxr-x 1 ubuntu ubuntu 6 Jun 22 11:42 n.sh ubuntu@ubuntu:~/n$


Example Output:

$./n.sh 4 four$
$./n.sh 7 seven$
$ • The names of the files are a key part of the solution. If we count the characters, then the names of the files have to be included. The total length of this solution is 51 (1one2two3three4four5five6six7seven8eight9ninecat$1). – Nicolas Barbulesco Jun 26 '14 at 18:41

# Racket / R5RS Scheme: 71 68

(vector-ref'#(z one two three four five six seven eight nine)(read))


It runs in the REPL of R6RS and R7RS with base library loaded too.

• You can save 3 characters in the Racket code by using 0 (or any other single character) instead of zero. – nyuszika7h Jun 22 '14 at 22:19
• @nyuszika7h Thanks. I originally though zero was needed and I forgot to remove it from the Racket version. – Sylwester Jun 22 '14 at 23:59
• @Charles It's done :) – Sylwester Jun 23 '14 at 8:28

## R, 67 characters

scan(t='one two three four five six seven eight nine',w='')[scan()]


First part creates a vector containing the names, second part subset it according to user input (indices are 1-based in R).

# PowerShell (71)

PS > "one,two,three,four,five,six,seven,eight,nine".split(",")[$(read-host)]  Works for valid inputs. ## Javascript, 9492 84 bytes alert(['one','two','three','four','five','six', 'seven','eight','nine'][prompt()-1])  • Explicit cast is useless : alert(['one','two','three','four','five','six', 'seven','eight','nine'][prompt()-1]) – Michael M. Jun 23 '14 at 18:15 • You can also abuse of split : alert('0one0two0three0four0five0six0seven0eight0nine'.split(0)[prompt()]) but that is the answer of @nderscore – Michael M. Jun 23 '14 at 18:16 # sed 90 It's really a trivial program (the brute force method); I wouldn't have bothered, except that it beats a surprising number of answers here. s/9/nine/ s/8/eight/ s/7/seven/ s/6/six/ s/5/five/ s/4/four/ s/3/three/ s/2/two/ s/1/one/  # Powershell 63 (-split"X one two three four five six seven eight nine")[$args]


(The default delimiter in Powershell is whitespace)

Python 3 + Japanese - 34 characters (52 bytes)

Not sure if this counts:

print('九八七六五四三二一'[-int(input())])


Numbers were taken from:

http://en.wikipedia.org/wiki/Japanese_numerals

# Sclipting (34)

껆뭮뉒롴덶묬덆둲뉖넬뉦뭵댢롦늗뉥껇꽩뎂롳뉗뉥닢롥늖덨덂롮늖멥壹坼⓶掘


This is very straight-forward. Remember the user input is first on the stack.

껆뭮뉒롴덶묬덆둲뉖넬뉦뭵댢롦늗뉥껇꽩뎂롳뉗뉥닢롥늖덨덂롮늖멥

• Push the string ",one,two,three,four,five,six,seven,eight,nine"

壹

• Retrieve the first character from that string (but leave the string on the stack)

坼

• Regular expression string-split. Result: ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

⓶

• Swap the top two elements (so the input is now on top)

掘

• Get nth element from the list, leaving only the result on the stack.
• Isn't it disallowed to use such languages? – Registered User Jun 25 '14 at 4:33
• @reg Why would it be? – undergroundmonorail Jun 25 '14 at 8:31
• For this reason meta.codegolf.stackexchange.com/a/1071/13171 – Registered User Jun 26 '14 at 4:22
• @RegisteredUser: Sclipting is not designed for any particular challenge. It is, in fact, similar in goal to Golfscript. – Timwi Jun 26 '14 at 7:42

## Apple Swift : 143

Not a winner, but my first Code-Golf, so hello everyone!

for i in 1..C_ARGC{print(["zero","one","two","three","four","five","six","seven","eight","nine"][String.fromCString(C_ARGV[Int(i)]).toInt()!])}


What I like about this is that the lookup table is defined in place in print so I could shave a few chars by removing the variable definition for it.

Accepts any number of input arguments, provided that they are separeted with spaces.

In Java (7 and newer) ; 116 chars

public class A{public static void main(String[] a){System.out.print(Character.getName(48+Integer.parseInt(a[0])));}}


This program takes the input number as command-line argument, and outputs the letters in the console.

Example :

• Input : 4
• Output : DIGIT FOUR
• I think the class doesn't need to be public; also, why not just call getName(a[0].charAt(0))? And you don't need a space after [] – aditsu Jun 26 '14 at 19:59

I know I'm a little late to the party, but thought I might as well:

fmap(words"_ One Two Three Four Five Six Seven Eight Nine"!!)readLn>>=print


# C - 939189 88 bytes (thanks Cool Guy)

main(x){scanf("%d",&x);write(1,5*--x+"one  two  threefour five six  seveneightnine",5);}


Old version with arguments instead of scanf:

main(int c,char**a){write(1,"one  two  threefour five six  seveneightnine"+*a[1]*5-245,5);}


# Cryptic version (code-trolling?):

This one is somewhat longer but at least no space is wasted on spaces (only tabs and newlines ... and one space in function header).

main(int c,char**a){
*a="039018342675onetwosixfourfivenineseveneightthree";
c=*(*a+*a[1]-46)-39;
write(1,c/3*(c-9)-*(*a+c/3-3)+60+*a,c/3);
}


Magic sequence 018342675 can be calculated as x+=6*!(x%3)-1;x%=9; but in the end I decided to use strings for everything.

Yet another version (different arrangement of numbers, easier to calculate position):

main(int c,char**a){
*a="038146257onefourseventwofiveeightsixninethree";
c=*(*a+*a[1]-49)-45;
write(1,*a+c*4+!(c%3)-4,3+c%3);
}

• Why not main(x){...} instead of x;main(){...} in the first program? – Spikatrix May 24 '15 at 14:28

# F# - 101

printf"%s"["one";"two";"three";"four";"five";"six";"seven";"eight";"nine"].[System.Console.Read()-49]


For a newline but 108 bytes, change printf to printfn.

It's a naïve solution that just fetches the given index of that hardcoded list. I would have loved to use base64 encoding and/or a regex to split a string into an array (so the elements would be separated with instead of ";"), but both of those would just make the solution longer, due to namespaces. Why can't they just be in System like everything else? :'(

• You could do Console.Read() like I did in the Csharp version. – rene Jun 21 '14 at 15:39
• @rene ah, didn't know that existed. Thanks! :) – Jwosty Jun 21 '14 at 15:39
• You don't need the parse...msdn.microsoft.com/en-us/library/… you get an int as return value for the ascii code of the char – rene Jun 21 '14 at 15:41
• Wait, how does adding a single character make it 124 bytes from 107? – nyuszika7h Jun 22 '14 at 22:20
• @nyuszika7h oops, I forgot to update that number earlier. Fixed. – Jwosty Jun 22 '14 at 22:35

# Wolfram, 11

There is a function for that of course: IntegerName

## Racket 159 bytes

(string-join(map(λ(x)(list-ref(list"zero""one""two""three""four""five""six""seven""eight""nine")(string->number(string x))))(string->list(number->string n))))


Ungolfed:

(define(f n)
(string-join
(map (λ (x)
(list-ref [list "zero" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine"]
(string->number(string x))))
(string->list (number->string n)))))


Testing:

(f 10357)


Output:

"one zero three five seven"


17 bytes can be saved if number is sent enclosed in double quotes:

(define(f n)
(string-join
(map (λ (x)
(list-ref [list "zero" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine"]
(string->number(string x))))
(string->list n))))

(f "10357")
"one zero three five seven"

• It only has to work for 1-9 but nice answer! This could be extended to write out real english (10357 -> ten thousand three hundred fifty seven) – cat Dec 18 '16 at 2:56

# PHP, 9373 68 bytes

<?=explode(' ','one two three four five six seven eight nine')[1-1];


Try it Online!

• This is short-circuiting the input method. The other solutions — including mine, to come — would be shorter too if they placed the “input” directly at the target spot. – Nicolas Barbulesco Jun 22 '14 at 11:03

# Jelly, 35 bytes

ị“¡5ç“¡Ḃḥ“¡Ị¿“Ɱ9“ƁẆ“¡⁹)“¡°Ṗ“z⁷“¡'Ṡ»


Try it online!

Explanation:

 “¡5ç“¡Ḃḥ“¡Ị¿“Ɱ9“ƁẆ“¡⁹)“¡°Ṗ“z⁷“¡'Ṡ» List of numbers 1-9 as words.
ị                                   xth element of y.


POSIX SHELL: 68

Similar to Eric's Bash, without the bashisms (using set to mimick an array using $@) and eval to handle indirection (in bash this could just be echo${!A} without the eval)

A=$1 set one two three four five six seven eight nine eval echo $A  • In bash you can just use X instead of echo {!X} as long as you use set :) If you use set, the curly braces actually cause nothing to happen while returning code 0. – Eric Lagergren Jun 22 '14 at 1:25 • @eric_lagergren - this is a proper posix implementation though -no bashisms allowed. Even in bash though the ! is needed for variable indirection to convert the 1 stored in A into a 1 variable. Just eval-ing A will try run 1, eval-ing$$A will try to run "one" ... the echo is just to print without a "command not found", just because I consider it bad form. (hopefully that's all - at least I don't know of any malicious binaries with spelled out number names) – technosaurus Jun 22 '14 at 2:08
• The ! isn't needed i.imgur.com/tnivt32.png ... or at least, for whatever reason, I didn't need it. Adding echo removes the bash: xxxx: command not found but adds 4 . – Eric Lagergren Jun 22 '14 at 2:11
• @eric_lagergren - Yeah, that works ... in interactive mode only. Now put it in an actual script. ... can't add it to an rc file because it clobbers your args. Isn't working without real bash arrays fun? – technosaurus Jun 22 '14 at 2:16
• As long as I'm not held accountable for it (except in perhaps a good way) I love writing hacky code :P – Eric Lagergren Jun 22 '14 at 2:18

# Rebol - 58

pick[one two three four five six seven eight nine]do input


Example from Rebol console:

>> pick[one two three four five six seven eight nine]do input
9
== nine


$><<%w[a one two three four five six seven eight nine][gets.to_i]  # 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 # 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. 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. ## Haskell : 89 do x<-readLn;print$[[],"one","two","three","four","five","six","seven","eight","nine"]!!x


## 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).