Convert 1, 2, 3, 4, 5, 6, 7, 8, and 9 to "one", "two", "three", etc

47

6

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

Jon

Posted 2014-06-21T04:38:45.260

Reputation: 1 505

The "winning" answer violates a standard loophole - no relying on external sources. bsdgames is not included with bash most of the time. – noɥʇʎԀʎzɐɹƆ – 2016-07-20T18:35:23.733

2Is trailing whitespace (i.e. one) acceptable? – grc – 2014-06-21T05:13:57.753

1@grc Yes, as long as the program outputs the word. – Jon – 2014-06-21T05:14:21.180

The user must type the number at some point. 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. – Jon – 2014-06-21T05:18:52.907

In languages like Julia, you can input variables at the prompt like this: n=5 - can we use this as our input method, and count the n= part as 2 characters in our code, and then use n in the rest of the code? – Glen O – 2014-06-21T06:43:13.790

You can do whatever you want as long as the user inputs the character 5 at some point (have them enter it, append it yourself, etc) – Jon – 2014-06-21T06:45:41.420

1you should specify that only the given number may be printed and not the other numbers. – Pinna_be – 2014-06-21T07:04:44.170

1@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 – 2014-06-21T07:06:59.010

So we can use multiple files and the final byte count is just the sum of the files' byte counts? – Dennis – 2014-06-21T15:27:41.570

Do spoken words count? – CousinCocaine – 2014-06-23T11:14:55.510

Can the output be as a file? – Οurous – 2014-06-23T11:25:54.210

1This 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 – 2014-06-26T18:12:54.620

1

If http://codegolf.stackexchange.com/questions/32151/convert-1-2-3-4-5-6-7-8-and-9-to-one-two-three-etc/32493#32493 answer is accepted, why isn't mine which is exactly the same, only shorter? http://codegolf.stackexchange.com/a/32298/15168

– CousinCocaine – 2014-06-27T12:41:01.613

Because you edited your answer to make it valid after the deadline. As of the deadline you had an invalid submission. – Jon – 2014-06-27T18:18:29.230

@CousinCocaine — Spoken words are words. ;-) In fact, spoken words existed long before written words. – Nicolas Barbulesco – 2014-06-28T18:07:27.497

Chipperyman, Except that the answer you declared winner ex æquo is invalid too, for the same reason as you rejected the answer by @CousinCocaine. – Nicolas Barbulesco – 2014-06-28T18:11:44.253

I never said spoken words aren't words, however as of the contest ending, it only worked if the input was 4. And Nicolas, can you link me to the rejected answer? I might have just not noticed it if it is <= 9 chars. – Jon – 2014-06-28T18:15:06.527

Chipperyman has declared the answer by Registered User winner ex æquo. But this answer misses taking the input. The other solutions — including mines — would be shorter too if they placed the “input” directly at the target spot. Besides, Chipperyman has rejected the answer by CousinCocaine because it missed taking the input. In addition, the answer by Registered User is a replica of CousinCocaine's answer. This is unfair — to say the least.

– Nicolas Barbulesco – 2014-06-28T18:21:48.873

1Oh, he told me that it worked for any number and I didn't know the language so I figured I was missing something. I guess he didn't win. – Jon – 2014-06-28T18:22:57.423

What is labelled “original post” here is not the original post.

– Nicolas Barbulesco – 2014-06-28T18:32:32.660

I feel like you're just trying to argue with me at this point. I (in my opinion) obviously meant the post before the winner was announced. Does it really matter that it's not the true original post? – Jon – 2014-06-28T18:40:04.347

"Original post kept for prosperity": I think you meant posterity (future generations), not prosperity (getting rich). – NinjaBearMonkey – 2014-07-27T15:12:34.657

Answers

19

Bash (with bsdgames), 9

number -l

Reads from standard input.

I don't know why there's a utility for this, but whatever.

user16402

Posted 2014-06-21T04:38:45.260

Reputation:

2I'm not sure I like installing bsdgames and then calling it "bash", though I'll admit that my reservation is subjective. – Paul Draper – 2014-06-30T04:43:31.743

@PaulDraper Neither do I. I didn't post this as a serious answer, I think one of the other answers deserved to be accepted – None – 2014-06-30T08:21:03.410

5Ain't this a standard loophole? – Justin – 2014-07-02T18:26:06.973

2@Quincunx yes, it basically is. Although most things are external utilities in Bash, I wouldn't count the bsdgames package as something very permissible (unlike grep, wget, coreutils, sed, bc...). – None – 2014-07-03T20:02:53.963

133

Python 2, 64

print' ottffssennwhoiieiieoruvxvgn  ere ehe  e   nt'[input()::9]

This is what the string looks like with some extra whitespace (try reading vertically):

o t t f f s s e n
n w h o i i e i i
e o r u v x v g n
    e r e   e h e 
    e       n t

As explained in the comments below, [input()::9] starts at the given index and selects every ninth subsequent character.

grc

Posted 2014-06-21T04:38:45.260

Reputation: 18 565

10Oh, shit, I like this. Probably worth pointing out that this is python 2, by the way. – undergroundmonorail – 2014-06-21T07:33:05.080

3Explain please! :) – Christofer Ohlsson – 2014-06-21T10:12:16.787

6@ChristoferOlsson input()::9 is a range from input() to the end of the string in steps of 9, like 1,10,19,28,37 or 3,12,21,30,39, which are exactly the indices at which the letters for the corresponding word are found. – Martin Ender – 2014-06-21T10:14:38.970

1@m.buettner thanks! That's nice. – Christofer Ohlsson – 2014-06-21T10:18:56.227

Python 3 would only be 7 characters longer right? The brackets for print() and adding int() around the input(). Also would be safer than using Python 2's input. – JeromeJ – 2014-06-21T11:01:24.553

@JeromeJ yes, that's correct. – grc – 2014-06-21T11:32:02.660

6Isn't this something like what a rail-fence cipher does? (+1 for a neat solution by the way.) – Lou – 2014-06-21T16:09:21.117

Visual, clever. I like this one ! – Nicolas Barbulesco – 2014-06-22T08:20:28.900

18

Common Lisp - 22 bytes

Here's a general one (not just for one-ten):

(format nil"~R"(read))

Oh, you want ordinals (first, second, ... three-hundredth...)? Ok!

(format nil"~:R"(read))

filcab

Posted 2014-06-21T04:38:45.260

Reputation: 305

10Isn't this a standard loophole? – seequ – 2014-06-21T22:45:25.483

2

Loophole in what sense? It's clearly defined in Hyperspec: http://www.lispworks.com/documentation/lw50/CLHS/Body/22_cba.htm

