| Bytes | Lang | Time | Link |
|---|---|---|---|
| 046 | AWK | 250806T151738Z | xrs |
| 331 | Bespoke | 250730T054327Z | Josiah W |
| 018 | APLNARS | 250308T162519Z | Rosario |
| 062 | Tcl | 170525T183719Z | sergiol |
| 001 | Thunno 2 | 230622T173908Z | The Thon |
| 019 | ><> Fish | 230314T131231Z | emirps |
| 022 | Julia 1.0 | 230328T201106Z | Ashlin H |
| 015 | Stax | 230328T151259Z | emirps |
| 050 | Lua | 230313T192451Z | emirps |
| 029 | q | 230327T184447Z | eohara-i |
| 010 | Japt | 190912T085924Z | Shaggy |
| 004 | Vyxal | 230312T120733Z | The Thon |
| 007 | Jelly | 201225T223314Z | caird co |
| 067 | Forth gforth | 190912T145232Z | reffu |
| 048 | Commodore BASIC C64/128/TheC64 | 190912T115054Z | Shaun Be |
| 062 | Haskell | 190912T122505Z | flawr |
| 013 | MathGolf | 190912T120919Z | Kevin Cr |
| 009 | 05AB1E legacy | 190912T101338Z | Kevin Cr |
| 010 | Brachylog | 190912T041142Z | Unrelate |
| 026 | q/kdb+ | 170817T152539Z | mkst |
| 027 | C++ gcc | 170817T154550Z | Karl Nap |
| 033 | Excel | 170817T145203Z | Wernisch |
| 033 | DASH | 161029T041138Z | Mama Fun |
| 058 | Racket | 161029T022618Z | rnso |
| 010 | ASM | 160816T233747Z | 6a75616e |
| 030 | Java | 160816T094531Z | Shaun Wi |
| 445 | Brainfuck | 160818T061911Z | ATaco |
| 041 | Mathematica 41 Bytes | 160816T084959Z | DavidC |
| 019 | Befunge 93 | 160816T100535Z | Daniel |
| 041 | JavaScript | 160817T140323Z | user2428 |
| 044 | q | 160817T105739Z | skeevey |
| 060 | Javascript | 160816T130144Z | Dylan Me |
| 061 | Java | 160817T051148Z | AJNeufel |
| 072 | SQF | 160817T003356Z | Οurous |
| 050 | Python 3 | 160815T232430Z | Destruct |
| 010 | MATL | 160816T215445Z | beaker |
| 051 | Pure Bash | 160816T010020Z | Digital |
| 031 | Ruby | 160815T220656Z | Value In |
| 019 | Befunge98* | 160816T153141Z | tngreene |
| 062 | R | 160816T123232Z | Forgotte |
| 014 | Dyalog APL | 160816T133628Z | Adá |
| 042 | PowerShell v2+ | 160816T125919Z | AdmBorkB |
| 073 | R | 160816T111415Z | Mamie |
| 040 | PHP | 160816T065701Z | Jeroen |
| 053 | Gema | 160816T103443Z | manatwor |
| 007 | Actually | 160816T014506Z | user4594 |
| 043 | Python 3 | 160816T044845Z | cdlane |
| 013 | Pyth | 160816T063232Z | Leaky Nu |
| 025 | Perl 6 | 160816T024524Z | Brad Gil |
| 032 | C# | 160816T021736Z | milk |
| 032 | Cheddar | 160815T231703Z | Downgoat |
| 055 | C | 160816T003558Z | orlp |
| 038 | Python 2 | 160816T002645Z | Dennis |
| 044 | Python 2 | 160815T234444Z | Adnan |
| 030 | Fourier | 160815T215359Z | Beta Dec |
| 049 | JavaScript ES6 | 160815T233911Z | Neil |
| 026 | Erlang | 160815T220555Z | a spaghe |
| 054 | Haskell | 160815T231431Z | nimi |
| 008 | 2sable | 160815T220341Z | Adnan |
| 075 | R | 160815T225245Z | user5957 |
| 061 | Python 2 | 160815T224040Z | Karl Nap |
Bespoke, 331 bytes
find A-B-C given an integer/convert ABC to number
I read it in ASCII
assuming an integer is put in now,my strategy builds digits
a decimal has ten as a number denoting values in places
add to a particular alphabet character
integers from ASCII:we do some division formula
finding out in a mod by thirty-twos whatever size counts it
Bespoke has a command to take in numerical input, but it errors out if the input given is not a number. So I was left in the unenviable position of writing a routine to convert string input to a number. Just about the best thing I can say about it is that it works.
APL(NARS), 18 chars
{''≡0↑⍵:⎕A⍳⍵⋄⍵⊃⎕A}
It suppose input only one letter in the range A..Z or a numeric range 1..26.
In my apl file it is said that ''≡0↑⍵ return 1 if in ⍵ there is one string or one letter
else return 0.
Test:
{''≡0↑⍵:⎕A⍳⍵⋄⍵⊃⎕A}1
A
{''≡0↑⍵:⎕A⍳⍵⋄⍵⊃⎕A}'B'
2
{''≡0↑⍵:⎕A⍳⍵⋄⍵⊃⎕A}'Z'
26
{''≡0↑⍵:⎕A⍳⍵⋄⍵⊃⎕A}26
Z
Tcl, 62 bytes
Not a champion by any measure, but I had to do it.
puts [expr {[string is di $a]?[format %c $a+64]:[scan $a %c]}]
Thunno 2, 1 byte
Ä
Overloaded built-in that works on both numbers (1 -> A, 2 -> B, ...) and letters (A -> 1, B -> 2, ...).
><> (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.
Lua, 76 53 50 bytes
x=...y=x:byte()print(64<y and y-64or x.char(x+64))
Explanation
x=... -- unpack command line arguments into x
y=x:byte()
print(
64<y -- if the first byte of the string > 64:
and y-64 -- print the first byte-64
or x.char(x+64) -- else, print chr(x+64)
)
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
Japt, 11 10 bytes
¤?UdI:InUc
¤?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
Vyxal, 12 4 bytes
øAɾt
- -8 thanks to @lyxal
Explanation
Of course there's a built-in
øAɾt # Implicit input
øA # Letter to number or number to letter
ɾ # Range or uppercase (overloaded)
t # Last item
Old:
‹kA:?c[?ḟ›|i # Implicit input
‹ # Decrement (no effect on strings)
kA: # Push uppercase alphabet and duplicate
?c # Is the input alphabetic?
[ # If it is:
?ḟ # Find the 0-based index of the input in the alphabet
› # And increment it to make it 1-based
| # Otherwise:
i # Index input-1 into the alphabet
Jelly, 7 bytes
i@ịe?ØA
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
Forth (gforth), 67 bytes
: f 2dup s>number? if d>s 64 + emit else 2drop d>s c@ 64 - . then ;
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
Commodore BASIC (C64/128/TheC64, VIC-20, PET) - 90 48 (tokenized) bytes
0inputa$:ifval(a$)>0then?chr$(64+val(a$)):end
1?asc(a$)-64
Explanation and screen shot later.
Haskell, 63 62 bytes
This uses the rule that wrapping the output in an optional type is allowed. We just use the standard lookup function to search in a list of tuples that define the input/output relationship.
(`lookup`zip(c++n)(n++c))
c=pure<$>['A'..'Z']
n=show<$>[1..26]
MathGolf, 13 bytes
▄l!╧¿É$♦-É♦+$
Explanation:
l # Push the input as string
! # Convert it to lowercase
¿ # If
▄ # the lowercase alphabet
╧ # contains this value:
É # Execute the following three operations:
$ # Convert the (implicit) input to its unicode value
♦- # Subtract 64
# (Implicit else:)
É # Execute the following three operations:
♦+ # Add 64
$ # And convert it to the character with this unicode value
# (after which the entire stack joined together is output implicitly)
05AB1E (legacy), 9 bytes
di.bëÇ64-
Try it online or verify all test cases.
Also works in the new version of 05AB1E instead of the legacy, but the Ç wraps in a list: Try it online or verify all test cases.
Explanation:
di # Legacy 05AB1E version: If the (implicit) input is an integer:
# New 05AB1E version: If the (implicit) input is a positive (>= 0) integer:
.b # Builtin to convert the integer to its 1-based uppercase alphabetic letter
ë # Else:
Ç # Get the codepoint of the uppercase letter
64- # And subtract 64 to convert it to its 1-based index
# (after which the result is output implicitly)
Brachylog, 10 bytes
;.L∧Ṇi₁pL∧
The core of this is only 4 bytes, but the I/O for that version is a bit less than sane.
q/kdb+, 28 26 bytes
Solution:
{((?;@) -7=(@)x)[`,.Q.A]x}
Examples:
q){((?;@) -7=(@)x)[`,.Q.A]x}1
"A"
q){((?;@) -7=(@)x)[`,.Q.A]x}2
"B"
q){((?;@) -7=(@)x)[`,.Q.A]x}26
"Z"
q){((?;@) -7=(@)x)[`,.Q.A]x}"A"
1
q){((?;@) -7=(@)x)[`,.Q.A]x}"B"
2
q){((?;@) -7=(@)x)[`,.Q.A]x}"Z"
26
Explanation:
Check the type of the input, if it's a long (-7h) then use the apply @ operator to index into the alphabet .Q.A, otherwise use ? to lookup the input in the alphabet. Note that we add null to the start of the alphabet to fake the 1-indexing:
{((?;@) -7h=type x)[`,.Q.A;x]} / ungolfed solution
{ } / lambda function
[ ; ] / apply a function to this
`,.Q.A / join ` (null) to the string "ABC...XYZ"
x / implicit input (e.g. "A" or 13)
( ) / do all this together
-7h=type x / is the type of parameter x a long (-7)
(?;@) / ? (lookup) if true, @ (index) if false
Notes:
- -2 bytes by using indexing instead of switch statement.
C++ (gcc), 27 bytes
As unnamed generic lambda, accepting char or int and returning via reference parameter (input=output)
[](auto&r){r+=r<64?64:-64;}
Excel, 33 bytes
=IFERROR(CHAR(A1+64),CODE(A1)-64)
DASH, 33 bytes
@[="num"type#0?fc +64#0?-(tc#0)64
Woo-hoo, first PPCG answer in my new functional programming language!
Installation and other info is all on the Github.
Usage
(@[="num"type#0?fc +64#0?-(tc#0)64])"A" #. strings
(@[="num"type#0?fc +64#0?-(tc#0)64])26 #. numbers
Explanation
@ #. lambda
[ #. start of conditional block
= "num" (type #0) #. check if argument is a number
? fc (+ 64 #0) #. if so, add 64 to argument and convert from charcode
? - (tc #0) 64 #. otherwise, convert argument to charcode and subtract 64
#. we can actually leave off the closing bracket for now
Racket 58 bytes
(if(char? i)(-(char->integer i)64)(integer->char(+ 64 i)))
Ungolfed:
(define (f i)
(if (char? i)
(-(char->integer i) 64)
(integer->char (+ 64 i))))
Testing:
(f 1)
(f 3)
(f #\A)
(f #\D)
Output:
#\A
#\C
1
4
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.
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
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
+
[>[->+>+<<]>[-<<-[>]>>>[<[-<->]<[>]>>[[-]>>+<]>-<]<<]>>>+<<[-<<+>>]<<<]>>>>>[-<<<<<+>>>>>]<<<<<
-
[->+>+<<]
>[-<++++++++++>]
<[-<->]
++++
[-<++++++++++++>]
++++
[->>++++++++++++<<]
>>.<<<.>
]
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
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.
JavaScript, 41 bytes
x=>-x?(9+x).toString(36):parseInt(x,36)-9
q, 44 bytes
Reads from stdin
{$[0N~i:"J"$x;1+.Q.A?x 0;.Q.A i-1]}[read0 0]
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
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".
SQF, 72 bytes
Using the function-as-a-file format:
i=_this;if((str i)==i)then{toString[i+64]}else{((toArray i)select 0)-64}
Call as: ARG call NAME_OF_COMPILED_FUNCTION
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
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.
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]}
Ruby, 47 39 + n flag = 40 bytes 33 34 31 bytes
n flag = 40 bytesAnonymous function. Uses an exception handling trick like in @KarlNapf's Python solution.
-3 bytes from @manatwork
->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
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.
R, 62 Bytes
f=function(x)ifelse(is.finite(x),LETTERS[x],which(LETTERS==x))
Takes the input x and checks if it is finite, which is one byte shorter than checking if it is numeric and equivalent in this case. If yes, output the letter, if no, output the position in the alphabet equal to the character input.
Dyalog APL, 14 bytes
{0::⎕A⍳⍵
⍵⊃⎕A}
0:: if any error happens:
⎕A⍳⍵ return the index of the argument in the alphabet
⍵⊃⎕A use the argument to pick an element from the alphabet (errors if the argument is character)
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
R, 73 bytes
f=function(x){L=LETTERS;if(is.numeric(x)){i=L[(x)]}else{i=which(L==x)};i}
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.
Gema, 53 characters
<K>=@sub{@char-int{$0};64}
<D>=@int-char{@add{$0;64}}
Sample run:
bash-4.3$ gema '<K>=@sub{@char-int{$0};64};<D>=@int-char{@add{$0;64}}' <<< 'M'
13
bash-4.3$ gema '<K>=@sub{@char-int{$0};64};<D>=@int-char{@add{$0;64}}' <<< '13'
M
Actually, 7 bytes
ú' +ûEí
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í
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 != ...
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)
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.
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.
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.
C, 55 bytes
i;f(char*s){i=atol(s);printf(i?"%c":"%d",64^(i?i:*s));}
Python 2, 45 44 bytes
Code:
lambda x:chr(x+64)if`x`[0]>"("else ord(x)-64
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
Erlang, 26 bytes
f([X])->X-64;f(X)->[X+64].
One of the few times where Erlang's string behavior is useful.
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).
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!.
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.
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