29
\$\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\$
15
  • 1
    \$\begingroup\$ Why duplicate? O.o It is not equal. \$\endgroup\$
    – Chad
    Commented Aug 15, 2016 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
    Commented Aug 15, 2016 at 21:55
  • 2
    \$\begingroup\$ Will we receive 26 as an integer or "26" as a string, or are both allowed? \$\endgroup\$
    – Leaky Nun
    Commented Aug 15, 2016 at 22:38
  • 2
    \$\begingroup\$ Does it have to be uppercase, or is lowercase acceptable instead? \$\endgroup\$
    – user45941
    Commented Aug 16, 2016 at 1:45
  • 1
    \$\begingroup\$ Seriously, another alphabet challenge? ( ͡° ͜ʖ ͡°) \$\endgroup\$
    – shooqie
    Commented Aug 16, 2016 at 7:11

58 Answers 58

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\$
2
9
\$\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\$

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

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\$
2
  • 1
    \$\begingroup\$ Can't you delete ,? Which bytes are without ,? You don't need to print a new line. \$\endgroup\$
    – Chad
    Commented Aug 16, 2016 at 11:30
  • \$\begingroup\$ @Chad Nope, that won't work for numeric inputs :( \$\endgroup\$
    – Adnan
    Commented Aug 16, 2016 at 17:33
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\$
3
  • \$\begingroup\$ I'm getting syntax error when trying to run on ideone, can you tell how to test? \$\endgroup\$
    – Leibrug
    Commented Aug 16, 2016 at 6:05
  • \$\begingroup\$ @Leibrug oops! It's fixed now \$\endgroup\$
    – Value Ink
    Commented Aug 16, 2016 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
    Commented Aug 16, 2016 at 11:31
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\$
2
  • \$\begingroup\$ "%s"%n==n saves 2 bytes \$\endgroup\$
    – Leaky Nun
    Commented Aug 16, 2016 at 0:54
  • \$\begingroup\$ @LeakyNun oh that's smart! I was tryomg of doing "%d"%n==n but that wasn't working :/ \$\endgroup\$
    – Downgoat
    Commented Aug 16, 2016 at 0:57