– filcab – 2014-06-21T22:48:20.840

10

As in this: http://meta.codegolf.stackexchange.com/a/1078/20356

– seequ – 2014-06-21T23:09:48.027

3Whether it's a standard loophole or not, it's what I came here to post, too. +1 :) – Joshua Taylor – 2014-06-21T23:16:50.400

1@TheRare I'll remember for the next time. Although I don't quite agree with it. – filcab – 2014-06-22T01:02:45.307

5@TheRare Looking at the comments to that answer, I don't think there is strong community consensus that this is a loophole. My feeling is that questions need to explicitly ban use of this kind of built in function if they are not to be used. – Digital Trauma – 2014-06-22T02:52:17.990

1@DigitalTrauma That is the reason I asked if it's one. Might be that my meaning got a little warped. – seequ – 2014-06-22T09:40:27.993

This seems more like an answer in SO than in PCG to me... – Saffron – 2014-06-23T16:09:42.973

16

BASH 51

So I made another one using my second idea plus some help from others:

set one two three four five six seven eight nine

$X

Where 'X' is the number you want.


BASH 48 (67)

67 with \n line breaks

I'm not sure if this totally counts, because it's reading from a file, but:

sed -n Xp a

Where "X" is the input and where a is a file with:

one
two
three
four
five
six
seven
eight
nine

The file is 36 chars, and the bash command is 13.

How it works:

Each line in the file is numbered, starting with 1. So 'one' is on the 1st line, 'two' on the 2nd. The command, sed -n 'Xp' a says, "Please print what's listed on line 'X' of file 'a'" sed is a Unix stream editor. -n means be silent, or only essentially ignore everything else. Xp means print what's on line 'X'.

Eric Lagergren

Posted 2014-06-21T04:38:45.260

Reputation: 473

@Quincunx updated my post from 46 -> 49 – Eric Lagergren – 2014-06-21T06:43:13.863

@Quincunx I added a spoiler. Hopefully I explained it well. – Eric Lagergren – 2014-06-21T06:52:31.737

2Nice first answer! Thank you for taking the time to figure out how we format our answers on this site. Welcome to Programming Puzzles and Code Golf! – Justin – 2014-06-21T07:04:16.890

For the record, this does fit within the rules because you could reasonably assume the text file had a number input by the user. – Jon – 2014-06-21T07:10:17.167

2You didn't count the linefeeds in a. – Dennis – 2014-06-21T07:24:53.753

1

One byte less than before, accepts a command-line argument and doesn't require additional files: http://pastebin.com/qdbXKQGP Bash will complain about the missing heredoc delimiter, but it will run fine with Dash and Zsh.

– Dennis – 2014-06-21T15:09:10.483

1@Dennis oh cool I like it. Am I supposed to edit my answer with yours now? Sorry, am new to code golf. – Eric Lagergren – 2014-06-21T16:49:31.827

That depends entirely on you. – Dennis – 2014-06-21T17:09:14.367

1You can omit the ' quotes. You could also try using a here string instead of the external file. But you really should count the line breaks in your file. – Digital Trauma – 2014-06-21T17:29:29.597

You might be able to use the d command within sed instead of -n – None – 2014-06-21T19:57:54.353

1@Dennis I'll add yours in when I get back to my laptop. I'm on my phone at the moment and it's hard to edit. – Eric Lagergren – 2014-06-21T21:13:06.153

It certainly is. By the way, your new approach is very similar to professorfish's. Yours has a syntax error; Bash uses () for array declaration, not [].

– Dennis – 2014-06-21T21:18:13.857

1@Dennis you're right. I fixed it. Stupid error because I wrote too much JS. – Eric Lagergren – 2014-06-21T21:19:12.153

You're also missing the echo. – Dennis – 2014-06-21T21:19:49.277

1@Dennis technically it prints the correct number, plus a bunch of other junk. If you only wanted the number you'd use echo, yes. – Eric Lagergren – 2014-06-21T21:21:31.570

1So that was by design. Sneaky! It's customary to count functional whitespace, so one linefeed between the first and the second command has to be counted. That gives a total of 58 bytes. I'd also change X to $1 to comply with the question's input requirements. – Dennis – 2014-06-21T21:29:23.403

I'm not sure I under stand the "change x" part @Dennis – Eric Lagergren – 2014-06-21T21:34:58.747

I won't pretend that I fully understand the input requirements of this question, but the usual forms of accepting user input with Bash scripts/functions are reading from STDIN and command line arguments. $1 refers to the first argument or the script/function, so it's the cheaper choice. – Dennis – 2014-06-21T21:44:51.927

set one two ... nine then user input is ${!X} to save 4 bytes – technosaurus – 2014-06-21T23:15:08.193

1@Dennis Oh, okay. I see what you mean. I updated it using set and $X – Eric Lagergren – 2014-06-22T01:19:31.510

Oh, and I should mention thanks for the warm welcome, @Quincunx Also, are line breaks 1 or two bytes? – Eric Lagergren – 2014-06-22T20:10:07.697

1They count as one byte. – nyuszika7h – 2014-06-22T22:06:40.890

59 bytes: sed -n $1p<<<'one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine' (replace \ns with line breaks). Takes a number as command line argument. – nyuszika7h – 2014-06-22T22:08:23.710

The first solution, with $1, misses the input. – Nicolas Barbulesco – 2014-06-26T17:36:08.540

What shell are you using @Nicolas Barbulesco ? – Eric Lagergren – 2014-06-26T17:37:12.687

@eric_lagergren — I tested this with bash on Mac OS X. This does not work. I did test your code, $X. The answer has to take the digit as input. I don't have to edit the script to place the digit inside the code. – Nicolas Barbulesco – 2014-06-26T17:42:41.430

@NicolasBarbulesco the question doesn't ask for the input to be inside the code. It just says 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 – Eric Lagergren – 2014-06-26T17:57:57.417

16

C# - 127 ( 86 / 46)

If you only take the executable part...

Console.Write(((HourOfDay)Console.Read()-48));

and if HourOfDay would have been part of the the System namespace you would need 46 chars. Unfortunately it sits in System.DirectoryServices.ActiveDirectory which makes it 86...the other noise spoils it.

This is compileable and runnable from the commandline (127 chars):

using System;class P{static void Main(){Console.Write((System.DirectoryServices.ActiveDirectory.HourOfDay)Console.Read()-48);}}

if saved to a file called cg2.cs

csc cg2.cs /r:System.DirectoryServices.dll  && cg2

How does this work?

