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

2
\$\begingroup\$

PowerShell, 32 bytes

$args|%{$s+=+$_}
[char]($s-shr1)

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Pari/GP, 57 bytes

fun(x,y)=Strchr(floor((Vecsmall(x)[1]+Vecsmall(y)[1])/2))

Try it online!

Description

  • Vecsmall(x)[1] gives the ASCII number of x.

  • Vecsmall(y)[1] gives the ASCII number of y.

  • /2 gives the average.

  • floor() truncates the average.

  • Strchr() converts the average to its ASCII character.

\$\endgroup\$
2
\$\begingroup\$

PHP (7.4), 34 bytes

fn($a,$b)=>chr(ord($a)+ord($b)>>1)

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Ceylon (named function) 74 68 65 64 60

Contrary to e.g. Java, in Ceylon you can't directly add characters (The compiler complains with Left operand must be of summable type: Character is not a subtype of Summable). Instead, we first need to convert it to an integer, and afterwards back to character. This gives 74 bytes:

Character c(Character x,Character y)=>((x.integer+y.integer)/2).character;

We can write this shorter by using the .hash attribute instead of the .integer attribute, which for characters turns out to have the same implementation (returning the Unicode value of a character). This is 68 bytes:

Character d(Character x,Character y)=>((x.hash+y.hash)/2).character;

A more ceylonic way of doing this is to use the fact that Character is a Enumerable type, and implements the .neighbour and .offset methods. The average has an offset from the smaller which is half as large as the offset of the larger to the smaller. This even turns out slightly smaller, with 65 bytes.

Character a(Character x,Character y)=>x.neighbour(y.offset(x)/2);

Unfortunately most of this is the declaration of the types. We can reduce this slightly by having the method take a sequence (tuple/pair) of two characters instead of both individually – this removes one parameter, but adds [...] several times, coming to 64 bytes:

Character b(Character[2]c)=>c[0].neighbour(c[1].offset(c[0])/2);

An alternative way of making the type declaration shorter is an alias declaration. This only is worthwile from 3 usages of Character, which we have here, reaching 60 bytes:

alias C=>Character;C e(C x,C y)=>x.neighbour(y.offset(x)/2);

(The .hash version with alias comes to 63 bytes.)

Try all the examples online

\$\endgroup\$
2
\$\begingroup\$

Wren, 67 bytes

There should be a Wren answer for every Lua answer.

Fn.new{|a|String.fromCodePoint((a.bytes.reduce{|a,i|a+i}/2).floor)}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Javascript, 59 53 Bytes

i=>String.fromCharCode(Buffer.from(i).reduce((t,v)=>t+v)/2)

i=>String.fromCharCode((b=Buffer.from(i))[0]+b[1]>>1)

> f('ae')
'c'
> f('ab')
'a'
> f('ac')
'b'
> f('ad')
'b'
> f('ae')
'c'
> f('aZ')
']'
> f('Be')
'S'

Bonus (Sorry if off topic, I'm not sure if this is allowed and couldn't find anything with a quick check. Please advise if this is actually a viable answer or not), here is the first answer written as NodeJS REPL input (making use of tabs to complete partial names):

i=>St .f h (Bu .f (i).reduce((t,v)=>t+v)/2) (43 bytes)

Note tabs might be malformed by page formatting. Note pasting anything after the tab will suppress completion on some terminals.

\$\endgroup\$
2
  • \$\begingroup\$ Could this be shortened using toString(36) and parseInt? \$\endgroup\$ Dec 11, 2020 at 6:00
  • \$\begingroup\$ Suggest Buffer(i) instead of Buffer.from(i) for 5 bytes. I think this is the shortest possible solution in Javascript (which is definitely not the right tool for this job, sadly). \$\endgroup\$
    – Fhuvi
    Jun 8, 2023 at 14:53
2
\$\begingroup\$

Whitespace, 45 bytes

