# 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!

• Please specify whether, for example, B e is a valid input. Nov 10, 2019 at 19:53
• 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 Nov 10, 2019 at 23:05
• If that example is correct, it will invalidate all of the existing answers.
– user85052
Nov 11, 2019 at 10:05
• Sorry. I read an ASCII table wrong and got 69 for B, not 66. Nov 11, 2019 at 14:00
• Could I enter for a non-ASCII compliant system? Nov 25, 2019 at 17:28

# C (gcc), 17 bytes

f(a,b){a=a+b>>1;}


Try it online!

# Charcoal, 7 bytes

℅⊘ΣＥＳ℅ι


Try it online! Link is to verbose version of code. Takes input as a string of two letters. Explanation:

    Ｓ   Input string
Ｅ    Map over characters
ι Current character
℅  Take the ordinal
Σ     Take the sum
⊘      Halved
℅       Convert to character
Implicitly print


# Python 3, 34 bytes

lambda a,b:chr((ord(a)+ord(b))//2)


Try it online!

# Labyrinth, 8 bytes

,,+_2/.@


Try it online!

### How?

, read a character from STDIN and push its ordinal
, read a character from STDIN and push its ordinal
+ pop y, pop x, push x+y
_ push a zero
2 pop x, push 10*x+2
/ pop y, pop x, push x/y (integer division, rounded towards negative infinity)
. pop x, print byte at (x%256)
@ exit program


toEnum.(div2).sum.map fromEnum


Try it online!

# Japt, 8 6 bytes

xc z d


Try it

• Why not just xc z d? Nov 10, 2019 at 5:08
• @EmbodimentofIgnorance, because beer! Also, lack of practice. But mainly beer! Nov 10, 2019 at 11:19

# Ruby, 27 bytes

->x,y{(x.ord+y.ord>>1).chr}


Try it online!

# Gol><>, 6 bytes

ii+2,H


Try it online!

Explanation

ii     Push 2 input chars as int to stack
2,  Float divide by 2
H Output as char and halt (Seems to truncate)


# JavaScript - 62 60 bytes

I'm certain there's a better way than this:

(a,b,c=x=>x.charCodeAt())=>String.fromCharCode(c(a)+c(b)>>1)


Would love some feedback!

Example:

f=(a,b,c=x=>x.charCodeAt())=>String.fromCharCode(c(a)+c(b)>>1)
f('a','c') // 'b'

• Another way to divide by 2 is to delete the last binary digit by doing a right-shift one place. This would be >>1. This is of course longer than /2, but the advantage is that it has lower precedence in terms of order-of-operations, so you can remove a pair of brackets. This results in a net gain of one character. So (...)/2 -> ...>>1
– Max
Nov 10, 2019 at 15:45
• I count 64 bytes. You don’t need the 0 argument. As far as I know only the function is needed, without the name; so you could use (a,b,c=x=>x.charCodeAt())=>String.fromCharCode((c(a)+c(b))/2), which would be 61 bytes. 60 with @Max’s suggestion. Nov 10, 2019 at 15:45
• Thanks you guys, very clever. I thought about the bitshift but didn't realize it is low in op precedence! Nov 10, 2019 at 22:50
• Write c as a helper and take two inputs as a=>b=>
– l4m2
Apr 22, 2020 at 8:47
• You can reduce it to 58 bytes by currying the function, defining the c function inline and doing the division by 2 within it: a=>b=>String.fromCharCode((c=x=>x.charCodeAt()/2)(a)+c(b)) Aug 17, 2020 at 4:44

# Pyth, 6 5 bytes

C.OmC


Try it online!

As a bonus, works with more than two letters

Edit: -1 thanks to @randomdude999, wasnt aware that the lambda function implicitly adds the lambda variable

## How it Works

C.OmC
mC - Map (char to int) to the implicit input
.O    - Take the average
C      - Int to char

• Oh wow, I made a solution that was identical to this yesterday but didn't post it, nice one Nov 10, 2019 at 16:29
• You don't need the trailing d. Nov 13, 2019 at 12:18
• @randomdude999 good catch Nov 13, 2019 at 17:30

# Stax, 4 bytes

:V@]


