22
\$\begingroup\$

Introduction

Every letter in the English alphabet can be represented as an ASCII code. For example, a is 97, and S is 83. As we all know, the formula for averaging two numbers \$x\$ and \$y\$ is \$\frac{x+y}{2}\$. I'm pretty sure you can see where this is going. Your challenge is to average two letters.

Challenge

Your program must take two letters as input, and output the average of the ASCII values in it. If the average is a decimal, you should truncate it.

  • Input will always be two ASCII letters. You can assume they will always be valid, but the case may vary. Basically, both letters will be in the range 97-122 or 65-90. The second letter will always have a greater ASCII value than the first. If your language has no method of input, you may take input from command line arguments or from a variable.
  • You must output the ASCII character signified by the average of the two numbers. As stated above, it should always be truncated to 0 decimal places. If your language has no method of output, you may store it in a variable. Exit codes and return values are considered valid output methods.

Example I/O

  • Input: A, C
    Output: B
  • Input: a, z
    Output: m
  • Input: d, j
    Output: g
  • Input: B, e
    Output: S
  • Input: Z, a
    Output: ]

Rules

This is , so shortest answer in bytes wins!

\$\endgroup\$
5
  • 2
    \$\begingroup\$ Please specify whether, for example, B e is a valid input. \$\endgroup\$ Commented Nov 10, 2019 at 19:53
  • 4
    \$\begingroup\$ Why is the output of the last example U? The value of B is 66 and the value of e is 101, which averages to 83.5, truncated to 83, which corresponds to S \$\endgroup\$ Commented Nov 10, 2019 at 23:05
  • 1
    \$\begingroup\$ If that example is correct, it will invalidate all of the existing answers. \$\endgroup\$
    – user85052
    Commented Nov 11, 2019 at 10:05
  • 1
    \$\begingroup\$ Sorry. I read an ASCII table wrong and got 69 for B, not 66. \$\endgroup\$
    – sugarfi
    Commented Nov 11, 2019 at 14:00
  • \$\begingroup\$ Could I enter for a non-ASCII compliant system? \$\endgroup\$ Commented Nov 25, 2019 at 17:28

93 Answers 93

9
\$\begingroup\$

C# (Visual C# Interactive Compiler), 20 bytes

a=>b=>(char)(a+b>>1)

Try it online!

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

x86-16 machine code, IBM PC DOS, 13 12 10 bytes

Binary (xxd):

00000000: a182 0002 c4d0 e8cd 29c3            ..........

Listing:

A1 0082     MOV  AX, [0082H]    ; load two chars into AH and AL from command line
02 C4       ADD  AL, AH         ; AL = AL + AH 
D0 E8       SHR  AL, 1          ; AL = AL / 2 
CD 29       INT  29H            ; write to console 
C3          RET                 ; return to DOS 

Standalone PC DOS executable. Input is via command line, output to console.

Example:

enter image description here

\$\endgroup\$
3
  • \$\begingroup\$ Only 5 bytes if written as a function :-) \$\endgroup\$
    – Cody Gray
    Commented Nov 12, 2019 at 18:57
  • \$\begingroup\$ @CodyGray only 4 bytes as a snippet or a MACRO. Not sure where the "line" is though... :) \$\endgroup\$
    – 640KB
    Commented Nov 13, 2019 at 17:22
  • \$\begingroup\$ Functions are always permitted, as are full programs, per house code golfing rules. Not sure about macros, though; interesting suggestion. Although avoiding that 1-byte RET is probably not going to be enough to make a difference most of the time... \$\endgroup\$
    – Cody Gray
    Commented Nov 14, 2019 at 0:53
7
\$\begingroup\$

Jelly, 4 bytes

OSHỌ

Try it online!

Explanation

OSHỌ  Main Link: takes (a, b)
O     (ord(a), ord(b))
 S    sum; ord(a) + ord(b)
  H   halve; (ord(a) + ord(b)) / 2
   Ọ  chr
\$\endgroup\$
1
7
\$\begingroup\$

Poetic, 163 bytes

software inside a computer
a robot+a man+a keypad+a plan=a PC
still,P.C.this,P.C.that?i await a day i crush a PC
resist P.C,Google,or an Apple
i do Linux,i suppose

Try it online!

Poetic is an esolang I made in 2018 for a class project. It's basically brainfuck with word-lengths instead of symbols.