[S S S N
_Push_0][S N
S _Duplicate_0][T   N
T   S _Read_char_from_STDIN][T  T   T   _Retrieve][S N
S _Duplicate][S N
S _Duplicate][T N
T   S _Read_char_from_STDIN][T  T   T   _Retrieve][T    S S S _Add][S S S T S N
_Push_2][T  S T S _Integer_divide][T    N
S S _Print_as_character]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Explanation:

Character inputs in Whitespace are always stored as unicode integers, so we don't have to do an explicit conversion manually. The program has the following pseudo-code:

int a = STDIN as character
int b = STDIN as character
int c = (a+b) integer-divided by 2
Print c as character to STDOUT

I also use the first input as a heap-address for the second input, since it's guaranteed to be non-negative. This saves a byte by using SNS (duplicate) twice instead of SSSN + SNS (push 0 and duplicate).

\$\endgroup\$
2
\$\begingroup\$

brainfuck, 46 bytes

,>,[-<+>]++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>>>.

Try it online!

,>,                             Takes input into cells 0 and 1
[-<+>]                          Sums the two values, storing the result in cell 0
++<                             Sets cell 1 to 2, and returns to cell 0
[->-[>+>>]>[+[-<+>]>+>>]<<<<<]  Divides cell 0 by cell 1
>>>.                            Moves to and outputs cell 3, where the quotient is stored
\$\endgroup\$
1
  • 1
    \$\begingroup\$ ,<,[>+<-]>[-[->+>]<[<<]>]>. is only 27 bytes, and it does the division by 2 by basically decrementing the sum, decrementing the sum again only if the result is still greater than 0, and incrementing another tape value, all until the sum is 0. \$\endgroup\$ Nov 12, 2019 at 13:09
2
\$\begingroup\$

(e)Lisp, 50 bytes

(insert (/ (+ (aref (read) 0) (aref (read) 0)) 2))
\$\endgroup\$
2
\$\begingroup\$

CJam, 10 bytes

rcirci+2/c

Try it online!

\$\endgroup\$
2
\$\begingroup\$

T-SQL, 64 bytes

declare @ char='Z',@2 char='a'print char((ascii(@)+ascii(@2))/2)

assuming @ and @2 the input.

37 bytes if only put:

print char((ascii('B')+ascii('C'))/2)
\$\endgroup\$
2
\$\begingroup\$

T-SQL, 32 bytes

SELECT CHAR(AVG(ASCII(c)))FROM i

Input is taken via a pre-existing table \$i\$ with CHAR(1) column \$c\$, per our IO standards.

The two input characters are entered as separate rows. (Note that because SQL is set-based, this will calculate the average of an arbitrary number of inputs.)

\$\endgroup\$
2
\$\begingroup\$

C (gcc), 22 bytes

Saved 0 bytes, but got a giggle, thanks to @ceilingcat!

(*c)()=L"\xd137048d쏸";

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ Can you write \xd1 as a char? \$\endgroup\$
    – l4m2
    Apr 22, 2020 at 8:46
  • \$\begingroup\$ @l4m2 Seg fault when I tried (*c)()=L"Ñ37048d쏸"; \$\endgroup\$
    – Noodle9
    Apr 22, 2020 at 11:39
  • \$\begingroup\$ That's because tio use utf8 \$\endgroup\$
    – l4m2
    Apr 22, 2020 at 12:53
  • \$\begingroup\$ (*c)()="??7???"; is 1 byte shorter than c(a,b){b=a+b>>1;} \$\endgroup\$
    – l4m2
    Apr 22, 2020 at 13:02
2
\$\begingroup\$

shortC, 12 8 bytes

APG+G>>1

Try it online!

Please advise on golfing skeeels.

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

1+, 25 23 bytes

,,+11##1+"\"/^"+1+<1+#;

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Husk, 3 bytes

c½Σ

Try it online!

Input as a two character string.

\$\endgroup\$
2
\$\begingroup\$

Zsh, 27 bytes

Not as original as @manatwork's solution, but shorter. Try it online!