Run and debug it

# Triangular, 10 9 bytes

t+~2.>_@


Try it online!

Ungolfed:

   t
+ ~
2 .
> _ @

----------------------

t~           - Read 2 characters from input; change direction to SE after
+2>_          - Add the two top values of the stack, then divide by 2
@             - Print as a character


Previous Version (10 bytes):

~.~..+@_2<


# @, 6 bytes

Has some trailing garbage, but can't be avoided because all functions have return values.

ō/+čč2


## Explanation

  +čč  Sum two inputs' charcodes
/   2 Halve the number
ō      Output as a character


## GolfScript, 10 bytes

{+}*2/]''+


## Explanation

{+}*       # Fold the input string with sums
2/     # Divide this number by 2
]''+ # Put stack into array & convert to string

# Implicit print


Try it online!

ii+2/o@


Try it online!

# Burlesque, 9 bytes

)**avavL[


Try it online!

If I'm allowed to report the ceiling, can save 2 chars )**foL[ fo is defined as avpd which calculates the average and then ceilings it.

)** # Map ord(a)
av  # Calculate average
av  # Floor the resulting double
L[  # Int to Char


f=fromEnum;g a b=toEnum(div((f a)+(f b))2)::Char


Try it online

# Erlang (escript), 26 bytes

Sums the input, divide by 2, and then convert to character.

f(X)->[lists:sum(X)div 2].


Try it online!

# MAWP, 5 bytes

|M2P;


Input must be given without any separator.

Try it!

• F***... MAWP have a division built-in... Aug 17, 2020 at 2:23
• @HighlyRadioactive well, there are four arithmetic operations: M, A, W, P
– Dion
Aug 18, 2020 at 5:48
• @Dion I'm getting used to the lack of subtraction and division. Isn't this answer nice, by the way? Aug 18, 2020 at 6:02

# 05AB1E, 4 bytes

ÇO;ç


Try it online!

ÇO;ç  # full program
;   # divide...
O    # sum of...
Ç     # charcodes of...
# implicit input...
;   # by 2
ç  # convert to charcodes, rounding down
# implicit output


# Raku, 18 characters

{($^a..^$^b)[*/2]}


Sample run:

> {($^a..^$^b)[*/2]}('A', 'C')
B


Try it online!

# Python 53 46 bytes

thanks to @manatwork recommended not to store inputs in variables.

print(chr(int((ord(input())+ord(input()))/2)))

• You only use a and b once, so better avoid storing them in variables by inputting them on separate lines. Try it online! Dec 11, 2020 at 9:52
• @manatwork yes, tanks! Dec 11, 2020 at 13:23

# Java, 45 chars

char c(char a,char b){return(char)((a+b)/2);}


Try it online!

# Pyth, 5 bytes

C.OCM


Try it online!

# Factor, 8 bytes

[ + 2/ ]


Try it online!

Or if we're allowed to take input in a list, then

# Factor, 4 bytes

mean


# C++ - 34 Bytes

int f(int a,int b){return(a+b)/2;}


Ungolfed

int f(int a, int b)
{
return (a + b) / 2;
}


Explanation

A function that returns the average of two integers, in this case it can be used with ASCII characters.

• you can remove the type names and use just f(a,b){return(a+b)/2;} since itll default to int Apr 10, 2022 at 21:31
• @sugarfi Thanks for your comment but implicit int in declarations' is not defined for C++, although it is defined for C (at least for C89). I even tried it on GCC to make sure, and it doesn't compile if I remove the type names; but thanks anyway.
– user111743
Apr 11, 2022 at 1:19

# Knight, 14 bytes

O A/+A P A P 2


Try it online!

# Scala, 32 bytes

Golfed version. Try it online!

z=>(z.map(_.toInt).sum/2).toChar


# C (gcc), 49 bytes

int a,b;scanf("%c,%c",&a,&b);printf("%c",a+b>>1);


Try it online!

Got the idea of bit-shift operators from this answer

Swift, 134 bytes

var s=readLine(),a=readLine();print(Character(UnicodeScalar((s?.compactMap{$0.asciiValue}[0])!+(a?.compactMap{$0.asciiValue}[0])!/2)))