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

93 Answers 93

1
\$\begingroup\$

C (gcc), 17 bytes

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

Try it online!

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

Charcoal, 7 bytes

℅⊘ΣES℅ι

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

    S   Input string
   E    Map over characters
      ι Current character
     ℅  Take the ordinal
  Σ     Take the sum
 ⊘      Halved
℅       Convert to character
        Implicitly print
\$\endgroup\$
1
\$\begingroup\$

Python 3, 34 bytes

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

Try it online!

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

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
\$\endgroup\$
1
\$\begingroup\$

Haskell, 32 bytes

toEnum.(`div`2).sum.map fromEnum

Try it online!

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

Japt, 8 6 bytes

xc z d

Try it

\$\endgroup\$
2
  • \$\begingroup\$ Why not just xc z d? \$\endgroup\$
    – Gymhgy
    Nov 10, 2019 at 5:08
  • 2
    \$\begingroup\$ @EmbodimentofIgnorance, because beer! Also, lack of practice. But mainly beer! \$\endgroup\$
    – Shaggy
    Nov 10, 2019 at 11:19
1
\$\begingroup\$

Ruby, 27 bytes

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

Try it online!

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

Gol><>, 6 bytes

ii+2,H

Try it online!

Explanation

ii     Push 2 input chars as int to stack
  +    Add them
   2,  Float divide by 2
     H Output as char and halt (Seems to truncate)
\$\endgroup\$
1
\$\begingroup\$

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'
\$\endgroup\$
5
  • 2
    \$\begingroup\$ 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 \$\endgroup\$
    – Max
    Nov 10, 2019 at 15:45
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Nov 10, 2019 at 15:45
  • \$\begingroup\$ Thanks you guys, very clever. I thought about the bitshift but didn't realize it is low in op precedence! \$\endgroup\$ Nov 10, 2019 at 22:50
  • \$\begingroup\$ Write c as a helper and take two inputs as a=>b=> \$\endgroup\$
    – l4m2
    Apr 22, 2020 at 8:47
  • 1
    \$\begingroup\$ 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)) \$\endgroup\$ Aug 17, 2020 at 4:44
1
\$\begingroup\$

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
\$\endgroup\$
3
  • \$\begingroup\$ Oh wow, I made a solution that was identical to this yesterday but didn't post it, nice one \$\endgroup\$
    – EdgyNerd
    Nov 10, 2019 at 16:29
  • 1
    \$\begingroup\$ You don't need the trailing d. \$\endgroup\$
    – trillian
    Nov 13, 2019 at 12:18
  • \$\begingroup\$ @randomdude999 good catch \$\endgroup\$
    – frank
    Nov 13, 2019 at 17:30
1
\$\begingroup\$

Stax, 4 bytes

:V@]

Run and debug it

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

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<
\$\endgroup\$
1
\$\begingroup\$

@, 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
\$\endgroup\$
1
\$\begingroup\$

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!

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

Ahead, 7 bytes

ii+2/o@

Try it online!

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

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
\$\endgroup\$
1
\$\begingroup\$

Haskell, 48 bytes

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

Try it online

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

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!

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

MAWP, 5 bytes

|M2P;

Input must be given without any separator.

Try it!

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

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
\$\endgroup\$
1
\$\begingroup\$

Raku, 18 characters

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

Sample run:

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

Try it online!

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

Python 53 46 bytes

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

print(chr(int((ord(input())+ord(input()))/2)))
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You only use a and b once, so better avoid storing them in variables by inputting them on separate lines. Try it online! \$\endgroup\$
    – manatwork
    Dec 11, 2020 at 9:52
  • \$\begingroup\$ @manatwork yes, tanks! \$\endgroup\$ Dec 11, 2020 at 13:23
1
\$\begingroup\$

Java, 45 chars

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

Try it online!

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

Pyth, 5 bytes

C.OCM

Try it online!

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

Factor, 8 bytes

[ + 2/ ]

Try it online!

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

Factor, 4 bytes

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

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ you can remove the type names and use just f(a,b){return(a+b)/2;} since itll default to int \$\endgroup\$
    – sugarfi
    Apr 10, 2022 at 21:31
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – user111743
    Apr 11, 2022 at 1:19
1
\$\begingroup\$

Knight, 14 bytes

O A/+A P A P 2

Try it online!

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

Scala, 32 bytes

Golfed version. Try it online!

z=>(z.map(_.toInt).sum/2).toChar
\$\endgroup\$
0
\$\begingroup\$

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

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

Swift, 134 bytes

var s=readLine(),a=readLine();print(Character(UnicodeScalar((s?.compactMap{$0.asciiValue}[0])!+(a?.compactMap{$0.asciiValue}[0])!/2)))
\$\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.