<<<${(#)$(((##$1+##$2)/2))}

Alternative for 30 bytes: printf \\$[[##8](##$1+##$2)/2]

\$\endgroup\$
2
\$\begingroup\$

Piet (ASCII-Piet), 12 bytes

jN LnN FjCjJ

ASCII-Piet is a compact encoding for the image-based esolang Piet that encodes each pixel (or codel) of Piet code as a single ASCII character. This ASCII-Piet code translates into the following program:

enter image description here

Or enlarged to codel size 25:

enter image description here

Edit: fixed the enlarged version, and golfed two bytes off by rotating the program.

Simply takes two characters from input (Piet can only represent chars on the stack by codepoints), sums them, divides by two, and outputs the result char.

\$\endgroup\$
2
\$\begingroup\$

Vyxal, 3 bytes

CṁC

Try it Online!

Explanation

CṁC
     (implicit input)
C    Convert to character code list
 ṁ   Get average
  C  Convert back to letter
\$\endgroup\$
2
\$\begingroup\$

ABAP

    SELECTION-SCREEN BEGIN OF BLOCK b0.
    PARAMETERS: p_first  TYPE c LOWER CASE OBLIGATORY,
                p_second TYPE c LOWER CASE OBLIGATORY.
    SELECTION-SCREEN END OF BLOCK b0.

    START-OF-SELECTION.
    
    WRITE / cl_abap_conv_in_ce=>uccpi( uccp =
         (
            ( cl_abap_conv_out_ce=>uccpi( char = p_first )
              + cl_abap_conv_out_ce=>uccpi( char = p_second ) ) / 2 )
          ).
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Golf! Is all of that whitespace necessary, or could some be removed to save bytes? \$\endgroup\$ Nov 16, 2022 at 19:30
2
\$\begingroup\$

><> (Fish), 5 bytes

+2,o;

Try it

\$\endgroup\$
2
\$\begingroup\$

Thunno 2 B, 2 bytes

mC

Attempt This Online!

mC  # implicit input, B flag converts string to byte array
m   # mean
 C  # convert to letter
\$\endgroup\$
2
\$\begingroup\$

PHP 7.3, 52 51 bytes

-1 byte thanks to manatwork

$x=function($a,$b){echo chr((ord($a)+ord($b))/2);};

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I guess you intended to write “51 bytes” there. Assuming you count the TIO version which is 1 byte shorter than the one posted here. \$\endgroup\$
    – manatwork
    Nov 14, 2019 at 12:24
  • \$\begingroup\$ And you got something wrong: either (x+y)/2 or x+y>>1. I mean, if you use parenthesis, then division is shorter. The shift-right is used to avoid the need to use parenthesis. \$\endgroup\$
    – manatwork
    Nov 14, 2019 at 12:27
2
\$\begingroup\$

Vyxal 2.4.1 K, 2 bytes

ṁC

Try it Online!

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

Java (JDK), 20 21 bytes

-1 byte after seeing in other languages that [...]>>1 is a shorter way to do ([...])/2 because it has less operator priority!

A 2-parameters problem is the only moment where the double lambda can shine!

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

C, 17 bytes

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

At first I thought

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

was the best but then I remembered Cs operation precedent and realized it would be shorter to shift right by 1.

\$\endgroup\$
2
\$\begingroup\$

Trilangle, 8 bytes

ii2+d@'o

Try it in the online interpreter!

Takes the two letters on stdin with no separator. Very straightforward, putchar((getchar() + getchar()) / 2).

\$\endgroup\$
2
\$\begingroup\$

Julia 1.0, 24 21 14 bytes

a\b=a+(b-a)÷2

Try it online!

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

05AB1E, 4 bytes

ÇÅAç

Try it online! or verify all test cases

Explanation

Ç    | Get ASCII values of both inputs
 ÅA  | Take the mean
   ç | Convert to ASCII char
\$\endgroup\$
1
\$\begingroup\$

Perl 5 -p, 22 bytes

$_=chr+(ord(<>)+ord)/2

Try it online!

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