Given a number, program must return the alphabet letter correspondent

-5

0

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.

BernaMariano

Posted 2012-09-14T02:13:41.467

Reputation: 329

I suspect the winning answer will be tiny. – DavidC – 2012-09-14T02:53:50.323

I think my answer won – dspyz – 2012-09-18T01:01:39.363

@BernaMariano What does "given a number" mean? – Toothbrush – 2014-02-20T11:34:40.327

@toothbrush If Goku gives you a 3, use 3. – BernaMariano – 2014-02-25T18:43:42.157

@BernaMariano Well, I'm not sure what you mean, but is my answer OK, then?

– Toothbrush – 2014-02-25T18:44:58.023

Answers

2

DC - 6 characters

Full program including input and output.

?64+af

save to file and run with $ dc file

daniero

Posted 2012-09-14T02:13:41.467

Reputation: 17 193

21 character shorter: ?64+P – seshoumara – 2016-09-02T08:46:07.270

Typically: dc -f file – None – 2018-03-20T09:57:13.067

7

APL (4)

(Full program)

⎕⌷⎕A

Explanation:

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

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

marinus

Posted 2012-09-14T02:13:41.467

Reputation: 30 224

7

Brainfuck, 107 bytes

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

dspyz

Posted 2012-09-14T02:13:41.467

Reputation: 875

3

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

Jo King

Posted 2012-09-14T02:13:41.467

Reputation: 38 234

Can be made shorter. Instead of tens, use "fives". Increment the tens by two for each ten, then multiply by five. Saves 3 Bytes. ,>,[<[-<+>[-<]>>]>[>++>]<++[-<+++++>]]++++[-<++++>]<. – Dorian – 2018-07-13T11:14:09.070

Even more. By changing the 44 routine in the end to 35+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. ,>,[<[-<+>[-<]>>]<<<[->+<]>+>>]<<+++[->+++++<]>+. – Dorian – 2018-07-13T11:45:04.200

@Dorian Your second solution doesn't work for numbers over 20, but thanks for the bytes saved! – Jo King – 2018-07-13T12:36:05.520

Oh, I'm sorry. Didn't test it well enough. I'm glad you repaired it. – Dorian – 2018-07-13T12:44:07.123

2

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.

Jo King

Posted 2012-09-14T02:13:41.467

Reputation: 38 234

2

R, 11 characters

LETTERS[x]

Usage:

LETTERS[21]
[1] "U"

Paolo

Posted 2012-09-14T02:13:41.467

Reputation: 696

1

TI-Basic, 34 bytes

sub("ABCDEFGHIJKLMNOPQRSTUVWXYZ",Ans,1

This is as short as it gets...

Timtech

Posted 2012-09-14T02:13:41.467

Reputation: 12 038

1

PHP, 17 bytes

<?=chr(64+$argn);

Run s pipe with -F

Titus

Posted 2012-09-14T02:13:41.467

Reputation: 13 814

1

MATL, 4 bytes (Non-competing)

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.

Stewie Griffin

Posted 2012-09-14T02:13:41.467

Reputation: 43 471

1

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)

Tornado547

Posted 2012-09-14T02:13:41.467

Reputation: 389

@Gt for 3 bytes – ovs – 2017-11-28T16:21:47.303

@ovs Thanks. Forgot about t and implicit Q – Tornado547 – 2017-11-28T16:27:37.797

1

Ly, 6 bytes

n8:*+o

Try it online!

weatherman115

Posted 2012-09-14T02:13:41.467

Reputation: 605

1

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.

Sophia Lechner

Posted 2012-09-14T02:13:41.467

Reputation: 1 200

