-5
\$\begingroup\$

The program

Given a number, program must return the alphabet letter correspondent.

Rules

  • The challenge here is to make it as short as possible, everyone knows it is a very easy program to do.
  • The number is not zero-based, it means 1 is A, 2 is B, 3 is C and so it goes...
  • Any language will be accepted.
\$\endgroup\$
5
  • \$\begingroup\$ I suspect the winning answer will be tiny. \$\endgroup\$
    – DavidC
    Commented Sep 14, 2012 at 2:53
  • \$\begingroup\$ I think my answer won \$\endgroup\$
    – dspyz
    Commented Sep 18, 2012 at 1:01
  • \$\begingroup\$ @BernaMariano What does "given a number" mean? \$\endgroup\$
    – Toothbrush
    Commented Feb 20, 2014 at 11:34
  • \$\begingroup\$ @toothbrush If Goku gives you a 3, use 3. \$\endgroup\$
    – user5403
    Commented Feb 25, 2014 at 18:43
  • \$\begingroup\$ @BernaMariano Well, I'm not sure what you mean, but is my answer OK, then? \$\endgroup\$
    – Toothbrush
    Commented Feb 25, 2014 at 18:44

38 Answers 38

7
\$\begingroup\$

APL (4)

(Full program)

⎕⌷⎕A

Explanation:

(user input) (index) ⎕A (alphabet)

(They're supposed to be boxes, it's not an encoding problem.)

\$\endgroup\$
7
\$\begingroup\$

Brainfuck, 107 bytes

>,----------[>++++++[-<------>],----------]<[<]>>[<--[->++++++++++<]>>]+++++++[-<+++++++++>]<-.>++++++++++.
\$\endgroup\$
3
\$\begingroup\$

brainfuck, 64 56 50 bytes

Thanks to Dorian for saving 3 bytes and inspiring 3 more

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

Try it online!

How it works

,>, Gets input
   [ if a two digit number
    <+[-<+>[-<]>>] Gets modulo 2 of first number plus 1
                  <[<++<-] Sets the fives cell to 2 if the first number is a 2
                          >++ Adds another 2 to the fives cell
                             >>]  Moves to cell after ones cell
                                <<+++ Adds 3 to fives cell
                                     [->+++++<] Multiply the fives cell by 5 and add it to the ones cell
                                               >+.  Add one and print
\$\endgroup\$
4
  • \$\begingroup\$ Can be made shorter. Instead of tens, use "fives". Increment the tens by two for each ten, then multiply by five. Saves 3 Bytes. ,>,[<[-<+>[-<]>>]>[>++>]<++[-<+++++>]]++++[-<++++>]<. \$\endgroup\$
    – Dorian
    Commented Jul 13, 2018 at 11:14
  • \$\begingroup\$ Even more. By changing the 4*4 routine in the end to 3*5+1, you can use the fives and add them to the 3. It's a bit tricky to exit the if then, but it saves another 4 Bytes. ,>,[<[-<+>[-<]>>]<<<[->+<]>+>>]<<+++[->+++++<]>+. \$\endgroup\$
    – Dorian
    Commented Jul 13, 2018 at 11:45
  • \$\begingroup\$ @Dorian Your second solution doesn't work for numbers over 20, but thanks for the bytes saved! \$\endgroup\$
    – Jo King
    Commented Jul 13, 2018 at 12:36
  • \$\begingroup\$ Oh, I'm sorry. Didn't test it well enough. I'm glad you repaired it. \$\endgroup\$
    – Dorian
    Commented Jul 13, 2018 at 12:44
2
\$\begingroup\$

DC - 6 characters

Full program including input and output.

?64+af

save to file and run with $ dc file

\$\endgroup\$
2
  • 2
    \$\begingroup\$ 1 character shorter: ?64+P \$\endgroup\$
    – seshoumara
    Commented Sep 2, 2016 at 8:46
  • \$\begingroup\$ Typically: dc -f file \$\endgroup\$
    – user19214
    Commented Mar 20, 2018 at 9:57
2
\$\begingroup\$

R, 11 characters

LETTERS[x]

Usage:

LETTERS[21]
[1] "U"
\$\endgroup\$
2
\$\begingroup\$

Befunge-93, 7 6 bytes

"&++,@

Try it online!

Takes advantage of the extra spaces at the end of a wrapping string literal by adding two spaces (32*2 = 64) to the inputted number to turn it into the corresponding alphabetic character. Funnily enough, if we use a implementation that doesn’t include the wrapping spaces, we can do "&+.@ to add the @ (64) to the number instead.

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

J, 7 characters

