51
\$\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
    Jun 21, 2014 at 5:13
  • 1
    \$\begingroup\$ @grc Yes, as long as the program outputs the word. \$\endgroup\$
    – Jon
    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
    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
    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\$ Jun 26, 2014 at 18:12

73 Answers 73

2
\$\begingroup\$

PHP, 93 73 68 bytes

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

Try it Online!

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Jun 22, 2014 at 11:03
2
\$\begingroup\$

Jelly, 35 bytes

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

Try it online!

Explanation:

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

APL, 54 bytes

⎕⊃↓9 5⍴'ONE  TWO  THREEFOUR FIVE SIX  SEVENEIGHTNINE '
\$\endgroup\$
1
\$\begingroup\$

Tcl, 61

Number entered as command line argument

lindex {- one two three four five six seven eight nine} $argv
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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()]
\$\endgroup\$
1
\$\begingroup\$

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);
\$\endgroup\$
1
1
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ you can eliminate the declaration of v which saves you 4 chars.. that is, p %w(...)[...] \$\endgroup\$ Jun 23, 2014 at 1:41
1
\$\begingroup\$

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$ 
$ 
\$\endgroup\$
1
  • \$\begingroup\$ 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). \$\endgroup\$ Jun 26, 2014 at 18:41
1
\$\begingroup\$

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.

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

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

\$\endgroup\$
1
\$\begingroup\$

PowerShell (71)

PS > "one,two,three,four,five,six,seven,eight,nine".split(",")[$(read-host)]

Works for valid inputs.

\$\endgroup\$
1
\$\begingroup\$

Javascript, 94 92 84 bytes

alert(['one','two','three','four','five','six', 'seven','eight','nine'][prompt()-1])
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Explicit cast is useless : alert(['one','two','three','four','five','six', 'seven','eight','nine'][prompt()-1]) \$\endgroup\$
    – Michael M.
    Jun 23, 2014 at 18:15
  • \$\begingroup\$ You can also abuse of split : alert('0one0two0three0four0five0six0seven0eight0nine'.split(0)[prompt()]) but that is the answer of @nderscore \$\endgroup\$
    – Michael M.
    Jun 23, 2014 at 18:16
1
\$\begingroup\$

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/
\$\endgroup\$
1
\$\begingroup\$

Powershell 63

(-split"X one two three four five six seven eight nine")[$args]

(The default delimiter in Powershell is whitespace)

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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.
\$\endgroup\$
4
  • \$\begingroup\$ Isn't it disallowed to use such languages? \$\endgroup\$ Jun 25, 2014 at 4:33
  • \$\begingroup\$ @reg Why would it be? \$\endgroup\$ Jun 25, 2014 at 8:31
  • \$\begingroup\$ For this reason meta.codegolf.stackexchange.com/a/1071/13171 \$\endgroup\$ Jun 26, 2014 at 4:22
  • \$\begingroup\$ @RegisteredUser: Sclipting is not designed for any particular challenge. It is, in fact, similar in goal to Golfscript. \$\endgroup\$
    – Timwi
    Jun 26, 2014 at 7:42
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
  • 2
    \$\begingroup\$ 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 [] \$\endgroup\$ Jun 26, 2014 at 19:59
1
\$\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\$
1
\$\begingroup\$

Haskell 75

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
\$\endgroup\$
1
\$\begingroup\$

C - 93 91 89 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 (?):

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);
}
\$\endgroup\$
1
  • \$\begingroup\$ Why not main(x){...} instead of x;main(){...} in the first program? \$\endgroup\$
    – Spikatrix
    May 24, 2015 at 14:28
1
\$\begingroup\$

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? :'(

\$\endgroup\$
6
  • \$\begingroup\$ You could do Console.Read() like I did in the Csharp version. \$\endgroup\$
    – rene
    Jun 21, 2014 at 15:39
  • 1
    \$\begingroup\$ @rene ah, didn't know that existed. Thanks! :) \$\endgroup\$
    – Jwosty
    Jun 21, 2014 at 15:39
  • \$\begingroup\$ 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 \$\endgroup\$
    – rene
    Jun 21, 2014 at 15:41
  • 1
    \$\begingroup\$ Wait, how does adding a single character make it 124 bytes from 107? \$\endgroup\$ Jun 22, 2014 at 22:20
  • 1
    \$\begingroup\$ @nyuszika7h oops, I forgot to update that number earlier. Fixed. \$\endgroup\$
    – Jwosty
    Jun 22, 2014 at 22:35
1
\$\begingroup\$

Wolfram, 11

There is a function for that of course: IntegerName

\$\endgroup\$
1
\$\begingroup\$

Factor, 11 bytes

a builtin: number>text.

\$\endgroup\$
1
\$\begingroup\$

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"
\$\endgroup\$
1
  • \$\begingroup\$ 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) \$\endgroup\$
    – cat
    Dec 18, 2016 at 2:56
1
\$\begingroup\$

Lexurgy, 79 bytes

a:
{\1,\2,\3,\4,\5,\6,\7,\8,\9}=>{one,two,three,four,five,six,seven,eight,nine}

Simple substitution.

\$\endgroup\$
1
\$\begingroup\$

Vyxal, 2 bytes

∆ċ

Try it Online!

Builtin.

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

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
\$\endgroup\$
5
  • \$\begingroup\$ 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. \$\endgroup\$ Jun 22, 2014 at 1:25
  • \$\begingroup\$ @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) \$\endgroup\$ Jun 22, 2014 at 2:08
  • \$\begingroup\$ 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 . \$\endgroup\$ Jun 22, 2014 at 2:11
  • \$\begingroup\$ @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? \$\endgroup\$ Jun 22, 2014 at 2:16
  • \$\begingroup\$ As long as I'm not held accountable for it (except in perhaps a good way) I love writing hacky code :P \$\endgroup\$ Jun 22, 2014 at 2:18
0
\$\begingroup\$

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
\$\endgroup\$

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.