1

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 ( {
 . . . .
? ' + ; @
 . . . .
  . . .

Adyrem

Posted 2012-09-14T02:13:41.467

Reputation: 521

1

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.

Gabriel Mills

Posted 2012-09-14T02:13:41.467

Reputation: 778

You can. Try this [char](64+"$args"). :)

– mazzy – 2018-12-27T19:37:10.397

1

Powershell 6, 17 bytes

('@'..'Z')[$args]

mazzy

Posted 2012-09-14T02:13:41.467

Reputation: 4 832

1

J, 7 characters

a.{~64+

Usage:

   a.{~64+1
A

Gareth

Posted 2012-09-14T02:13:41.467

Reputation: 11 678

1

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$

kSiR

Posted 2012-09-14T02:13:41.467

Reputation: 111

0

Tcl, 30 bytes

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

Try it online!

sergiol

Posted 2012-09-14T02:13:41.467

Reputation: 3 055

0

BrainFuck, 66

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

l4m2

Posted 2012-09-14T02:13:41.467

Reputation: 5 985

Require number ends with \n(10) and mod 256 env – l4m2 – 2017-11-28T04:16:21.997

Please add some explanation in the body of the answer. – pajonk – 2017-11-28T10:31:42.863

0

Jelly, 3 bytes

ịØA

Try it online!

caird coinheringaahing

Posted 2012-09-14T02:13:41.467

Reputation: 13 702

0

SmileBASIC, 18 bytes

INPUT N?CHR$(N+64)

12Me21

Posted 2012-09-14T02:13:41.467

Reputation: 6 110

0

Java 8, 15 bytes (as lambda function)

n->(char)(n+64)

Try it online.

Java 8, 84 bytes (as full program)

interface M{static void main(String[]a){System.out.printf("%c",new Byte(a[0])+64);}}

Try it online.

Kevin Cruijssen

Posted 2012-09-14T02:13:41.467

Reputation: 67 575

1This works only for single digit. Double digit input will fail. ex: give 10 as input and output still comes as A. You care considering charAt(0), for double digits, you have to consider 2 characters. – BarathVutukuri – 2018-03-20T10:46:54.850

@BarathVutukuri Thanks, fixed! – Kevin Cruijssen – 2018-03-20T10:49:25.223

0

Haskell, 11 bytes

(['@'..]!!)

Try it online! This anonymous function indexes into the string "@ABCDEFG ...".

Laikoni

Posted 2012-09-14T02:13:41.467

Reputation: 23 676

0

Python 3, 91 bytes

Just for fun, the Rube Goldberg equivalent

print(chr(sum((ord(j)-ord('0'))*10**i for i,j in enumerate(reversed(input())))+ord('A')-1))

Try it online!

Rick Rongen

Posted 2012-09-14T02:13:41.467

Reputation: 223

Unfortunately, this answer is not a serious contender because it is optimized for complexity rather than length. – pppery – 2020-01-07T00:20:52.720

0

Fortran (GFortran), 30 bytes

READ*,I
PRINT*,ACHAR(I+64)
END

My entry in the good ol' Fortran.

Try it online!

rafa11111

Posted 2012-09-14T02:13:41.467

Reputation: 310

0

FORTH 16 BYTES

: n 64 + EMIT ;

OUTPUT:

8 N
8 N H ok
5 N
5 N E ok
12 N
12 N L ok
12 N
12 N L ok
15 N
15 N O ok
23 N
23 N W ok
15 N
15 N O ok
18 N
18 N R ok
12 N
12 N L ok
4 N
4 N D ok

panicmore

Posted 2012-09-14T02:13:41.467

Reputation: 51

0

Tcl, 31 bytes

puts [format %c [incr argv 64]]

Try it online!

sergiol

Posted 2012-09-14T02:13:41.467

Reputation: 3 055

0

Japt, 2 bytes

dI

Try it or test all numbers

Shaggy

Posted 2012-09-14T02:13:41.467

Reputation: 24 623

0

Classic ASP (14 bytes):

<%=Chr(c+64)%>

Expects c to hold the character number.

Toothbrush

Posted 2012-09-14T02:13:41.467

Reputation: 3 197

program must return -> I see no program, I don't see c being initialized, I don't see anything returned/printed. – RobIII – 2014-02-19T21:32:36.760

@RobIII OK; changed to Classic ASP. Is that what you mean? – Toothbrush – 2014-02-20T09:18:13.467

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.). – RobIII – 2014-02-20T11:30:35.187

@RobIII The question says "given". Perhaps we can ask for clarification? – Toothbrush – 2014-02-20T11:31:18.737

I interpret this as "when the program is given" but asking for clarification won't hurt ;) – RobIII – 2014-02-20T11:32:08.600

0

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

PauloHDSousa

Posted 2012-09-14T02:13:41.467

Reputation: 119

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. – Justin – 2014-02-19T20:58:35.400

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.

– RobIII – 2014-02-19T21:20:55.980

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. – RobIII – 2014-02-20T15:04:31.007

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

– RobIII – 2014-02-20T15:07:49.100

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())]);}}}. – RobIII – 2014-02-20T15:10:38.753