HourOfDay is an enum type so we can use names instead of magic constants. Enum types have a ToString() implementation that gives you the name of the value. You can cast an int to an enum. Console.Read() reads a character from the input stream represented as an integer. typing '1' gives 49, substract 48 to get 1, cast/box to HourOfDay to return 'One'.

Take a look at the Powershell version of this same trick

rene

Posted 2014-06-21T04:38:45.260

Reputation: 301

How does this work ? Let's say I enter 9. What does the program do then ? – Nicolas Barbulesco – 2014-06-22T10:00:21.923

What does this -48 do here ? – Nicolas Barbulesco – 2014-06-22T10:09:09.143

2@NicolasBarbulesco added an How does this work? paragraph. Does it make sense? – rene – 2014-06-22T10:56:28.617

Rene, thank you, it is clearer now. The -48 is there to convert character code to integer. – Nicolas Barbulesco – 2014-06-22T16:11:27.257

1@NicolasBarbulesco - It's sorted alphabetically. Which makes sense in every context except "These words are numbers". Since I'm sure the documentation is automatically generated, it wouldn't make sense to special case the one article. – Bobson – 2014-06-23T18:35:41.940

In reality, the cast to HourOfDay occurs before the subtraction (cast operator has higher precedence than -), but to my surprise C# still allows that. – Timwi – 2014-06-24T17:26:19.427

@Timwi Hmmm, The IL doesn't show a cast OpCode but only a box OpCode just before the call to write to get an object?! – rene – 2014-06-24T18:37:44.443

@Bobson — The alphabetical order is non-sense. The days in the week are friday, monday, saturday, sunday, thursday, tuesday, wednesday. :-) – Nicolas Barbulesco – 2014-06-24T22:42:15.080

@NicolasBarbulesco - Yep. Do note, though, that the order they are listed in is not the order of their int values. "The value of the constants in this enumeration ranges from DayOfWeek.Sunday to DayOfWeek.Saturday. If cast to an integer, its value ranges from zero (which indicates DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday)."

– Bobson – 2014-06-25T10:10:27.253

13

Befunge 98, 116 105 103 bytes