5
\$\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\$
4
  • 1
    \$\begingroup\$ Some golfing, bringing it to 41 bytes: If[#>0,FromLetterNumber,,LetterNumber]@#& \$\endgroup\$ Commented Aug 17, 2016 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
    Commented Aug 17, 2016 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\$ Commented Aug 17, 2016 at 23:40
  • \$\begingroup\$ Amazing how the 4-argument form of If works. \$\endgroup\$
    – DavidC
    Commented Aug 18, 2016 at 1:13
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\$
1
  • 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\$ Commented Aug 16, 2016 at 7:39
3
\$\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\$
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
  • 2
    \$\begingroup\$ Using 0<($i=$argv[1]) instead of is_numeric($i=$argv[1]) saves you 8 bytes. \$\endgroup\$ Commented Aug 16, 2016 at 7:35
  • 1
    \$\begingroup\$ Continuing on that idea: 0<+. \$\endgroup\$
    – manatwork
    Commented Aug 16, 2016 at 10:39
3
\$\begingroup\$

Excel, 33 bytes

=IFERROR(CHAR(A1+64),CODE(A1)-64)
\$\endgroup\$
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\$
5
  • \$\begingroup\$ Use input() nevertheless and change int(i) to i. \$\endgroup\$
    – Leaky Nun
    Commented Aug 15, 2016 at 22:45
  • \$\begingroup\$ Then character inputs don't work. \$\endgroup\$
    – Karl Napf
    Commented Aug 15, 2016 at 22:46
  • 2
    \$\begingroup\$ Take input as "A" \$\endgroup\$
    – Leaky Nun
    Commented Aug 15, 2016 at 22:49
  • 3
    \$\begingroup\$ That's lame. A or nothing. \$\endgroup\$
    – Karl Napf
    Commented Aug 15, 2016 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
    Commented Aug 16, 2016 at 5:28
2
\$\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\$
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\$
10
  • 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
    Commented Aug 16, 2016 at 14:20
  • \$\begingroup\$ @Sp3000, oh I did't notice that. Thanks! \$\endgroup\$
    – Daniel
    Commented Aug 16, 2016 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
    Commented Aug 17, 2016 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
    Commented Aug 17, 2016 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
    Commented Aug 17, 2016 at 20:11
2
\$\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\$
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\$
4
  • 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
    Commented Aug 16, 2016 at 14:55
  • \$\begingroup\$ you can remove casting to int as well, which allows you yo reduce by 7 bytes. \$\endgroup\$
    – user902383
    Commented Aug 16, 2016 at 15:47
  • \$\begingroup\$ The program does not take any input. The program does not give any output. There even is no program! \$\endgroup\$ Commented Aug 20, 2016 at 22:18
  • \$\begingroup\$ @NicolasBarbulesco You are not required to write a full program unless stated otherwise. \$\endgroup\$
    – Shaun Wild
    Commented Aug 22, 2016 at 8:14
2
\$\begingroup\$

Forth (gforth), 67 bytes

: f 2dup s>number? if d>s 64 + emit else 2drop d>s c@ 64 - . then ;

Try it online!

Input is passed as a string to the function

Code Explanation

: f             \ start a new word definition
  2dup          \ make a copy of the input string
  s>number?     \ attempt to convert to a number in base 10
  if            \ check if successful
    d>s         \ drop a number (don't need double-precision)
    64 + emit   \ add to 64 and output the ascii letter at that value
  else          \ if not successful (input was a letter)
    2drop d>s   \ get rid of garbage number conversion result and string-length
    c@ 64 - .   \ get ascii value of char, subtract 64 and output 
  then          \ end the if block
;               \ end the word definition
\$\endgroup\$
2
\$\begingroup\$

Jelly, 7 bytes

i@ịe?ØA

Try it online!

How it works

i@ịe?ØA - Main link. Takes an input A on the left
     ØA - Set the right argument to "ABC...XYZ"
    ?   - If statement:
   e    -   Condition: left in right; is A in the alphabet?
i@      -   If so: return the 1-index of A in the alphabet
  ị     -   Else: yield the A'th element of the alphabet
\$\endgroup\$
2
\$\begingroup\$

Japt, 11 10 bytes

¤?UdI:InUc

Try it

¤?UdI:InUc     :Implicit input of integer or string U
¤              :Convert to binary string if integer (always truthy)
¤              :Slice off first 2 character if string (always falsey)
 ?             :If truthy
  Ud           :  Get character at codepoint U
    I          :  After first adding 64
     :         :Else
      In       :  Subtract 64 from
        Uc     :  Codepoint of U
\$\endgroup\$
2
\$\begingroup\$

q, 29 bytes

{@[1+.Q.A?;x;{.Q.A y-1}[;x]]}

Find the index at which our letter occurs in the built-in list of uppercase letters

If it errors, index into the same list with the input instead

Adding and taking away ones as q indexing starts at 0, not 1

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to Code Golf! \$\endgroup\$ Commented Mar 27, 2023 at 18:45
  • \$\begingroup\$ And nice first answer! \$\endgroup\$
    – The Thonnu
    Commented Mar 27, 2023 at 19:14
2
\$\begingroup\$

><> (Fish), 27 24 21 19 bytes

<o+v?)0-}:"@":
n-$<

Try it with a number test case

Try it with a letter test case

Takes input on the stack, which is a default input method for stack-based languages, and outputs with error.

\$\endgroup\$
1
  • \$\begingroup\$ Happy to see others use my interpreter \$\endgroup\$
    – mousetail
    Commented Mar 21, 2023 at 9:05
1
\$\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\$
1
\$\begingroup\$

Fourier, 30 bytes

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

Try it online!

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

R, 73 bytes

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

Your Answer

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

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