(I actually use PC myself. 😉)

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Love the idea of this language. \$\endgroup\$
    – Jonah
    Commented Nov 12, 2019 at 21:55
7
\$\begingroup\$

R, 56 37 bytes

intToUtf8(mean(utf8ToInt(scan(,""))))

Try it online!

Description

  • intToUtf8() converts the average into its ASCII character.

  • mean() takes the average which is automatically truncated.

  • utf8ToInt() converts the inputs into two ASCII numbers.

  • scan() allows inputs.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to CGCC! A few tips: 1. You can use an anonymous function, so don't need the f=; 2. You don't need print; 3. In this case, it is actually shorter to take input with scan that to define a function; 4. floor is not needed: if you feed a non-integer numeric to intToUtf8, R truncates it automatically before converting to character. Also, TIO must have been experiencing issues when you tried it; the base package is included. All in all, your solution can be shortened to 37 bytes. \$\endgroup\$ Commented Nov 10, 2019 at 17:43
  • \$\begingroup\$ @RobinRyder Thanks for the reduction! \$\endgroup\$ Commented Nov 10, 2019 at 19:15
6
\$\begingroup\$

J, 17 bytes

(+/<.@%#)&.(3&u:)

Try it online!

  • (+/<.@%#) truncated average...
  • &. "Under", which applies a transform, then the verb it modifies -- truncated avg in this case -- then the inverse transform....
  • 3&u: convert to ascii byte integer.

That is, it converts each letter to its ascii number, gets the truncated average of those, and applies the inverse of "convert to ascii number", which takes an ascii number and returns a letter.

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

Ruby, 22 bytes

->a,b{""<<(a+b).sum/2}

Try it online!

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Huh. TIL the functionality of str << int. \$\endgroup\$
    – Value Ink
    Commented Nov 11, 2019 at 22:54
5
\$\begingroup\$

Keg -ir -oc, 5 2 bytes (SBCS)

Works in all 3 test cases.

Try it online!

Explanation

-ir will *not* try to evaluate the input
+   Add them
 ½  Halve the value
-oc Output all as a character, if possible

Implicit print. The output is print nice by default.
```
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Loading issue. I just tried the first test case and it didn't error.(I got to clone the most recent Keg interpreter.) \$\endgroup\$
    – user85052
    Commented Nov 9, 2019 at 14:36
  • \$\begingroup\$ Using -ir and -oc will allow this two byter: \$\endgroup\$
    – lyxal
    Commented Nov 9, 2019 at 21:12
  • 2
    \$\begingroup\$ What a great solution. I think we cannot go shorter. \$\endgroup\$
    – stephanmg
    Commented Nov 11, 2019 at 9:11
5
\$\begingroup\$

K (oK), 8 bytes

Solution:

`c$.5*+/

Try it online!

Explanation:

Sum, multiply by 0.5 and convert to ASCII.

`c$.5*+/ / the solution
      +/ / sum (add-over)
   .5*   / multiply by 0.5
`c$      / convert to ASCII
\$\endgroup\$
5
\$\begingroup\$

Bash, 56 bytes

printf \\x$(printf %x $[`printf "(%d+%d)/2" \'$1 \'$2`])

Try it online!

\$\endgroup\$
5
  • 3
    \$\begingroup\$ 1) The outer pair of quotes is needed only because the \x, so better escape just that single character; 2) The deprecated $[..] is shorter for arithmetic evaluation; 3) The old `..` is shorter for subcommand, except when needs escaping; 4) The inner pairs of quotes are needed only because of ', so better escape just those characters. Try it online! \$\endgroup\$
    – manatwork
    Commented Nov 11, 2019 at 11:29
  • \$\begingroup\$ @manatwork: Do you want to post this as an answer? Or should I edit my post? \$\endgroup\$
    – stephanmg
    Commented Nov 11, 2019 at 11:38
  • 1
    \$\begingroup\$ Feel free to edit your post. Is mostly your work. I didn't had the patience this time to juggle with the printfs. \$\endgroup\$
    – manatwork
    Commented Nov 11, 2019 at 11:47
  • 1
    \$\begingroup\$ You have an error in the version posted here: the closing " should be on the left side of the nearby space to avoid touching the next argument's first character, the \. (The TIO code is correct though.) \$\endgroup\$
    – manatwork
    Commented Nov 11, 2019 at 15:14
  • \$\begingroup\$ @manatwork: Thanks for pointing me to this. \$\endgroup\$
    – stephanmg
    Commented Nov 11, 2019 at 15:21