0

Haskell, 38 bytes

main = putChar $ ('a':['a'..'z']) !! c

Where c is the number.

PyRulez

Posted 2012-09-14T02:13:41.467

Reputation: 6 547

There is a lot of unnecessary white space: main=putChar$('a':['a'..'z'])!!c works as well. However you can not assume input to be present in a certain variable, but then again returning from a function is acceptable, so f c=('a':['a'..'z'])!!c or equivalently (('a':['a'..'z'])!!) works. – Laikoni – 2018-03-20T10:54:27.910

0

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

RobIII

Posted 2012-09-14T02:13:41.467

Reputation: 397

I've changed my answer to a full Classic ASP example now. – Toothbrush – 2014-02-20T09:19:31.160

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. – mroman – 2014-10-01T13:17:01.353

@mroman See my comments on the Classic ASP and C answers.

– RobIII – 2014-10-01T19:40:39.950

0

q/k (7)

As partially applied composition:

10h$64+

skeevey

Posted 2012-09-14T02:13:41.467

Reputation: 4 139

0

Ruby 1.9 (10)

(x+64).chr

Reading from STDIN and printing to STDOUT:

$><<(gets.to_i+64).chr

Paul Prestidge

Posted 2012-09-14T02:13:41.467

Reputation: 2 390

0

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@\/!!

mroman

Posted 2012-09-14T02:13:41.467

Reputation: 1 382

0

Python 2, 9 bytes

chr(64+x)

Or, reading from stdin and printing to stdout: 21

print chr(64+input())

Matt

Posted 2012-09-14T02:13:41.467

Reputation: 1 395

1Submissions have to be full programs or functions, and your first solution is a "snippet". You should only include your second solution. – Jakob – 2017-11-28T02:38:59.687

0

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

Rob

Posted 2012-09-14T02:13:41.467

Reputation: 1 277

It can indeed: assuming that the only thing on the stack is the number from 1 to 26, you don't need the [. – Peter Taylor – 2012-09-18T12:14:18.633

@PeterTaylor Okay, thank you! – Rob – 2012-09-18T20:31:45.940

0

Javascript, 25 chars

String.fromCharCode(66-x);

PHP, 22 chars

echo chr(66-$argv[0])

Clyde Lobo

Posted 2012-09-14T02:13:41.467

Reputation: 1 395

-1

PHP 442

$alphabet = Array('A' => 1,'B' => 2,'C' => 3,'D' => 4,'E' => 5,'F' => 6,'G' => 7,'H' => 8,
'I' => 9,'J' => 10,'K' => 11,'L' => 12,'M' => 13,'N' => 14,'O' => 15,'P' => 16,'Q' => 17,
'R' => 18,'S' => 19,'T' => 20,'U' => 21,'V' => 22,'W' => 23,'X' => 24,'Y' => 25,'Z' => 26
)

function testInputForAlphabetCharacter($input){
    for($index = 0; $index < count($alphabet); $i = $i + 1){
        if($alphabet[$index] == $input){
            echo($index);
        }
    }
}


$input = $_GET['input'];
testInputForAlphabetCharacter($input);

scleaver

Posted 2012-09-14T02:13:41.467

Reputation: 507

2@scleaver... o_0 – Ian Brindley – 2014-02-20T09:30:31.780

2I agree with @IanBrindley, this is serious overkill... There's no chance of shrinking this down? – WallyWest – 2014-02-20T10:56:06.623

1echo chr(64+$input); – Ian Brindley – 2014-02-20T13:29:07.370

4this is as small as i could get it – scleaver – 2014-02-21T23:11:02.473