a.{~64+

Usage:

   a.{~64+1
A
\$\endgroup\$
1
\$\begingroup\$

TI-Basic, 34 bytes

sub("ABCDEFGHIJKLMNOPQRSTUVWXYZ",Ans,1

This is as short as it gets...

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

PHP, 17 bytes

<?=chr(64+$argn);

Run s pipe with -F

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

Ly, 6 bytes

n8:*+o

Try it online!

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

Husk, 4 bytes

c+64

Try it online!

Boring, but probably optimal. Adds 64 to the given number and converts it to ASCII. !¡→'A is cuter (awww, the exclamation marks are friends!) but one byte longer.

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

Pyth - 5 3 Bytes

@Gt

Saved two bytes thanks to ovs

Completely forgot about t and implicit Q

Explanation:

@    Index
 G   in alphabet of
  t  one less than
   Q Input (implicitly added to solve arity)
  
\$\endgroup\$
2
  • \$\begingroup\$ @Gt for 3 bytes \$\endgroup\$
    – ovs
    Commented Nov 28, 2017 at 16:21
  • \$\begingroup\$ @ovs Thanks. Forgot about t and implicit Q \$\endgroup\$
    – Tornado547
    Commented Nov 28, 2017 at 16:27
1
\$\begingroup\$

Perl, 20 characters

chr(($ARGV[0] + 64))

Verification :

risk@skynet:~/perl$ for x in {1..26}; do perl ./ord.pl $x; done;
ABCDEFGHIJKLMNOPQRSTUVWXYZrisk@skynet:~/perl$
\$\endgroup\$
1
\$\begingroup\$

Hexagony, 10 Bytes

Try it online!

A({/'+;?/@

Expanded:

  A ( {
 / ' + ;
? / @ . .
 . . . .
  . . .

This honestly took me way longer than it should have, as it's just 2 simple reflections.


The simplest version for this problem is only 2 bytes longer

  A ( {
 . . . .
? ' + ; @
 . . . .
  . . .
\$\endgroup\$
1
\$\begingroup\$

Powershell 6, 17 bytes

('@'..'Z')[$args]
\$\endgroup\$
1
\$\begingroup\$

PowerShell, 19 18 bytes

-1 byte thanks to @mazzy lol
I don't think it'll get much shorter than this.

[char](64+"$args")

Try it online!

Takes input from a command line argument.

\$\endgroup\$
1
  • \$\begingroup\$ You can. Try this [char](64+"$args"). :) \$\endgroup\$
    – mazzy
    Commented Dec 27, 2018 at 19:37
1
\$\begingroup\$

MATL, 4 bytes

64+c

Try it online!

Because, why not. Takes input implicitly, pushes 64 and adds it to the input number. Finally it converts the number to its ASCII-equivalent using c.

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

q/k (7)

As partially applied composition:

10h$64+
\$\endgroup\$
0
\$\begingroup\$

Ruby 1.9 (10)

(x+64).chr

Reading from STDIN and printing to STDOUT:

$><<(gets.to_i+64).chr
\$\endgroup\$
0
\$\begingroup\$

Burlesque (6 characters)

Assuming the number is already on the stack:

64.+L[
(see here in action.).

Does the usual: Add 64, convert to character based on ASCII value.

If number is supplied as a string via stdin to stdout (10 characters):

ri64.+L[sh

Alternative version without using ASCII value conversion:

'@'Zr@\/!!
\$\endgroup\$
0
\$\begingroup\$

Javascript, 25 chars

String.fromCharCode(66-x);

PHP, 22 chars

echo chr(66-$argv[0])
\$\endgroup\$
0
\$\begingroup\$

GolfScript, 7

64+]''+

Commentary

64    # corresponds to '@' in ASCII (65 is 'A')
+]    # add the input to 64. ']' is used for ASCII.
''+   # the conversion process

1 corresponds to A

26 corresponds to Z

\$\endgroup\$
1
  • \$\begingroup\$ It can indeed: assuming that the only thing on the stack is the number from 1 to 26, you don't need the [. \$\endgroup\$ Commented Sep 18, 2012 at 12:14
0
\$\begingroup\$

Classic ASP (14 bytes):

<%=Chr(c+64)%>

Expects c to hold the character number.

\$\endgroup\$
5
  • \$\begingroup\$ program must return -> I see no program, I don't see c being initialized, I don't see anything returned/printed. \$\endgroup\$
    – RobIII
    Commented Feb 19, 2014 at 21:32
  • \$\begingroup\$ @RobIII OK; changed to Classic ASP. Is that what you mean? \$\endgroup\$
    – Toothbrush
    Commented Feb 20, 2014 at 9:18
  • \$\begingroup\$ Where does the value of c come from? You'd need something like request.form("c") or something (and the inevitable cast to int/long etc.). \$\endgroup\$
    – RobIII
    Commented Feb 20, 2014 at 11:30
  • \$\begingroup\$ @RobIII The question says "given". Perhaps we can ask for clarification? \$\endgroup\$
    – Toothbrush
    Commented Feb 20, 2014 at 11:31
  • \$\begingroup\$ I interpret this as "when the program is given" but asking for clarification won't hurt ;) \$\endgroup\$
    – RobIII
    Commented Feb 20, 2014 at 11:32
0
\$\begingroup\$

C# 4,50KB (196 characters)

My first time here =)

using System; namespace W { class P { static void Main(string[] args) {
            Console.WriteLine(" abcdefghijklmnopqrstuvwxyz".ToCharArray()[int.Parse(Console.ReadLine())]);
        }
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ First of all, don't forget the byte count of the program. Second, there are several ways of shortening this. First, remove all unnecessary whitespace (for example, the ` s after the " ",s. You could split the string " abcdefghijklmnopqrstuvwxyz"`. Also, rather than compile a list beforehand, it is much more efficient to choose the characters based off of ascii values. \$\endgroup\$
    – Justin
    Commented Feb 19, 2014 at 20:58
  • \$\begingroup\$ Also: the contest explicitly mentions The program; this is not a complete program but (part of) a function. Other than that: drop the array/lookup! Ever heard of the ASCII table? ;-) And dont forget that, if you insist on a "lookup table", a string is an array of chars: string a=" abcdefghijklmnopqrstuvwxyz"; shortens your code massively. \$\endgroup\$
    – RobIII
    Commented Feb 19, 2014 at 21:20
  • \$\begingroup\$ You can drop the namespace entirely, use the args for input instead of Console.readline, shorten args to a and drop the using System and just write System.Console.WriteLine. The .ToCharArray() is also not required (a string is a char-array implicitly). Finally, the WriteLine can be shortened to Write since the challenge doesn't specify that the program needs to output a newline. \$\endgroup\$
    – RobIII
    Commented Feb 20, 2014 at 15:04
  • \$\begingroup\$ ...So your entry could be shortened to 112 chars (compiled size doesn't matter, "lose" the 4,50KB) if you incorporate all these tips: class P{static void Main(string[] a){System.Console.WriteLine(" abcdefghijklmnopqrstuvwxyz"[int.Parse(a[0])]);}}. The only thing left to do to "optimize" this code further is to drop the alphabet string and just use ASCII as I did in my entry which will get you down to 88 :-) \$\endgroup\$
    – RobIII
    Commented Feb 20, 2014 at 15:07
  • \$\begingroup\$ Oh, and your entry states "196 characters"; I don't know how you counted (but I assume you looked at the filesize of the .cs file?): your entry is actually 162 chars when all non-functional(!) whitespace is removed: using System;namespace W{class P{static void Main(string[] args){Console.WriteLine(" abcdefghijklmnopqrstuvwxyz".ToCharArray()[int.Parse(Console.ReadLine())]);}}}. \$\endgroup\$
    – RobIII
    Commented Feb 20, 2014 at 15:10
0
\$\begingroup\$

C#, 87

class P{static void Main(string[]a){System.Console.Write((char)(int.Parse(a[0])+64));}}

Complete program (not some (part of a) "function" expecting foo to be bar to work), accepts command line parameter: foo.exe 1 prints A, foo.exe 16 prints P

C#, 10

When I make the same assumptions as, for example, the 'code-golfers' that posted the VSCript Classic ASP, Python, Burlesque, Ruby, C solutions we can get it down to 10:

(char)x+64

"Assuming x is magically initialized / passed in / on the stack / whatever excuse" and the contest doesn't explicitly require me to print it, just "return" it (which most of the above solutions don't do, either).

\$\endgroup\$
3
  • \$\begingroup\$ I've changed my answer to a full Classic ASP example now. \$\endgroup\$
    – Toothbrush
    Commented Feb 20, 2014 at 9:19
  • \$\begingroup\$ Well, the author of the question did NOT specify how the number is given. "Given a number" is completely under-specified. Is it on stdin, is it a command line argument is it given somewhere else... You have no other choice than to make assumptions about how you get that number and how you return it. \$\endgroup\$
    – mroman
    Commented Oct 1, 2014 at 13:17
  • \$\begingroup\$ @mroman See my comments on the Classic ASP and C answers. \$\endgroup\$
    – RobIII
    Commented Oct 1, 2014 at 19:40
0
\$\begingroup\$

Tcl, 30 bytes

puts [expr [scan $argv %c]-64]

Try it online!

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

Python 2, 9 bytes

chr(64+x)

Or, reading from stdin and printing to stdout: 21

print chr(64+input())
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Submissions have to be full programs or functions, and your first solution is a "snippet". You should only include your second solution. \$\endgroup\$
    – Jakob
    Commented Nov 28, 2017 at 2:38
0
\$\begingroup\$

BrainFuck, 66

,>,----------[<[->++++++++++<]++++++[>+++++++<-]>>]++++[<++++>-]<.
\$\endgroup\$
2
  • \$\begingroup\$ Require number ends with \n(10) and mod 256 env \$\endgroup\$
    – l4m2
    Commented Nov 28, 2017 at 4:16
  • \$\begingroup\$ Please add some explanation in the body of the answer. \$\endgroup\$
    – pajonk
    Commented Nov 28, 2017 at 10:31
0
\$\begingroup\$

Jelly, 3 bytes

ịØA

Try it online!

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

SmileBASIC, 18 bytes

INPUT N?CHR$(N+64)
\$\endgroup\$

Your Answer

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