5
\$\begingroup\$

Shakespeare Programming Language, 144 bytes

Try it online!

(Whitespace added for readability)

/.Ajax,.Puck,.Act I:.Scene I:.[Enter Ajax and Puck]
Ajax:Open mind.
Puck:Open mind.
You is the quotient betweenthe sum ofyou I a big cat.
Speak thy.

Simple enough, just finds the average. ASCII characters and numbers are identical in SPL, so this language was ideal for the task.

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

dzaima/APL, 11 bytes

(+/÷≢)⍢⎕UCS

Try it online! dzaima/APLs ⎕UCS - convert to/from char currently ignores the fractional part of the given number, so no floor is necessary.

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

Befunge-98 (PyFunge), 7 bytes

~~+2/,@

Try it online!

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

Python 2, 31 bytes

lambda*A:chr(sum(map(ord,A))/2)

Try it online!

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

jq, 23 characters

[explode|add/2]|implode

Sample run:

bash-5.0$ jq -Rr '[explode|add/2]|implode' <<< 'AC'
B

Try it online!

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

Excel, 28 bytes

=CHAR((CODE(A1)+CODE(B1))/2)
\$\endgroup\$
4
\$\begingroup\$

Lua, 42 41 bytes

a=...print(a.char(a:byte()+a:byte(2)>>1))

Try it online!

Removed 1 byte using ouflak's method of taking input as a single command line argument.

Takes input as a single command line argument of two characters. Uses the convenient operator precedence of >>.

Note that this is actually a full standalone Lua 5.3 program, because command line arguments are accessible as a top-level vararg.

\$\endgroup\$
2
  • \$\begingroup\$ You beat me: print(string.char((io.read():byte()+io.read():byte())/2)). \$\endgroup\$
    – stephanmg
    Commented Nov 11, 2019 at 9:30
  • 1
    \$\begingroup\$ Thanks to your tip on my answer, I think I just figured out how to save 1 byte on yours. \$\endgroup\$
    – ouflak
    Commented Nov 11, 2019 at 14:46
4
\$\begingroup\$

Julia 1.0, 26 bytes

a\b=Char(sum(Int[a,b])÷2)

TIO was timing out for me, so only tested at REPL.

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ "You must output the ASCII character signified by the average of the two numbers". Shouldn't your output be a single ASCII character? \$\endgroup\$
    – ouflak
    Commented Nov 12, 2019 at 16:58
  • \$\begingroup\$ Thanks for catching that, fixed. \$\endgroup\$
    – gggg
    Commented Nov 12, 2019 at 17:42
4
\$\begingroup\$

Preproc, 1187 bytes

#define A
#define B,
#define C,,
#define D,C
#define E,D
#define F,E
#define G,F
#define H,G
#define I,H
#define J,I
#define K,J
#define L,K
#define M,L
#define N,M
#define O,N
#define P,O
#define Q,P
#define R,Q
#define S,R
#define T,S
#define U,T
#define V,U
#define W,V
#define X,W
#define Y,X
#define Z,Y
#define a Z H
#define b,a
#define c,b
#define d,c
#define e,d
#define f,e
#define g,f
#define h,g
#define i,h
#define j,i
#define k,j
#define l,k
#define m,l
#define n,m
#define o,n
#define p,o
#define q,p
#define r,q
#define s,r
#define t,s
#define u,t
#define v,u
#define w,v
#define x,w
#define y,x
#define z,y
#define _(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,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,_,cb,cc,cd,ce,cf,cg,ch,ci,cj,ck,cl,cm,cn,co,cp,cq,cr,cs,ct,cu,cv,cw,cx,cy,cz,da,db,dc,dd,de,df,dg,dh,di,dj,dk,dl,dm,dn,do,dp,dq,dr,ds,dt,du,dv,dw,dx,dy,dz,ea,eb,ec,ed,ee,ef,eg,eh,ei,ej,$,...)#$
#define $($a,$b)_($a $b z,y,y,x,x,w,w,v,v,u,u,t,t,s,s,r,r,q,q,p,p,o,o,n,n,m,m,l,l,k,k,j,j,i,i,h,h,g,g,f,f,e,e,d,d,c,c,b,b,a,a,`,`,_,_,^,^,],],\\,\\,[,[,Z,Z,Y,Y,X,X,W,W,V,V,U,U,T,T,S,S,R,R,Q,Q,P,P,O,O,N,N,M,M,L,L,K,K,J,J,I,I,H,H,G,G,F,F,E,E,D,D,C,C,B,B,A,A)

