22
\$\begingroup\$

Challenge

In this challenge you have to take a number as input and output the corresponding letter of the alphabet, and vice versa. (1 <=> A, 2 <=> B) etc.

1 -> A
2 -> B
...
26 -> Z

A -> 1
B -> 2
...
Z -> 26

Rules

  • This is , so shortest code in bytes wins.
  • The input will only consist of either an uppercase letter from A to Z or an integer from 1 to 26 inclusive.
  • Trailing whitespaces (space and newline) are allowed.
\$\endgroup\$
  • 1
    \$\begingroup\$ Why duplicate? O.o It is not equal. \$\endgroup\$ – Chad Aug 15 '16 at 21:50
  • 3
    \$\begingroup\$ Welcome to Programming Puzzles and Code Golf! This challenge could use a bit of clarification. For example, you could specify what inputs we would need to handle, since there are invalid inputs. I recommend posting future challenges to the Sandbox where they can get meaningful feedback before being posted to the main site. \$\endgroup\$ – Leaky Nun Aug 15 '16 at 21:55
  • 1
    \$\begingroup\$ Will we receive 26 as an integer or "26" as a string, or are both allowed? \$\endgroup\$ – Leaky Nun Aug 15 '16 at 22:38
  • 2
    \$\begingroup\$ Does it have to be uppercase, or is lowercase acceptable instead? \$\endgroup\$ – Mego Aug 16 '16 at 1:45
  • 1
    \$\begingroup\$ Seriously, another alphabet challenge? ( ͡° ͜ʖ ͡°) \$\endgroup\$ – shooqie Aug 16 '16 at 7:11

45 Answers 45

6
\$\begingroup\$

Actually, 7 bytes

ú' +ûEí

Try it online!

Explanation:

ú' +ûEí
ú' +     lowercase English alphabet, prepend space
    û    uppercase
     E   element (pushes the nth letter if input is an integer, leaves stack alone otherwise)
      í  index (pushes index of input if input is a string, leaves stack alone otherwise)

If lowercase is acceptable, this is 6 bytes:

ú' +Eí

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ You're winning at the moment, I think nobody could do a program with less 7 bytes. \$\endgroup\$ – Chad Aug 16 '16 at 10:07
  • 1
    \$\begingroup\$ I joined just to ask this. @Mego what language is this? \$\endgroup\$ – FoldedChromatin Aug 17 '16 at 8:19
  • 2
    \$\begingroup\$ @FoldedChromatin looks like github.com/Mego/Seriously \$\endgroup\$ – Alfred Bez Aug 17 '16 at 10:16
  • 1
    \$\begingroup\$ @FoldedChromatin Actually, it's Actually. Hence Actually, 7 bytes. :P \$\endgroup\$ – Dan Aug 17 '16 at 13:37
  • 2
    \$\begingroup\$ Moments like these make me happy about the names I chose for my languages :) \$\endgroup\$ – Mego Aug 17 '16 at 13:38
11
\$\begingroup\$

Pure Bash, 51

Most of the rest of the answers use some sort of conditional. This one dispenses with conditionals entirely, and instead treats the input as a base-36 number which indexes into an appropriately constructed bash-brace-expansion array:

a=(_ {A..I} {1..26} {J..Z} {A..Z})
echo ${a[36#$1]}

Ideone.

\$\endgroup\$
8
\$\begingroup\$

Erlang, 26 bytes

f([X])->X-64;f(X)->[X+64].

One of the few times where Erlang's string behavior is useful.

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

Python 2, 38 bytes

lambda x:x>''and 64^ord(x)or chr(64^x)

Test it on Ideone.

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

Python 3, 43 bytes

lambda x:x!=str(x)and chr(64|x)or ord(x)^64

The interesting thing about this solution is that it incorporates all the senses of OR, bitwise OR |, logical OR or, bitwise XOR ^ and logical XOR != ...

\$\endgroup\$
6
\$\begingroup\$

Ruby, 47 39 + n flag = 40 bytes 33 34 31 bytes

Anonymous function. Uses an exception handling trick like in @KarlNapf's Python solution.

-3 bytes from @manatwork

Try it online

->i{(64+i).chr rescue i.ord-64}

Original full program version with the n flag for 40 bytes and reads from STDIN:

puts$_!~/\d/?$_.ord-64:(64+$_.to_i).chr
\$\endgroup\$
  • \$\begingroup\$ I'm getting syntax error when trying to run on ideone, can you tell how to test? \$\endgroup\$ – Leibrug Aug 16 '16 at 6:05
  • \$\begingroup\$ @Leibrug oops! It's fixed now \$\endgroup\$ – Value Ink Aug 16 '16 at 7:27
  • \$\begingroup\$ You can reduce it more by shamelessly applying Karl Napf's trick from his Python solution: ->i{(64+i).chr rescue i.ord-64}. \$\endgroup\$ – manatwork Aug 16 '16 at 11:31
5
\$\begingroup\$

2sable, 9 8 bytes

Code:

.bAu¹kr,

Explanation:

.b        # Convert 1 -> A, 2 -> B, etc.
  A       # Push the alphabet.
   u      # Convert it to uppercase.
    ¹k    # Find the index of the letter in the alphabet.
      r   # Reverse the stack.
       ,  # Pop and print with a newline.

Uses the CP-1252 encoding. Try it online!.

\$\endgroup\$
  • 1
    \$\begingroup\$ Can't you delete ,? Which bytes are without ,? You don't need to print a new line. \$\endgroup\$ – Chad Aug 16 '16 at 11:30
  • \$\begingroup\$ @Chad Nope, that won't work for numeric inputs :( \$\endgroup\$ – Adnan Aug 16 '16 at 17:33
5
\$\begingroup\$

Cheddar, 34 32 bytes

Saved 2 bytes thanks to @LeakyNun

n->"%s"%n==n?n.ord()-64:@"(n+64)

I wish there was shorter way to check if string or number.

Try it online! or Test Suite

Explanation

n ->                // func with arg `n`
    "%s"%n==n ?     // if n is string... (see below)
       n.ord() - 64  // return code point - 64
    :               // else...
    @"(n+64)         // chr(n+64)

"%s"%n==n checks if it is a string in a simple way. "%s" is a string format, I can format with % e.g. "a %s c" % "b" is equal to "a b c". %s specifies it is a string, if a digit is passed it'll remain as %s.

\$\endgroup\$
  • \$\begingroup\$ "%s"%n==n saves 2 bytes \$\endgroup\$ – Leaky Nun Aug 16 '16 at 0:54
  • \$\begingroup\$ @LeakyNun oh that's smart! I was tryomg of doing "%d"%n==n but that wasn't working :/ \$\endgroup\$ – Downgoat Aug 16 '16 at 0:57
4
\$\begingroup\$

Haskell, 54 bytes

f s|s<"A"=[['@'..]!!read s]|1<2=show$fromEnum(s!!0)-64

Usage example: map f ["1","26","A","Z"] -> ["A","Z","1","26"].

Haskell's strict type system is a real pain here. Additionally all the short char <-> int functions like chr and ord need an import, so I have to do it by hand. For the letter -> int case, for example I need to convert String -> Char (via !!0) -> Integer (via fromEnum) -> String (via show).

\$\endgroup\$
4
\$\begingroup\$

C, 55 bytes

i;f(char*s){i=atol(s);printf(i?"%c":"%d",64^(i?i:*s));}
\$\endgroup\$
4
\$\begingroup\$

Perl 6, 25 bytes

{+$_??chr $_+64!!.ord-64}

Explanation:

# bare block lambda with implicit parameter of 「$_」
{
    +$_         # is the input numeric
  ??
    chr $_ + 64 # if it is add 64 and get the character
  !!
    $_.ord - 64 # otherwise get the ordinal and subtract 64
}

Example:

say ('A'..'Z').map: {+$_??chr $_+64!!.ord-64}
# (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26)

say (1..26).map: {+$_??chr $_+64!!.ord-64}
# (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
\$\endgroup\$
  • 2
    \$\begingroup\$ Even though the syntax is so different, that same mechanism is the same number of bytes in Perl 5: perl -pe '$_=/\d/?chr$_+64:-64+ord'! \$\endgroup\$ – Dom Hastings Aug 16 '16 at 7:39
4
\$\begingroup\$

Mathematica 54 41 Bytes

With an absolutely clever suggestion from LegionMammal978 that saves 13 bytes.

If[#>0,FromLetterNumber,,LetterNumber]@#&

If[#>0,FromLetterNumber,,LetterNumber] serves the sole purpose of deciding whether to apply FromLetterNumber or LetterNumber to the input.

#>0 will be satisfied if the input, #, is a number, in which case FromLetterNumberwill be selected.

However #>0 will be neither true nor false if # is a letter, and LetterNumber will be selected instead.


If[#>0,FromLetterNumber,,LetterNumber]@#&["d"]

4


If[#>0,FromLetterNumber,,LetterNumber]@#&[4]

d


In Mathematica, FromLetterNumber and LetterNumber will also work with other alphabets. This requires only a few more bytes.

If[# > 0, FromLetterNumber, , LetterNumber][#, #2] &[4, "Greek"]
If[# > 0, FromLetterNumber, , LetterNumber][#, #2] &[4, "Russian"]
If[# > 0, FromLetterNumber, , LetterNumber][#, #2] &[4, "Romanian"]

δ
г
b

If[# > 0, FromLetterNumber, , LetterNumber][#, #2] &[δ, "Greek"]
If[# > 0, FromLetterNumber, , LetterNumber][#, #2] &[г, "Russian"]
If[# > 0, FromLetterNumber, , LetterNumber][#, #2] &[b, "Romanian"]

4
4
4

\$\endgroup\$
  • 1
    \$\begingroup\$ Some golfing, bringing it to 41 bytes: If[#>0,FromLetterNumber,,LetterNumber]@#& \$\endgroup\$ – LegionMammal978 Aug 17 '16 at 1:04
  • \$\begingroup\$ I interpret your suggestion as: If[#>0,FromLetterNumber[#],LetterNumber@#]‌&. Although If[#>0,FromLetterNumber[#],LetterNumber@#]‌&[4] works, If[#>0,FromLetterNumber[#],LetterNumber@#]‌&["c"] does not. It apparently cannot resolve "c">0. Did I misunderstand? \$\endgroup\$ – DavidC Aug 17 '16 at 7:27
  • \$\begingroup\$ The double ,, is intentional, and so is the exterior @#; it evaluates as If[# > 0, FromLetterNumber, Null, LetterNumber][#]&, which uses the 4-argument form of If (look it up). \$\endgroup\$ – LegionMammal978 Aug 17 '16 at 23:40
  • \$\begingroup\$ Amazing how the 4-argument form of If works. \$\endgroup\$ – DavidC Aug 18 '16 at 1:13
3
\$\begingroup\$

PHP, 49 41 40 bytes

<?=+($i=$argv[1])?chr($i+64):ord($i)-64;

I do not think there is a good alternative to is_numeric right?

This is executed from command line ($argv[1] is the first variable given)

Thanks to:

@insertusernamehere: Golfed 8 bytes. Replacing is_numeric($i=$argv[1]) with 0<($i=$argv[1]).This works because (int)"randomLetter" == 0.

@manatwork: Reduced with 1 byte. Replace 0< with +. What happens in this case is that the + signal will cast the "Z" (or whatever letter) to an 0. This will result in false. Therefor any letter is always false and a number is always true.

\$\endgroup\$
  • 2
    \$\begingroup\$ Using 0<($i=$argv[1]) instead of is_numeric($i=$argv[1]) saves you 8 bytes. \$\endgroup\$ – insertusernamehere Aug 16 '16 at 7:35
  • 1
    \$\begingroup\$ Continuing on that idea: 0<+. \$\endgroup\$ – manatwork Aug 16 '16 at 10:39
2
\$\begingroup\$

Python 2, 61 bytes

i=raw_input()
try:o=chr(int(i)+64)
except:o=ord(i)-64
print o

Yes I could switch to Python 3 for input

\$\endgroup\$
  • \$\begingroup\$ Use input() nevertheless and change int(i) to i. \$\endgroup\$ – Leaky Nun Aug 15 '16 at 22:45
  • \$\begingroup\$ Then character inputs don't work. \$\endgroup\$ – Karl Napf Aug 15 '16 at 22:46
  • 2
    \$\begingroup\$ Take input as "A" \$\endgroup\$ – Leaky Nun Aug 15 '16 at 22:49
  • 3
    \$\begingroup\$ That's lame. A or nothing. \$\endgroup\$ – Karl Napf Aug 15 '16 at 22:49
  • \$\begingroup\$ You could knock a few bytes off by reformulating it as a function: line 1: def f(i):, line 2: <space> try:o=chr(i+64), line 3 <space> otherwise unchanged, line 4: <space> return o In that form, it would work in either Python 2 or Python 3 \$\endgroup\$ – cdlane Aug 16 '16 at 5:28
2
\$\begingroup\$

C#, 32 bytes

n=>(n^=64)>26?(object)(char)n:n;

Casts to Func<int, object>.

Input: char implicitely converts to int so can be called with int (1-26) or char ('A'-Z').

Output: Either a char or int.

\$\endgroup\$
2
\$\begingroup\$

Befunge-98*, 19 bytes

&:39*\`'@\j;+,@;-.@

Because the question said you'll receive a 1-26 or an A-Z I assumed this meant the number 26 or the character A-Z. Most interprets struggle with entering alt-codes, so it is easier to use & and enter values like 26 for 26 or 90 for 'Z', as opposed to ~.

Pseudo-code

int c = get stdin
push the value of 27
bool is_number =  27 > c
push the value of `@` (64)
if is_number == 1
   jump to adding 64 to c //putting it the ASCII range
   print as ASCII
   end
else
   jump to subtracting 64 from c //putting it in the numerical range
   print as number
   end

Test it out (on Windows) here!

*This is technically Unefunge-98 because it only uses 1 dimension, but that name might be unfamiliar.

\$\endgroup\$
2
\$\begingroup\$

Befunge 93, 144 90 66 54 36 19 bytes

Not 100% sure if this is allowed, but if you are allowed to type A as 65, B as 66, etc., then (for [my] convenience's sake):

&:"@"`"@"\#. #-_+,@

Otherwise, at 36 bytes:

~:0\"A"-`#v_88*-.@
**~28*++,@>68*-52

(Thanks to tngreene for the suggestions!)

~:0\567+*-`#v_88*-.>$28*+,@
52**\28*++,@>~:0`!#^_\68*-

(Thanks to Sp3000 for saving 12 bytes by rearranging!)

~:0\567+*-`#v_88*-.>$82*+,@
            >~:0`!#^_\68*-52**\28*++,@


v                   >$28*+,@
             >~:0`!#^_\68*-52**\28*++,@
>~:0\567+*-`#^_88*-.@


v                    >$28*+,@
~           >11g~:0`!|
1                    >\68*-52**\28*++,@
1
p           
>011g567+*-`|
            >11g88*-.@

Ungolfed:

v                       >$ 28* + , @
                 >~:0 `!|
                        >\ 68* - 52* * \ 28* + + , @
>~:0\ 5 67+ * - `|
                 >88* - . @

This is my first working Befunge program ever, and I feel the need to golf this further. Any help would be greatly appreciated.

You can test Befunge code here.

\$\endgroup\$
  • 1
    \$\begingroup\$ Passing quick glance comment: Befunge wraps around, so you can move the last 12 chars of the second line to the front and get 52**\28*++,@>~:0`!#^_\68*- \$\endgroup\$ – Sp3000 Aug 16 '16 at 14:20
  • \$\begingroup\$ @Sp3000, oh I did't notice that. Thanks! \$\endgroup\$ – Daniel Aug 16 '16 at 14:25
  • \$\begingroup\$ Congratulations on your first program ever! One thing to consider would be to generate large numbers by pushing ASCII values in a string. Compare 567+* to "A". Also, don't forget about g and p instructions for reusing a value instead of having to build it up repeatedly. Also, I couldn't find any input that would take the IP to the branch >$ 28* + , @. What is this for? Are you sure its needed? \$\endgroup\$ – tngreene Aug 17 '16 at 20:03
  • \$\begingroup\$ Lastly, I admire your dedication to parsing "26" or "08". Your method, as I read it, involves much symbol<->number conversion math, as in ('2' to 2 back to '2'). Having your first and second inputs as numbers before you start comparing them could decrease the amount of ASCII-arithmetic you're doing. Alternatively, maybe there is a way to efficiently deal with inputs as symbols ('2' as in '2'), no conversion to numbers needed! \$\endgroup\$ – tngreene Aug 17 '16 at 20:03
  • \$\begingroup\$ @tngreene, Integer inputs <10 go to the branch $28*+,@ whereas those >=10 go to the other one. This was done ultimately because you cannot read over the input more than once as far as I know. \$\endgroup\$ – Daniel Aug 17 '16 at 20:11
2
\$\begingroup\$

Java, 104 98 97 83 54 53 51 50 30 bytes

x->(x^=64)>64?(char)x+"":x+"";

Test Program:

IntFunction<String> f = x -> (x ^= 64) > 64 ? (char) x + "" : x + "";
out.println(f.apply('A')); // 1
out.println(f.apply('Z')); // 26
out.println((f.apply(1))); // A
out.println((f.apply(26))); //Z
\$\endgroup\$
  • 1
    \$\begingroup\$ You can drop about 20 bytes by using a ternary operator like so: return(s.matches("\\d+")?(char)(Integer.parseInt(s)+64)+"":(s.charAt(0)-64)+""); \$\endgroup\$ – yitzih Aug 16 '16 at 14:55
  • \$\begingroup\$ you can remove casting to int as well, which allows you yo reduce by 7 bytes. \$\endgroup\$ – user902383 Aug 16 '16 at 15:47
  • \$\begingroup\$ The program does not take any input. The program does not give any output. There even is no program! \$\endgroup\$ – Nicolas Barbulesco Aug 20 '16 at 22:18
  • \$\begingroup\$ @NicolasBarbulesco You are not required to write a full program unless stated otherwise. \$\endgroup\$ – Shaun Wild Aug 22 '16 at 8:14
1
\$\begingroup\$

Fourier, 30 bytes

I~F<64{1}{F+64a}F>64{1}{F-64o}

Try it online!

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

R, 73 bytes

f=function(x){L=LETTERS;if(is.numeric(x)){i=L[(x)]}else{i=which(L==x)};i}
\$\endgroup\$
  • \$\begingroup\$ No need for f=, and you cloud try to use the ifelse function to maybe golf out some bytes ! \$\endgroup\$ – Frédéric Aug 21 '16 at 15:17
1
\$\begingroup\$

PowerShell v2+, 42 bytes

param($n)([char](64+$n),(+$n-64))[$n-ge65]

Takes input $n (as an integer or an explicit char) and uses a pseudo-ternary to choose between two elements of an array. The conditional is $n-ge65 (i.e., is the input ASCII A or greater). If so, we simply cast the input as an int and subtract 64. Otherwise, we add 64 to the input integer, and cast it as a [char]. In either case, the result is left on the pipeline and printing is implicit.

Examples

PS C:\Tools\Scripts\golfing> ([char[]](65..90)|%{.\alphabet-to-number.ps1 $_})-join','
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26

PS C:\Tools\Scripts\golfing> (1..26|%{.\alphabet-to-number.ps1 $_})-join','
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
\$\endgroup\$
1
\$\begingroup\$

MATL, 10 bytes

6WZ~t42>?c

Explanation:

6W              % 2**6 = 64, but golfier looking
  Z~            % bit-wise XOR with input
    t42>?       % if result is greater than 42
         c      % convert it to a character 
                % else, don't

Try it online! with numeric inputs.
Try it online! with alphabetic inputs.

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

Python 3, 49 48 53 50 bytes

Somehow I got the byte count wrong ;_; thanks dahuglenny

isalpha is shorter than isnumeric

lambda x:x.isalpha()and ord(x)-64or chr(int(x)+64)

takes input as string, which could be a letter or number

\$\endgroup\$
  • 1
    \$\begingroup\$ You can remove the space between x.isnumeric() and else to save one byte. \$\endgroup\$ – acrolith Aug 15 '16 at 23:34
1
\$\begingroup\$

Java, 61 bytes

int f(char c){return c^64;}char f(int i){return(char)(i^64);}

Ungolf'd:

int f(char c) {
    return c^64;
}

char f(int i) {
    return (char) (i^64);
}

Calling f('A') invokes the first function, retuning an int 1; calling f(1) invokes the second function, returning the char "A".

\$\endgroup\$
  • \$\begingroup\$ ` you have to take a number as input and output the corresponding letter of the alphabet, and vice versa. (1 <=> A, 2 <=> B) etc.` I don't think a set of functions qualifies for this. \$\endgroup\$ – Shaun Wild Aug 17 '16 at 8:33
  • 1
    \$\begingroup\$ @SeanBean it's a function overload. \$\endgroup\$ – NoOneIsHere Aug 18 '16 at 4:50
  • \$\begingroup\$ That takes no input. That gives no output. There is no program! \$\endgroup\$ – Nicolas Barbulesco Aug 20 '16 at 22:22
1
\$\begingroup\$

Javascript 86 77 66 60 bytes

i=>typeof i<'s'?String.fromCharCode(i+64):i.charCodeAt(0)-64
  • saved 7 bytes after comments about using arrow functions
  • saved another 11 bytes by removing return / brackets as noted by @manatwork
  • saved another 6 bytes thanks to @manatwork
\$\endgroup\$
  • 1
    \$\begingroup\$ Use arrow functions \$\endgroup\$ – Bald Bantha Aug 16 '16 at 16:52
  • \$\begingroup\$ @BaldBantha cheers, changed it :-) \$\endgroup\$ – Dylan Meeus Aug 17 '16 at 8:11
  • \$\begingroup\$ No need for return statement: i=>typeof i=='number'?String.fromCharCode(i+64):i.charCodeAt(0)-64. \$\endgroup\$ – manatwork Aug 17 '16 at 8:22
  • \$\begingroup\$ @manatwork Cheers! \$\endgroup\$ – Dylan Meeus Aug 17 '16 at 8:24
  • 1
    \$\begingroup\$ According to task description the typeof input can be only "number" or "string". So no need to check for =='number', <'s' will also do it. \$\endgroup\$ – manatwork Aug 17 '16 at 8:31
1
\$\begingroup\$

Brainfuck, 445 Characters

More a proof of concept than a golfed code. Requires Unsigned, Non-wrapping Brainfuck.

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

With Comments

,[>+>+<<-] Firstly Duplicate it across two buffers
>[<+>-] Move the second buffer back to the first buffer
>>++[->++++++<]>[-<<<+++++>>>] Establish 60 in the second buffer
<<<<
Compare Buffers 1 and 2
[->-<]
>
[ If there's still data in buffer 2
, Write the value in the units column to buffer two
<
++++
[->------------<] Subtract 12 from the units buffer
++++
[->>------------<<] Subtract 12 from the tens buffer
[-<<++++++++++>>] Multiply buffer three by ten into buffer 1
>
[-<+>] Add the units
>
[-<<++++++++++>>] Add the tens
>++ Add 65 to the buffer
[->++++++<]>+
[-<+++++>]
<- Actually we need 64 because A is 1
[-<<<+>>>] Add 64 to the first buffer
<<<
. Print the new letter
> Move to blank buffer
]
>
[ Otherwise we're a letter
[-<+<+>>] Copy it back over the first two buffers
>++ Write 64 to the buffer
[->++++++<]>+
[-<+++++>]
<-
[-<<->>] Subtract 64 from the letter
<<[->+>+<<]
>>>++++++++++< Copy pasted Division step x = current buffer y = 10 rest of the buffers are conveniently blank

+
[>[->+>+<<]>[-<<-[>]>>>[<[-<->]<[>]>>[[-]>>+<]>-<]<<]>>>+<<[-<<+>>]<<<]>>>>>[-<<<<<+>>>>>]<<<<<
-
[->+>+<<]
>[-<++++++++++>]
<[-<->]
++++
[-<++++++++++++>]
++++
[->>++++++++++++<<]
>>.<<<.>
] 
\$\endgroup\$
1
\$\begingroup\$

ASM: 10 bytes

3C 40 77 04 2C 40 EB 02 04 40

Explanation: This is the assembled representation of a program that does exactly what is asked. It is not fully functional, because it needs some directives, but if it is added to the code segment of an assembly program it should work. It receives the input in the AL register, and if its a letter it subtracts 40h from the ASCII code value, leaving just the number(i.e B=42h, 42h-40h=2h). If the input is a number it does the opposite procedure by adding 40h. It leaves the result in the AL register. Below is the assembly source code

cmp al,40h
ja letter_to_number
sub al,40h
jmp continue
letter_to_number: add ax,40h
continue:

Also, if you convert all the other answers to machine code, I am positive that mine would be the smallest.

\$\endgroup\$
  • \$\begingroup\$ I think there are a few issues: 77 02 2C should be 77 **04** 2C; the sub and add are backwards. \$\endgroup\$ – ceilingcat Aug 23 '16 at 0:55
  • \$\begingroup\$ I applied the above corrections and created a "function" that you can call from a C program on an x86_64 machine. #define F(x) ((int(*)(int))"\x89\xf8\x3c\x40\x76\4\x2c\x40\xeb\2\4\x40\xc3")(x) \$\endgroup\$ – ceilingcat Aug 23 '16 at 0:58
  • \$\begingroup\$ What type of assembly is this? \$\endgroup\$ – mbomb007 Oct 26 '16 at 14:39
  • \$\begingroup\$ Turbo Assembler \$\endgroup\$ – 6a75616e Oct 28 '16 at 19:15
0
\$\begingroup\$

R, 75 bytes

f=function(l,a=which(LETTERS==l))ifelse(length(a),a,LETTERS[as.numeric(l)])

This function technically has two inputs. But the second one is merely there because by setting a through a default, I can get rid of 2 bytes. Give the function a quoted letter or number, or just a number, and it will return the opposite.

Explanation: a tests if it is a letter and which one it is. If it isn't it pulls the relevant letter.

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

JavaScript (ES6), 49 bytes

s=>s>0?String.fromCharCode(s|64):parseInt(s,36)-9

Accepts integers in either number or string format, always return integers as a number. 42 bytes if I can return lower case and don't need to handle integers in string format:

s=>s>0?(s+9).toString(36):parseInt(s,36)-9
\$\endgroup\$
0
\$\begingroup\$

Python 2, 45 44 bytes

Code:

lambda x:chr(x+64)if`x`[0]>"("else ord(x)-64
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.