&1-       v
vvvvvvvvvj<
"""""""""
etnxereoe
nheivuewn
igvsiorto
nie"ffh""
"es ""t
 ""   "
>>>>>>>>>4k,@

Befunge was not made for this...

Explanation:

&1-           ;Grab the input number (`n`) and subtract 1 from it
&1-       v   ;Start moving downwards
          <   ;Start moving leftwards
         j    ;Jump `n - 1` characters to the left.
vvvvvvvvvj    ;Redirect each of the possible locations to jump to, sending the IP down.

        "     ;If `n` was 1, push `o, n, e` onto the stack (`o` is at the bottom)
        e
        n
        o
        "

       "      ;If `n` was 2, push `t, w, o` onto the stack
       o
       w
       t
       "

    *
    *
    *

"             ;If `n` was 9, push `n, i, n, e` onto the stack
e
n
i
n
"

>>>>>>>>>     ;Collect all the different possible positions the IP could be in, and send it right
>>>>>>>>>4k,  ;Print the top 5 chars. If there are less than 5, the rest are null characters. This is allowed
            @ ;End of program

Justin

Posted 2014-06-21T04:38:45.260

Reputation: 19 757

8@Dennis I can't see how that could be so hard. Simply take the file, turn it into a char[][], keep an x and y location, and execute the character at that location. The hardest thing would be defining the operation for every possible character. – Justin – 2014-06-21T06:07:47.560

1You can easily save two bytes with a &1-. – har-wradim – 2014-06-21T19:42:44.843

10

Javascript 73

alert('0one0two0three0four0five0six0seven0eight0nine'.split(0)[prompt()])

69

alert(btoa("ôéÞõ5õ1ëxO_¢êý|Þöȱõ'xß^hSôا{Ý").split(9)[prompt()])

nderscore

Posted 2014-06-21T04:38:45.260

Reputation: 4 912

Technically wouldn't alert('0one0two0three0four0five0six0seven0eight0nine'.split(0)[#]) be the same thing but save you 8 bytes? The user still inputs the number they want... – Eric Lagergren – 2014-06-22T19:32:02.847

@eric_lagergren do you mean the user would have to change the number in the source? I don't think that would count as input. – nderscore – 2014-06-22T23:45:44.167

Oh, okay, I see what you mean. I suppose if you ran it like a command in the console it'd be part of the input as opposed to the source. – Eric Lagergren – 2014-06-22T23:52:53.017

10

Javascript 68

Atob / btoa can be a poor's man compressing tool (but if you want to try this in console, you cannot copy from the formatted text you see at once. Go to 'edit' and copy from the source panel)

Markdown editor does not like some of the characters in this answer: some characters get lost at saving. Still, I think it's an editor problem, not mine. The lost characters are perfectly valid 8 bit unicode chars.
(Or else I'm wrong, if this issue was already debated in meta, let me know) Here is the version with offending characters escaped, each sequence \xNN should count 1

alert(btoa("×C§{Dð£Dá­ç´\x16\x8b«ÐX¯{D¢ÇD\x9e½éô\x12(!·Cb\x9dí").split(0)[prompt()])

Simple Test

In firefox console:

[0,1,2,3,4,5,6,7,8,9]
.map(x=>x +',' btoa("×C§{Dð£Dá­ç´\x16\x8b«ÐX¯{D¢ÇD\x9e½éô\x12(!·Cb\x9dí").split(0)[x])

edc65

Posted 2014-06-21T04:38:45.260

Reputation: 31 086

I tried something similar but couldn't figure out a valid base64 string starting with a number. I think you answer has been corrupted by stackexchange though. – nderscore – 2014-06-21T16:40:23.793

@nderscore, damn your'right. Amending the post, still it's valid according to me – edc65 – 2014-06-21T16:43:57.043

1With the version that has the escaped characters, everything works but 3 and 8, the output Tree and EightF. – Justin – 2014-06-21T16:54:06.370

3@Quincunx 'Tree' is me not speak english – edc65 – 2014-06-21T17:14:28.367

7

Perl, 55

$_=(nine,eight,seven,six,five,four,three,two,one)[-$_]

It is run with option -p (+1 byte), e.g.:

perl -pe '$_=(nine,eight,seven,six,five,four,three,two,one)[-$_]'

Input is expected in STDIN and output is written to STDOUT.

The code just generates an array and selects the right element. Option -p takes care of reading the input line into $_ and prints the result in $_.

Variations:

  • With trailing new line (+3):

    $_=(nine,eight,seven,six,five,four,three,two,one)[-$_].$/
    

    or (same byte count):

    s/./(nine,eight,seven,six,five,four,three,two,one)[-$&]/e
    

Update:

  • Using bare words instead of qw[...] (thanks Zaid).

  • Negative index saves a byte (thanks aragaer).

Heiko Oberdiek

Posted 2014-06-21T04:38:45.260

Reputation: 3 841

1Save a couple of characters: $_=(one,two,three,four,five,six,seven,eight,nine)[$_-1] – Zaid – 2014-06-21T18:30:38.017

2@Zaid: Thanks, it saved two bytes. (I am to used to use strict). – Heiko Oberdiek – 2014-06-21T19:10:23.793

I don't like that "-1". $_=(nine,eight,seven,six,five,four,three,two,one)[-$_] - one character shorter. – aragaer – 2014-06-23T19:26:50.840

@aragaer: Thanks, nice idea. – Heiko Oberdiek – 2014-06-23T19:39:34.363

6

CJam, 45 43 bytes

"^AM-^L8M-xM-^L^US^_M-^WrM-rM- 1M-s^CHM-|M-X^HE,M-qM-^EM-q4"256bKb'ef+'j/li=

The above uses ^ and M- notation, since some characters are unprintable.

At the cost of 9 more bytes, unprintable characters can be avoided:

" one two three four five six seven eight nine"S/li=

Try it online.

How it works

" Convert the string into an integer by considering it a base-256 number.                ";

"^AM-^L8M-xM-^L^US^_M-^WrM-rM- 1M-s^CHM-|M-X^HE,M-qM-^EM-q4"256b

" Convert the integer from above into an array by considering it a base-20 number.       ";

Kb

" Add the ASCII character code of “e” to all elements of that array. This casts to char. ";

'ef+

" So far, we've pushed the string “jonejtwojthreejfourjfivejsixjsevenjeightjnine”.       ";

" Split the the above string where the character “j” occurs.                             ";

'j/

" Read an integer from STDIN and push the corresponding substring.                       ";

li=

Example

$ base64 -d > convert.cjam <<< IgGMOPiMFVMfl3LyoDHzA0j82AhFLPGF8TQiMjU2YktiJ2VmKydqL2xpPQ==
$ wc -c convert.cjam
43 convert.cjam
LANG=en_US cjam convert.cjam <<< 5
five

Dennis

Posted 2014-06-21T04:38:45.260

Reputation: 196 637

Wow, our answers are identical, even in length (I assume the user inputs a string). – Justin – 2014-06-21T05:06:58.290

1@Quincunx: Alike minds think great! :P But reading user input will cost two bytes in CJam, so I'll have to think of something else... – Dennis – 2014-06-21T05:20:19.253

@Quincunx: It should work with GolfScript as well, although it probably won't save bytes. Base64 dump: IgGMOPiMFVMfl3LyoDHzA0j82AhFLPGF8TQiMjU2YmFzZSAyMGJhc2V7MTAxK30lIiIr – Dennis – 2014-06-21T06:53:52.303

Do you know what encoding golfscript expects for non-ascii characters? It seems to not be utf-8 – Justin – 2014-06-21T07:26:22.647

@Quincunx: The official interpreter depends on $LANG. I don't know that the online interpreter does. – Dennis – 2014-06-21T07:31:44.633

Okay. I give up. I managed to write something, but it doesn't work. At least I learned how to do file-io in python, though. – Justin – 2014-06-21T07:44:55.253

I haven't checked, but I don't think the :c is necessary, since int + char should give char. – aditsu quit because SE is EVIL – 2014-06-21T19:22:15.417

6

Oracle SQL - 46

select to_char(to_date(&1,'j'),'jsp')from dual

Demonstration

This does include a standard loophole, I admit, but the SQL is shorter than Golfscript; I couldn't resist!

It works by (ab)using Oracle's datetime format models. TO_DATE(n, 'j') converts a number into a Julian day, the number of days since January 1, 4712 BC. TO_CHAR(<date>, 'jsp') converts this back into the integer (though as a string). The sp, is a format element suffix that spells the number. This'll actually work with quite a lot of numbers.

The &1 is a substitution variable that'll only work with clients that accept it, for instance SQL*Plus.

Ben

Posted 2014-06-21T04:38:45.260

Reputation: 198

I have a love/hate relationship with Oracle's RDBMS but this is nice. – rene – 2014-06-24T09:07:39.870

5

GolfScript, 51 bytes

~'
one
two
three
four
five
six
seven
eight
nine'n/=

It's a simple lookup table. The input is evaluated (~), an array of the values is created, and the index is found.

Try it here

Justin

Posted 2014-06-21T04:38:45.260

Reputation: 19 757

The user must input a string, you can't have it hard-coded. Also, your Try it here link has different code than the pasted code. – Jon – 2014-06-21T05:12:20.443

@Chipperyman The user does input a string in this program. The Try it here link's difference in code is because the webpage does not support user input. Change the string after the ;, this is identical to user input. – Justin – 2014-06-21T05:15:05.193

Ah, I see. I don't know golfscript very well, thanks for letting me know. – Jon – 2014-06-21T05:15:46.617

2@Chipperyman: GolfScript places the contents of STDIN on the stack before executing the script itself. – Dennis – 2014-06-21T05:18:34.403

2

Hah! I can beat you by one byte! ;-)

– Ilmari Karonen – 2014-06-25T10:09:05.930

4

Perl, 60 bytes

$_=(ZOneTwoThreeFourFiveSixSevenEightNine=~/.[a-z]*/g)[$_]

Requires the -p switch (two bytes).

Example

$ perl -p convert.pl <<< 5
Five

How it works

  • -p reads from STDIN and saves the result in $_.

  • =~/.[a-z]*/g splits the preceding bareword into substrings of one (uppercase) letter followed by any number of lowercase letters.

  • (…) collects the substrings into an array.

  • [$_] retrieves the substring corresponding to the user input.

  • $_=… saves the result in $_.

  • -p prints the value of $_.

Dennis

Posted 2014-06-21T04:38:45.260

Reputation: 196 637

2I like the regex trick. +1 – seequ – 2014-06-21T15:56:55.257

3

Python 2.x - 65 64

Not as good as @grc 's answer, but certainly more legible :-)

'one two three four five six seven eight nine'.split()[input()-1]

One less char, thanks to @flornquake

'nine eight seven six five four three two one'.split()[-input()]

Willem

Posted 2014-06-21T04:38:45.260

Reputation: 1 528

This one is nice too. :-) It works. – Nicolas Barbulesco – 2014-06-22T09:55:21.173

3You can save one character like this: 'nine eight seven six five four three two one'.split()[-input()] – flornquake – 2014-06-22T23:01:08.307

Thats a Nice one ! – Willem – 2014-06-23T17:18:59.660

3

DOS Batch - 162 Chars (incl' line breaks)

This answer was inspired by @grc's Python answer, although I did have something similar in mind.

@setlocal enabledelayedexpansion
@set s="ottffssennwhoiieiieoruvxvgn  ere ehe  e   nt
@set /a c=%1
@set z=!s:~%c%,1!
@if _%z%==_ exit /b
@echo %z%
@%0 %c%+9

Usage:

[Filename] [number]

For example, if the code is in a file called speak.bat, and you want to see the number "five", you would run it as:

speak 5

Also, the output is top-to-bottom, not left-to-right! So instead of

five

you will see

f
i
v
e

Ruslan

Posted 2014-06-21T04:38:45.260

Reputation: 280

3

(pure) Bash, 64

Takes input as its first argument, assuming valid input.

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

Creates an array v, then accesses the element specified on the input. Since arrays are zero-indexed, I had to add a 0th element as a placeholder. Alternatively (thnx @DennisWilliamson for pointing this out):

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

user16402

Posted 2014-06-21T04:38:45.260

Reputation:

2Same character count: v=(one two three four five six seven eight nine) and echo ${v[$1-1]} – Paused until further notice. – 2014-06-22T04:52:07.613

3

Bash + coreutils, 64

Non-competitive compression concept

xxd -p<<<TàPnàõ:àsÀ~®@ãCN|tr 0-9a-d \\ng-inor-x|sed -n $1p

Some of the exotic characters here may not render well, so this script may be reconstructed from its base64 representation:

base64 -d <<< eHhkIC1wPDw8VOCLUIJu4PWWDzrgc8B+rkDjEoBDTnx0ciAwLTlhLWQgXFxuZy1pbm9yLXh8c2VkIC1uICQxcA==

Example output

$ ./n.sh 1
one
$ ./n.sh 9
nine
$ 

Explanation

It occurred to me that the string one two three four five six seven eight nine contains only the letters efghinorstuvwx and a space separator - 15 character values in total. Thus each character can potentially be represented in 4 bits, or 2 characters per 1 byte. We can use the hex representation of a byte as an easy way to split each byte into two hex digits. We can then transform the hex digits back to the letters we require using tr or similar. As luck would have it, rstuvwx are consecutive, so may be expressed as r-x to tr. The encoding is arranged such that e and f are left as-is, and that the words are line-break separated, so we can use sed to ouptut just the line we need.

This decoding process ends up using a fair amount of extra space, so makes this answer non-competitive as a shell-script-style answer, but may be a useful concept in other languages.

Digital Trauma

Posted 2014-06-21T04:38:45.260

Reputation: 64 644

3

bash say (OS X): 3 8

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

Last time I checked, spoken words are words as well. Previous attempt (accepts no input)

say 4

Edit, must be able to read input:

say|read

Type any number and the word comes out. I know the end date is due, but in my opinion I should have won the contest.

Example audio file: four.aiff

CousinCocaine

Posted 2014-06-21T04:38:45.260

Reputation: 1 572

shouldn't be 5? – rpax – 2014-06-23T14:13:55.533

@rpax I don't believe the input is counted as a character, is it? – CousinCocaine – 2014-06-24T12:58:18.843

Thinking again...no. +1 – rpax – 2014-06-24T13:41:23.463

1This is clever. But this misses the input. – Nicolas Barbulesco – 2014-06-26T17:45:56.670

1Unfortunately this is not a valid answer because: It never accepts input from the user AND it only works if the number is 4. – Jon – 2014-06-27T07:10:15.277

CousinCocaine, you should say something how to run this answer : in the Terminal. – Nicolas Barbulesco – 2014-06-28T13:51:26.430

1

The comment by Chipperyman on this answer by CousinCocaine is true. But it is true for the answer by @RegisteredUser too. However, Chipperyman has rejected this answer by CousinCocaine, and has declared the answer by Registered User winner ex æquo. In addition, the answer by Registered User is a replica of this answer by CousinCocaine. This is more than unfair.

– Nicolas Barbulesco – 2014-06-28T16:42:45.747

3

Java 7 - 185

class Num{
public static void main(String[] args) {
    System.out.print("ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE".split(",")[(new java.util.Scanner(System.in)).nextInt()-1]);
    }}

Baby

Posted 2014-06-21T04:38:45.260

Reputation: 481

3

C 111

#include <stdio.h>
int main(){printf("%.5s","one  two  threefour five six  seveneightnine"+5*(getchar()-'1'));}

The length here is carefully engineered so I can interpret it as binary and convert that to decimal. At only 7 characters, I'm confident I have a winner!

Jerry Coffin

Posted 2014-06-21T04:38:45.260

Reputation: 539

1

+1 Code-golfers generally don't mind if your c code compiles with warnings, as long as it meets the specs. So you can omit the #include and main()'s return type. Also a bit of refactoring of your expression: main(){printf("%.5s","one two threefour five six seveneightnine"+5*getchar()-245);}. 86 chars by my count.

– Digital Trauma – 2014-06-24T15:51:02.840

I don't get the length stuff, and I don't see where there would be 7 chars. – Nicolas Barbulesco – 2014-06-26T18:06:38.563

@NicolasBarbulesco: It's 111 characters--but if we treat the 111 as binary, that would convert to 7 in decimal. – Jerry Coffin – 2014-06-26T18:13:08.173

Jerry, there is code golf running in your head ! – Nicolas Barbulesco – 2014-07-08T10:29:09.050

2

VBScript 98 80 75

msgbox split(" one two three four five six seven eight nine")(inputbox(""))

comfortablydrei

Posted 2014-06-21T04:38:45.260

Reputation: 701

2

Ruby 64

p %w?one two three four five six seven eight nine?[$*[0].to_i-1]

Mhmd

Posted 2014-06-21T04:38:45.260

Reputation: 2 019

1You can chop few more bytes by substituting 9 directly at the place of x – Mr. Alien – 2014-06-21T09:38:24.607

6I don't see where this code takes in input. And why write 9-1 when you can just say 8? Of course both 9-1 and 8 are wrong; this script outputs "nine" all the time and ignores input. – Ray Toal – 2014-06-21T22:27:39.397

2

J - 68 or 60 57 or 53 bytes

Interactive version (stdin):

>one`two`three`four`five`six`seven`eight`nine{~<:".1!:1]1

Function version:

f=:one`two`three`four`five`six`seven`eight`nine>@{~<:

Explanation:

f=:one`two`three`four`five`six`seven`eight`nine>@{~<:
                                                   <: Decrease by one
                                                 {~   Get the correct string
                                               >@     Unbox

".(1!:1)1 reads a string and converts it to integer

seequ

Posted 2014-06-21T04:38:45.260

Reputation: 1 714

You can save one char by writing it as a train ((;:'one two blah')>@{~<:) and another six by using the gerund one`two`blah instead of ;:. – algorithmshark – 2014-06-21T16:57:35.470

@algorithmshark Thanks! I'm still learning J. Although, wouldn't the train version require the text to be infinite rank (edit: apparently not... why not?) and the gerund version extra quotes (edit: oh, it creates a list of boxes)? – seequ – 2014-06-21T17:07:23.513

A fork can have a noun in the left argument, which is shorthand for a constant verb producing that noun with infinite rank (q.v. the Dictionary). And no, no extra quotes on the gerund: undefined names are treated as references to verbs, and the gerund for that is just a boxed string containing the name.

– algorithmshark – 2014-06-21T17:22:02.807

Also, you don't have to write the STDIN version as a verb expression: >one`two`three`four`five`six`seven`eight`nine{~<:".1!:1]1 is 3 chars shorter. – algorithmshark – 2014-06-21T17:35:49.527

1@algorithmshark Haha, why didn't I do it that way? – seequ – 2014-06-21T18:02:34.597

@algorithmshark Also, your website is correct. – seequ – 2014-06-22T09:46:56.607

2

Powershell - 91 74

[Enum]::ToObject([System.DirectoryServices.ActiveDirectory.HourOfDay],[Console]::Read()-48)

Found out how to cast to remove the Enum and ToObject call:

[System.DirectoryServices.ActiveDirectory.HourOfDay]([Console]::Read()-48)

How does this work?

HourOfDay is an enum type so we can use names instead of magic constants. Enum types have a ToString() implementation that gives you the name of the constant value. You can cast an int to an enum. Console.Read() reads a character from the input stream represented as an integer. typing '1' gives 49, substract 48 to get 1, cast to HourOfDay to return 'One'.

Because powershell does a ToString on all objects being written to the output stream and doesn't need the fluff to turn this into an executable this all that is needed besides powershell...

rene

Posted 2014-06-21T04:38:45.260

Reputation: 301

2

In AppleScript ; 123 chars.

{"one","two","three","four","five","six","seven","eight","nine"}'s item((display dialog""default answer"")'s text returned)

This script takes the input in a dialog. Then it gives the output in AppleScript’s result.

Example :

  • Input : 6
  • Output : "six"

Here is a nicer version :

set l to {"one","two","three","four","five","six","seven","eight","nine"}
set n to text returned of (display dialog "?" default answer "")
display dialog (l's item n) buttons ("OK") default button 1

This version displays the output in a nice dialog.

Example :

  • Input : 9
  • Output : nine

[ Answer edited ; slightly improved the dialog for the input ; still 124 chars. Answer edited again ; now 1 char less ! ]

Nicolas Barbulesco

Posted 2014-06-21T04:38:45.260

Reputation: 249

1set n to text returned of ... Wow. – seequ – 2014-06-22T17:27:36.440

2

CJam - 50

This is a plain ASCII solution that uses HTTP requests (this is allowed in the question):

"aj21.com/"r+g

On the server there are 9 plain-text files named 1, 2, ..., 9, each containing the corresponding word.

Total size: 14 + 3 ("one") + 3 ("two") + 5 + 4 + 4 + 3 + 5 + 5 + 4 = 50.
It can be golfed more by using a shorter domain.

The online interpreter doesn't support HTTP requests, so the program needs to be run using the java interpreter.

aditsu quit because SE is EVIL

Posted 2014-06-21T04:38:45.260

Reputation: 22 326

1Please add a comment if downvoting. – aditsu quit because SE is EVIL – 2014-06-22T12:45:02.693

-1, this is a standard loophole.

– nyuszika7h – 2014-06-22T21:58:05.130

3@nyuszika7h it's specifically allowed in this question – aditsu quit because SE is EVIL – 2014-06-23T02:46:23.020

1Sorry, I missed that. I can't undo my downvote now until the answer is edited. – nyuszika7h – 2014-06-23T09:34:06.360

1@nyuszika7h edited – aditsu quit because SE is EVIL – 2014-06-23T10:26:30.717

2

Perl 36 (58 standalone)

use Number::Spell;say spell_number<>

Or, without additional modules:

say qw(one two three four five six seven eight nine)[<>-1]

el.pescado

Posted 2014-06-21T04:38:45.260

Reputation: 2 017

Your first script does not work, on my Mac. Can't locate Number/Spell.pm in @INC (@INC contains: /Library/Perl/5.16/darwin-thread-multi-2level /Library/Perl/5.16 /Network/Library/Perl/5.16/darwin-thread-multi-2level /Network/Library/Perl/5.16 /Library/Perl/Updates/5.16.2 /System/Library/Perl/5.16/darwin-thread-multi-2level /System/Library/Perl/5.16 /System/Library/Perl/Extras/5.16/darwin-thread-multi-2level /System/Library/Perl/Extras/5.16 .) at toto.pl line 1. BEGIN failed--compilation aborted at toto.pl line 1. – Nicolas Barbulesco – 2014-06-26T17:59:45.560

Your second script does not work, on my Mac. syntax error at titi.pl line 1, near "say qw(one two three four five six seven eight nine)" Execution of titi.pl aborted due to compilation errors. – Nicolas Barbulesco – 2014-06-26T18:01:56.247

2

Batch - 86

Far shorter than the other batch answer, and actually surprisingly competitive.

for /f "tokens=%1" %%1 in ("one two three four five six seven eight nine") do echo>%%1

Used as filename.bat number, and the output is in the form of a file with the name of the correct number.

Οurous

Posted 2014-06-21T04:38:45.260

Reputation: 7 916

2

BASH 9

shameless Linux-replica of cousincoicane's answer

spd-say 1

speaks out one

Registered User

Posted 2014-06-21T04:38:45.260

Reputation: 297

1Wouldn't this only work if the input was 1? – Jon – 2014-06-25T04:38:22.817

1@Chipperyman it works for any number – Registered User – 2014-06-25T04:38:53.337

Limit, but clever ! – Nicolas Barbulesco – 2014-06-26T17:27:22.483

This misses the input. – Nicolas Barbulesco – 2014-06-26T17:32:31.693

@nicolasbarbulesco yes. but there were many other answers that miss input, so I went ahead with this. – Registered User – 2014-06-27T02:39:47.377

@Chipperyman — You have accepted this answer, missing the input, but you have rejected the answer by CousinCocaine because it misses the input. And this answer is a replica of CousinCocaine's answer. This is more than unfair. – Nicolas Barbulesco – 2014-06-28T10:00:10.717

@RegisteredUser — No, it does not work for numbers ≠ 1. – Nicolas Barbulesco – 2014-06-28T18:35:55.757

The other solutions — including mines — would be shorter too if they placed the “input” directly at the target spot. – Nicolas Barbulesco – 2014-06-28T18:41:21.560

@NicolasBarbulesco It works for every number.have you checked it on a linux system? – Registered User – 2014-06-29T06:41:23.357

2

GolfScript, 50 bytes

I wanted to see if I could beat Quincunx's 51-byte self-contained GolfScript solution. Turns out that, with enough tricks, yes, I can — by one byte.

Since one of the tricks I'm using is the use of bytes outside the printable ASCII range, the resulting program is cannot be directly pasted here. Instead, I'm providing a hex dump of it; users on Unixish systems can use xxd -r to reconstruct the actual 50-byte GolfScript program from the hex dump:

0000000: 7e6e 270b 97eb 442f e166 9894 9f00 c63c  ~n'...D/.f.....<
0000010: 8128 73a3 9b55 5065 a9fb f06a 2727 ff16  .(s..UPe...j''..
0000020: 277b 6261 7365 7d2f 2b6e 2f3d 7b39 392b  '{base}/+n/={99+
0000030: 7d25                                     }%

The basic trick used to generate this program is simple: I compress the long string literal that makes up most of Quincunx's code by subtracting 99 (the ASCII code of the letter c) from the character values, interpreting the resulting values as a number in base 22 (enough to encode the letters up to x) and then re-encode the resulting number in base 255 to produce the unprintable byte string that makes up most of the first half of my program. The rest of the program then reverses this process, decoding the string back into something printable.

(Since the lowest letter actually present in the number names is e, I could've shortened the byte string further by subtracting 101 from the ASCII codes and using base 20. However, subtracting 101 would've mapped the letter o to a newline, which I'm using as the number delimiter because it's conveniently available as the built-in constant n in GolfScript. Working around that would cost me more than the one byte that using a lower base would save. Using the offset 99 leaves the newline corresponding to the letter m, which is conveniently absent from the number names.)

Here's a de-golfed version of the program:

~       # eval the input, turning it into a number
n       # push a newline onto the stack; we'll need it later

# this is the byte string encoding the number names:
"\x0B\x97\xEBD/\xE1f\x98\x94\x9F\x00\xC6<\x81(s\xA3\x9BUPe\xA9\xFB\xF0j"

# convert the encoded string from base 255 to base 22
# (and, incidentally, from a string to an array):
"\xFF\x16" {base}/

+       # prepend the newline pushed earlier to the array, re-stringifying it
n/      # split the resulting string at newlines
=       # pick the substring corresponding to the input number
{99+}%  # add 99 to the character values in the chosen substring

Ilmari Karonen

Posted 2014-06-21T04:38:45.260

Reputation: 19 513

1

Jelly, 35 bytes

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

Try it online!

Explanation:

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

Erik the Outgolfer

Posted 2014-06-21T04:38:45.260

Reputation: 38 134

1

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"

rnso

Posted 2014-06-21T04:38:45.260

Reputation: 1 635

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 – 2016-12-18T02:56:11.693

1

Wolfram, 11

There is a function for that of course: IntegerName

swish

Posted 2014-06-21T04:38:45.260

Reputation: 7 484

1

PHP, 93 73 68 bytes

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

Try it Online!

Mr. Alien

Posted 2014-06-21T04:38:45.260

Reputation: 141

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 – 2014-06-22T11:03:28.933

1

Tcl, 61

Number entered as command line argument

lindex {- one two three four five six seven eight nine} $argv

slebetman

Posted 2014-06-21T04:38:45.260

Reputation: 629

1

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

Jwosty

Posted 2014-06-21T04:38:45.260

Reputation: 3 530

You could do Console.Read() like I did in the Csharp version. – rene – 2014-06-21T15:39:00.853

1@rene ah, didn't know that existed. Thanks! :) – Jwosty – 2014-06-21T15:39:55.110

You don't need the parse...http://msdn.microsoft.com/en-us/library/system.console.read(v=vs.110).aspx you get an int as return value for the ascii code of the char

– rene – 2014-06-21T15:41:04.500

@rene Didn't see that either... Thanks again! xP – Jwosty – 2014-06-21T15:42:16.910

1Wait, how does adding a single character make it 124 bytes from 107? – nyuszika7h – 2014-06-22T22:20:06.763

1@nyuszika7h oops, I forgot to update that number earlier. Fixed. – Jwosty – 2014-06-22T22:35:13.187

1

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.

Sylwester

Posted 2014-06-21T04:38:45.260

Reputation: 3 678

You can save 3 characters in the Racket code by using 0 (or any other single character) instead of zero. – nyuszika7h – 2014-06-22T22:19:07.817

@nyuszika7h Thanks. I originally though zero was needed and I forgot to remove it from the Racket version. – Sylwester – 2014-06-22T23:59:13.383

@Charles It's done :) – Sylwester – 2014-06-23T08:28:51.860

1

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()]

Jack

Posted 2014-06-21T04:38:45.260

Reputation: 199

1

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.

Anti

Posted 2014-06-21T04:38:45.260

Reputation: 11

1

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

Milo

Posted 2014-06-21T04:38:45.260

Reputation: 3 082

This is not cheating. This is what I wanted to do in AppleScript, but I did not find the built-in method I wanted. So I wrote the method myself.

– Nicolas Barbulesco – 2014-06-22T11:13:52.780

1

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!

Colt

Posted 2014-06-21T04:38:45.260

Reputation: 11

you can eliminate the declaration of v which saves you 4 chars.. that is, p %w(...)[...] – Not that Charles – 2014-06-23T01:41:55.453

1

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

Digital Trauma

Posted 2014-06-21T04:38:45.260

Reputation: 64 644

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 – 2014-06-26T18:41:57.403

1

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.

Losiowaty

Posted 2014-06-21T04:38:45.260

Reputation: 121

1

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

plannapus

Posted 2014-06-21T04:38:45.260

Reputation: 8 610

1

PowerShell (71)

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

Works for valid inputs.

microbian

Posted 2014-06-21T04:38:45.260

Reputation: 2 297

1

Javascript, 94 92 84 bytes

alert(['one','two','three','four','five','six', 'seven','eight','nine'][prompt()-1])

Cilan

Posted 2014-06-21T04:38:45.260

Reputation: 724

1Explicit cast is useless : alert(['one','two','three','four','five','six', 'seven','eight','nine'][prompt()-1]) – Michael M. – 2014-06-23T18:15:07.590

You can also abuse of split : alert('0one0two0three0four0five0six0seven0eight0nine'.split(0)[prompt()]) but that is the answer of @nderscore – Michael M. – 2014-06-23T18:16:42.170

1

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);
}

aragaer

Posted 2014-06-21T04:38:45.260

Reputation: 431

Why not main(x){...} instead of x;main(){...} in the first program? – Spikatrix – 2015-05-24T14:28:20.133

1

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/

Kevin

Posted 2014-06-21T04:38:45.260

Reputation: 501

1

Powershell 63

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

(The default delimiter in Powershell is whitespace)

DarkAjax

Posted 2014-06-21T04:38:45.260

Reputation: 669

1

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

hdante

Posted 2014-06-21T04:38:45.260

Reputation: 181

1

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.

Timwi

Posted 2014-06-21T04:38:45.260

Reputation: 12 158

Isn't it disallowed to use such languages? – Registered User – 2014-06-25T04:33:39.543

@reg Why would it be? – undergroundmonorail – 2014-06-25T08:31:00.913

For this reason http://meta.codegolf.stackexchange.com/a/1071/13171

– Registered User – 2014-06-26T04:22:23.473

@RegisteredUser: Sclipting is not designed for any particular challenge. It is, in fact, similar in goal to Golfscript. – Timwi – 2014-06-26T07:42:07.717

1

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

Nicolas Barbulesco

Posted 2014-06-21T04:38:45.260

Reputation: 249

2I 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 quit because SE is EVIL – 2014-06-26T19:59:32.853

1

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

DrJPepper

Posted 2014-06-21T04:38:45.260

Reputation: 499

0

Factor, 11 bytes

a builtin: number>text.

cat

Posted 2014-06-21T04:38:45.260

Reputation: 4 989

0

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

technosaurus

Posted 2014-06-21T04:38:45.260

Reputation: 231

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 – 2014-06-22T01:25:28.473

@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 – 2014-06-22T02:08:30.630

The ! isn't needed http://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 – 2014-06-22T02:11:29.037

@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 – 2014-06-22T02:16:31.577

As long as I'm not held accountable for it (except in perhaps a good way) I love writing hacky code :P – Eric Lagergren – 2014-06-22T02:18:21.400

0

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

draegtun

Posted 2014-06-21T04:38:45.260

Reputation: 1 592

0

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

MB6

Posted 2014-06-21T04:38:45.260

Reputation: 9

0

Ruby - 66

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

Siva

Posted 2014-06-21T04:38:45.260

Reputation: 241

0

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

Sylwester

Posted 2014-06-21T04:38:45.260

Reputation: 3 678

0

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.

Sylwester

Posted 2014-06-21T04:38:45.260

Reputation: 3 678

0

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.

18446744073709551615

Posted 2014-06-21T04:38:45.260

Reputation: 111

0

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

+>,-------------------------------------------------[-[-[-[-[-[-[-[[-]+
[------->++<]>.-----.+++++.---------.[-]<[-]>[-]]<[[-]--[----->+<]>-.++
++.--.+.++++++++++++.[-]<[-]>[-]][-]<[-]>[-]]<[[-]+[--------->++<]>+.++
++[->+++<]>.[--->+<]>-.+[->+++<]>.+++++++++.[-]<[-]>[-]][-]<[-]>[-]]<[[
-]+[--------->++<]>+.----------.-[--->+<]>.[-]<[-]>[-]][-]<[-]>[-]]<[[-
]--[----->+<]>.+++.[->++++++<]>.+[->+++<]>.[-]<[-]>[-]][-]<[-]>[-]]<[[-
]--[----->+<]>.+++++++++.++++++.---.[-]<[-]>[-]][-]<[-]>[-]]<[[-]------
--[-->+++<]>.------------.++++++++++.-------------..[-]<[-]>[-]][-]<[-]
>[-]]<[[-]--------[-->+++<]>.+++.--------.[-]<[-]>[-]][-]<[-]>[-]]<[[-]
+[------->++<]>+.-.---------.[-]]

rpax

Posted 2014-06-21T04:38:45.260

Reputation: 171

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? – german_guy – 2014-06-23T12:40:35.013

1Ups!. I'll change it. Thanks @german_guy – rpax – 2014-06-23T13:21:34.717

2@german_guy changed! – rpax – 2014-06-23T13:29:14.073

@ProgramFOX I'm sorry I don't understand you. Now it is using only swipl. – rpax – 2014-06-23T13:55:42.380

@rpax Ah, now I see. I thought you were only using bash now. I'm sorry. – ProgramFOX – 2014-06-23T14:12:11.290

0

Haskell : 89

do x<-readLn;print$[[],"one","two","three","four","five","six","seven","eight","nine"]!!x

rpax

Posted 2014-06-21T04:38:45.260

Reputation: 171

0

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)];

mcrumley

Posted 2014-06-21T04:38:45.260

Reputation: 101

0

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.

John B

Posted 2014-06-21T04:38:45.260

Reputation: 109

0

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.

Registered User

Posted 2014-06-21T04:38:45.260

Reputation: 297

Why not num2words(raw_input())? Or even input(), since it's guaranteed to be a number? – undergroundmonorail – 2014-06-25T08:32:07.927

1@undergroundmonorail Thanks. didn't knew about it. I'm not too good in python. – Registered User – 2014-06-26T04:23:24.127

0

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.

Nicolas Barbulesco

Posted 2014-06-21T04:38:45.260

Reputation: 249

0

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!

o79y

Posted 2014-06-21T04:38:45.260

Reputation: 509

Does this actually output in japanese? – Erik the Outgolfer – 2016-07-20T21:55:45.587

Yes. ideone.com test. @Eʀɪᴋ ᴛʜᴇ Gᴏʟғᴇʀ

– o79y – 2016-07-21T03:34:27.623

0

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

Erik the Outgolfer

Posted 2014-06-21T04:38:45.260

Reputation: 38 134

Nine // is just enough for /_/\/\// to save a byte. – Martin Ender – 2016-07-21T09:37:28.267

@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. – Erik the Outgolfer – 2016-07-21T09:39:09.530

@MartinEnder So that is the reason I've had problems with forward slashes (/) in the past... – Erik the Outgolfer – 2016-07-21T09:44:52.507

0

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

Erik the Outgolfer

Posted 2014-06-21T04:38:45.260

Reputation: 38 134