Try it online!

Defines a macro $ which can be called (as shown in the footer of the TIO link above)

This challenge is barely possible in preproc (pure macro) -- if the input can be non-alphanumeric characters too, it would not be possible.

I don't see any simple way to shorten this code -- it might be possible to make the part to take the (n)th element shorter, but that would come at the cost of making the initial macro definitions significantly longer.

\$\endgroup\$
3
\$\begingroup\$

Python 2, 33 32 bytes

-1B from Embodiment of Ignorance using bit ops.

Exactly as specified. For a Python 3 answer change the / into //.

lambda a,b:chr(ord(a)+ord(b)>>1)

Try it online!

\$\endgroup\$
7
  • \$\begingroup\$ This could be just /2 in Python 2. \$\endgroup\$
    – Arnauld
    Commented Nov 9, 2019 at 14:18
  • \$\begingroup\$ I mean, this way. \$\endgroup\$
    – Arnauld
    Commented Nov 9, 2019 at 14:24
  • 1
    \$\begingroup\$ I see, I thought the complete function body can be turned into /2. \$\endgroup\$
    – user85052
    Commented Nov 9, 2019 at 14:25
  • 2
    \$\begingroup\$ You can save 2 bytes with lambda*s:chr(sum(map(ord,s))/2) \$\endgroup\$
    – Uri Granta
    Commented Nov 9, 2019 at 15:42
  • 1
    \$\begingroup\$ For a Python 3 answer change the / into //. part is outdated \$\endgroup\$
    – l4m2
    Commented Apr 22, 2020 at 8:45
3
\$\begingroup\$

Lua, 63 60 bytes

s=io.read()print(s.char(math.floor((s:byte()+s:byte(2))/2)))

Takes the two letters with no delimeters, i.e. AB, j$, |1, etc....

Try it online!

Saved 3 bytes thanks to PhillipRoman

\$\endgroup\$
5
  • \$\begingroup\$ There are shorter Lua solutions. They look also interesting, @outflak. \$\endgroup\$
    – stephanmg
    Commented Nov 11, 2019 at 9:45
  • \$\begingroup\$ @ouflak I'd like to clarify that my solution is a full standalone Lua 5.3 program, because command line arguments are always accessible as a top-level vararg \$\endgroup\$ Commented Nov 11, 2019 at 13:26
  • \$\begingroup\$ @PhilipRoman, So what TIO is doing is virtually running a command line Lua 'shell' and passing in the arguments appropriately? Interesting.... \$\endgroup\$
    – ouflak
    Commented Nov 11, 2019 at 14:09
  • \$\begingroup\$ @PhilipRoman, I assumed naturally that that's what you were doing. I was pondering how TIO did it. Somehow they have to emulate "lua myscript.lua a z". \$\endgroup\$
    – ouflak
    Commented Nov 11, 2019 at 14:23
  • \$\begingroup\$ @ouflak Ah, sorry, I misunderstood you. Yeah, the fact that TIO shows the time taken just like the linux "time" command, seems to indicate that there is indeed a real shell involved. \$\endgroup\$ Commented Nov 11, 2019 at 14:26
3
\$\begingroup\$

MarioLANG, 63 bytes

,
)
,
>[!(>[!
"=#="=#
  - ( >
) ( + -
!+< ) [
#=" !-<)
    #=".

Try it online!

Super golfable, I'm sure - not really able to think in MarioLANG yet. Calculates \$\lfloor\frac{x+y}{2}\rfloor\$.

\$\endgroup\$
3
\$\begingroup\$

Zsh, 33 31 characters

a=({$1..$2} 0)
echo ${a[$#a/2]}

This one does no character code conversion.

Sample run:

manatwork ~ % set -- A C    

manatwork ~ % a=({$1..$2} 0);echo ${a[$#a/2]}
B

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Very clever use of brace expansion! I managed 27 bytes in a more boring way :) \$\endgroup\$
    – roblogic
    Commented Dec 8, 2020 at 14:47
3
\$\begingroup\$

Forth (gforth), 15 bytes

: f + 2/ emit ;

Try it online!

Code Explanation

: f      \ start a new word definition
  +      \ add top two stack arguments
  2/     \ divide top stack value by 2
  emit   \ output char of resulting ascii value
;        \ end word definition
\$\endgroup\$
3
  • \$\begingroup\$ The input requires char, e.g. char a char c f would output b. \$\endgroup\$
    – agc
    Commented Nov 12, 2019 at 21:46
  • \$\begingroup\$ More usable version: : g char char + 2/ cr emit ;, run like g e B, (outputs S), etc. Note that it doesn't seem to matter which order the arguments are in, so g B e also outputs S. \$\endgroup\$
    – agc
    Commented Nov 12, 2019 at 21:51
  • \$\begingroup\$ @agc In gforth ' will also work, so 'a 'b f would also output b and looks much more similar to other languages \$\endgroup\$
    – reffu
    Commented Nov 13, 2019 at 13:41
3
\$\begingroup\$

K4, 9 8 bytes

Solution:

10h$_avg

Examples:

q)k)10h$_avg"AC"
"B"
q)k)10h$_avg"az"
"m"
q)k)10h$_avg"dj"
"g"

Explanation:

Unfortunately the space is needed. Turns out the space isn't necessary!

10h$_avg / the solution
     avg / calculate mean
    _    / floor
10h$     / cast to char

Bonus:

  • 10h$-256+avg for a 12 byte Q version (more/less hacky than 10h$(_)avg for 10)
\$\endgroup\$
0
3
\$\begingroup\$

Hexagony, 14 13 bytes

|*;:2@+/',{,|

Try it online!

Image

Linear control flow: ,{,'+{*2':;@

I decide to plug it in the Hexagony brute forcer program, and it can't find a 12-byte program. At least for that specific linear code.

Other possible linear control flows:

,{,'+'2=':;@
,{,'+'2{=:;@
,},"+{*2':;@
,},"+'2=':;@
,},"+'2{=:;@
,{,'+{+2':;@

(none of them results in a 12-byte solution (there are only some non-halting 12-byte solutions); nevertheless, the brute force program has room for improvement.)

\$\endgroup\$
1
  • \$\begingroup\$ That's one horrific control flow D: \$\endgroup\$
    – Bubbler
    Commented Jan 27, 2021 at 3:25
3
\$\begingroup\$

Perl 5, 31 30 bytes

$x+=ord for@ARGV;print chr$x>>1

Try it online!

Shortened to 30 bytes thanks to comment by Dadsdy

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$
    – rydwolf
    Commented Apr 8, 2022 at 20:09
  • 1
    \$\begingroup\$ Can't you replace the >>1 with /2? \$\endgroup\$
    – Dadsdy
    Commented Jun 8, 2023 at 6:03
2
\$\begingroup\$

Red, 18 bytes

func[a][average a]

Try it online!

Takes the input as a list of two letters.

If this is not acceptable:

Red, 20 bytes

func[a b][a + b / 2]

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ So Red's default behavior is to do the conversion to numbers and then back to letter for the output? \$\endgroup\$
    – Jonah
    Commented Nov 9, 2019 at 19:28
  • 1
    \$\begingroup\$ @Jonah The type of the result is implied by the first argument. #"A" + 1 is #"B"; 1 + #"A" is 66 \$\endgroup\$ Commented Nov 9, 2019 at 19:33
  • 1
    \$\begingroup\$ @Jonah From the documentation: "The full range of mathematical functions can be used with char! values. A Math Error is raised if the result of the arithmetic falls outside of the range 00 - 10FFFF (hexadecimal)." \$\endgroup\$ Commented Nov 9, 2019 at 19:39
2
\$\begingroup\$

MATL, 3 bytes

Ymc

Try it online!

Explanation

Ym   % Implicit input: string of two letters. Implicitly convert to ASCII, and take mean
c    % Implicitly round down, and convert to char
     % Implicit display
\$\endgroup\$
1
  • 6
    \$\begingroup\$ It's fun to stay at the... \$\endgroup\$
    – Luis Mendo
    Commented Nov 9, 2019 at 16:02
2
\$\begingroup\$

Runic Enchantments, 7 bytes

ii+2,k@

Try it online!

Input is space sepatrated. Use invalid inputs at your own peril.

\$\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.