# Alphabet Checksum

Given a string of lowercase letters, output the "alphabet checksum" of that string, as a letter.

### Example

Let's say we have the string "helloworld". With a = 0, b = 1, c = 2 ... z = 25, we can replace all of the letters with numbers:

h  e  l  l  o  w  o  r  l  d
7  4  11 11 14 22 14 17 11 3


Now, we can sum these:

7+4+11+11+14+22+14+17+11+3 = 114


If we mod this by 26, we get:

114 % 26 = 10


Now, using the same numbering system as before, get the 10th letter, k. This is our answer.

### Test cases

Input          Output

helloworld     k
abcdef         p
codegolf       h
stackexchange  e
aaaaa          a


This is , so shortest code in bytes wins.

• Sandbox Oct 21, 2022 at 17:54

# Excel (ms365), 59, 58 bytes

-1 Thanks to @TheThonnu

=CHAR(MOD(SUM(CODE(MID(A1,SEQUENCE(LEN(A1)),1))+7),26)+97) • I think you can change the -97 to +7 (as some other people have done in their answers) Oct 23, 2022 at 7:55
• @TheThonnu, you are right, but only the 1st one.
– JvdV
Oct 23, 2022 at 8:03
• Yes, I meant the first one. Oct 23, 2022 at 11:01

# MATL, 10 bytes

2Y2j97-sQ)


### Explanation

2Y2j97-sQ)
2Y2         % Push predefined literal: string 'abc···xyz'
% STACK: 'abc···xyz'
j        % Take input as a string
% STACK: 'abc···xyz', 'helloworld'
97-     % Push 97 (ASCII code of 'a'), and subtract element-wise
% STACK: 'abc···xyz', [7 4 11 11 14 22 14 17 11 3]
s    % Sum
% STACK: 'abc···xyz', 114
Q)  % Add 1, and use as index (1-based, modular). Implicit display
% STACK: 'k'

• Please can you add an explanation? Oct 21, 2022 at 19:39
• @TheThonnu Done! Oct 21, 2022 at 20:13

# JavaScript (Node.js), 51 bytes

s=>(B=Buffer)([B(s).map(c=>t+=c+7,t=0)|97+t%26])+''


Try it online!

# 05AB1E, 6 bytes

ADIkOè


Input as a list of characters.

Explanation:

A       # Push the lowercase alphabet
D      # Duplicate it
I     # Push the input-list
k    # Get the index of each character in the (top) alphabet
O   # Sum these together
è  # (Modular 0-based) index it into the alphabet
# (after which this character is output as result)


# Go, 68 bytes

func(s string)(t int){for _,r:=range s{t+=int(r)-97}
return t%26+97}


Attempt This Online!

# brainfuck, 142 126 bytes

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


Try it online!

Edit: -16 bytes due to common sense. Remembered I didn't have to load 97 into its own cell before adding/subtracting.

My first time golfing in brainfuck! Here's the ungolfed code for your viewing displeasure:

,					GET EACH CHARACTER IN THE INPUT
[
>++++++++[-<------------>]<-	SUBTRACT 97 (8 TIMES 12 PLUS 1) FROM CELL 0
[->>>>+<<<<]			ADD CELL 0 TO CELL 4
, 				INPUT TO CELL 0
]
>>>>					GO TO CELL 4
>+++++[->+++++<]>+<<			LOAD 26 (5 TIMES 5 PLUS 1) INTO CELL 6
[>+>->+<[>]>[<+>-]<<[<]>-]		TAKE CELL 4 MOD CELL 6
>>>					GO TO RESULT IN CELL 7
>++++++++[-<++++++++++++>]<+		ADD 97 (8 TIMES 12 PLUS 1) TO CELL 7
.					DISPLAY

• As stated in some other answers, you can probably save some bytes by replacing the "-97" by "+7" (because 97+7 = 104 and 104%26=0). Oct 24, 2022 at 8:30
• @F.Carette I tried to do that by just changing the third line of the ungolfed code to +++++++, but that started giving me the wrong answer somehow. Oct 25, 2022 at 7:43
• Looks like you suffer overflow, current code fail zzzzzzzzzzzzzzz (assuming another answer is correct)
– l4m2
Jan 15 at 5:27

# Gaia, 16 15 bytes

⟨c97%⟩¦Σ26%97+c


Try it online!

Pretty standard implementation of what the challenge asks.

I was able to save a byte thanks to the golfing advice given by
Dominic van Essen!

## Explained

⟨c97%⟩¦Σ26%97+c
⟨    ⟩¦           # To each letter in the input {
c97%            #  modulo the character code by 96
#  }
Σ26%      # Get the sum of that list and modulo 26
97+c   # and add 97 to turn it back into an ascii letter

• Why use mod 96 + decrement instead of just mod 97? Nov 1, 2022 at 14:29
• @DominicvanEssen I'm not the best at figuring out modulos just yet. Thanks for the golfing advice! Nov 2, 2022 at 12:23

# Mathematica, 60 48 bytes

FromLetterNumber[Tr[LetterNumber@#-1]~Mod~26+1]&


View it on Wolfram Cloud!

-12 bytes thanks to JSorngard!

• -12 bytes if we avoid StringLength with some arithmetic tricks Nov 2, 2022 at 13:22

# K (ngn/k), 16 14 bytes

c$97+26!+/97!  Try it online! -2 bytes thanks to coltim! Explanation: c$97+26!+/97!  Main function. Takes implicit input
97!  Modulo by 97 to each character in the string
to convert them into the 0..25 system
(In K, every character in a string is also an ASCII charcode)
+/     Sum
26!       Modulo by 26
97+          + 97 to each of them to convert them back to ASCII charcode
c$And convert them back to characters  • You don't need the ' on the -97+ (it will "vectorize" automatically); additionally, -97+ can be swapped out for 97! Nov 4, 2022 at 14:52 # Python 3, 43 bytes -1 byte from @Arnauld lambda s:chr(sum(ord(c)+7for c in s)%26+97)  Try it online! • No need for f=, just put it in the header code (saving you 2 bytes). Oct 21, 2022 at 18:09 • The +7 is really clever! I had the exact same solution, except I used -97 instead. Oct 22, 2022 at 2:22 • @TheThonnu can you explain how 'header code' is valid for not being able to call this function now? How does this work and how is this valid for a code golf? Oct 27, 2022 at 9:16 • @Jeroentetje3 - there's a rule somewhere on codegolf.meta.stackexchange.com where it says that for lambda functions in Python, you can name it in the header. If you look at some other Python answers as well, you'll see this convention. Oct 27, 2022 at 9:26 • @Jeroentetje3 - I found it: codegolf.meta.stackexchange.com/a/1503/114446: functions do not need to be named. Oct 27, 2022 at 9:45 # Japt, 8 bytes ;x!aC gC  Try it Input as an array of characters. Explanation: ;x!aC gC ; # C = "abcdefghijklmnopqrstuvwxyz" x # Compute the following for each character, then sum the results: !aC # Find its index in C gC # Get the character at that index in C (wrapping)  You can also rearrange things like this for a different 8 byte solution which works almost exactly the same way. # jq, 37 bytes explode|map(.-97)|[add%26+97]|implode  Try it online! # Raku, 27 bytes {chr sum(.ords X-97)%26+97}  Try it online! # Nibbles, 7 bytes (14 nibbles) +%+.$-$;'a'26   . # map over$           # the input:
-          #   subtract
'a'     #   the letter 'a'
;        #   (and save it)
$# from each letter # (which gives its 0-based index) + # now sum this list % 26 # apply modulo-26 + # and add back the saved letter 'a' # Java (OpenJDK 8), 132 bytes interface T{static void main(String[]a){int i=a.length(),s=0;for(;i-->0;s+=a.charAt(i)+7);System.out.print((char)(97+s%26));}}  Try it online! • Welcome to Code Golf, and nice answer! Oct 23, 2022 at 19:23 ## C# (.Net 6), 64 bytes int i=0,j=0;for(;i<a.Length;)j+=a[i++]-97;return(char)(97+j%26);  Try it here # J, 19 bytes (26|+/)&.(_97+3&u:)  Attempt This Online! (26|+/)&.(_97+3&u:) &. NB. F&.G y applies G⁻¹(F(G x)) to y, rank is decided by G (_97+3&u:) NB. fork converting str to char codes and subtracting 97 from each (26|+/) NB. fork that sums the arg and mods the result by 26 NB. +/ can be used because u: operates on y as a whole NB. The inverse of G in this case would be to add 97 to NB. the result of F and then convert char code to str  # Bits, 130 bits = 16.25 bytes 1110000111000001110001011111110111010100000001100111110101011100011000000110001011001101101110000000110100111001111101010111111100  This was very painful. ### Explanation 11100001 // Get a string input 1100000 // Push 0 to the stack [this will be our running sum] 11100010 // For each character in the input string: 111111101 // Get ord(c) 1101010 // Add [to the running sum] 000000 // Separator so the numbers don't get joined together 1100111 // Push 7 1101010 // Add [to the running sum] 11100011 // ENDFOR 000000 // Separator so the numbers don't get joined together 1100010 // Push 2 1100110 // Push 6 [this gets joined into 26] 1101110 // Mod our running sum by 26 000000 // Separator so the numbers don't get joined together 1101001 // Push 9 1100111 // Push 7 [this gets joined into 97] 1101010 // Add [to the result of the mod] 111111100 // Get chr(x) // [implicit output of the item on the top of the stack]  ### Output # R v4.2.0, 63 59 -4 thanks to Dominic van Essen \(x,l=letters)l[(sum(match(el(strsplit(x,"")),l)-1)%%26)+1]  The input string x is parsed as characters, and match is used to retrieve the index of each character in the letters builtin, minus one to convert from 1-based to 0-based. • Nice approach. However, generally 'code golf' assumes that you need to include the code to 'receive' the string that you're being given, rather than assuming it's already been stored inside a variable whose name you know (as x here). For R, this typically involves using scan(,'') , or defining a function that accepts the string as a parameter (here this would add 4 bytes like this) Nov 1, 2022 at 9:47 • (and, by the way, match is vectorized, so you can shave-off a few bytes by skipping the lapply altogether like this...) Nov 1, 2022 at 9:55 • @DominicvanEssen again, thanks for the tips! I wasn't aware of the "\" function shorthand -- it seems that's new with R 4.1. Very handy indeed. Nov 1, 2022 at 16:29 # Julia 1.0, 36 26 bytes !s=sum([s...].-'a')%26+'a'  Try it online! -10 MarcMush • -10 bytes by adding and subtracting chars directly !s=sum([s...].-'a')%26+'a' Nov 12, 2022 at 10:25 # Thon (Symbols)s flag, 10 bytes $åị$Σ26%å  ### Explanation $åị$Σ26%å // (implicit input as a string)$  $// For each character in the input string: åị // Get the index in the alphabet // (implicitly create a list of all of these indexes) Σ // Sum the list 26% // Mod by 26 å // Get that letter of the alphabet // (implicit output)  • -1? Please explain? It works fine for all test cases. Oct 21, 2022 at 18:56 • My guess is that the downvote is for the challenge poster immediately posting their own solution. – Adám Oct 23, 2022 at 6:42 • @Adám thanks. I thought that was fine. I won't do that in future, then. Oct 23, 2022 at 6:44 • Well, it is fine, especially when doing so in a lesser known language. It is just my guess that the downvoter expressed their personal opinion. – Adám Oct 23, 2022 at 6:45 # Python, 74 bytes from string import* f=lambda s,l=ascii_lowercase:l[sum(map(l.index,s))%26]  Try it online! # Python + golfing-shortcuts, 47 bytes lambda S:Sl[s(m(Sl.index,S))%26] from s import*  (Is this one a competitive answer?) # Ruby, 33 32 bytes -1 byte thanks to Arnauld ->a{(a.sum{_1.ord+7}%26+97).chr}  Attempt This Online! • @Arnauld Nice! Thanks. Oct 21, 2022 at 18:29 # Factor + math.unicode, 25 24 bytes [ 7 v+n Σ 26 mod 97 + ]  -1 byte thanks to some black magic from Arnauld! Try it online!  ! "helloworld" 7 ! "helloworld" 7 v+n ! { 111 108 115 115 118 126 118 121 115 107 } Σ ! 1154 26 ! 1154 26 mod ! 10 97 ! 10 97 + ! 107  # simply, 76 bytes It is a pretty long one... Creates an anonymous function that outputs the expected result. fn($S){$X=0each$S as$C;$X=&add($X&sub(&ord($C)97))out!ABCL[$_=&mod($X,26)];}


Yes, that's right, I'm assigning the result of &mod into a variable.
Without it, it gives a syntax error, because ... I made mistakes in the compiler...

# Using the code

Simply call the function.

$fn = fn($S){$X=0each$S as$C;$X=&add($X&sub(&ord($C)97))out!ABCL[$_=&mod($X,26)];}

// should output "h"
call $fn("codegolf");  # Ungolfed Somewhat code-y looking: $fn = fn($string) => {$sum = 0;
each $string as$char {
$sum = &add($sum, &sub(&ord($char), 97)); }$index = call &mod($sum, 26); echo !ABCL[$index];
}


Plain English-ish/Pseudo-code looking:

Set $fn to an anonymous function($string).
Begin.
Set $sum to 0. Loop through$string as $char. Begin. Set$sum to the result of calling the function &add(
$sum, Call the function &sub( Call the function &ord($char),
97
)
).
End.

Set $index to the result of calling the function &mod($sum, 26).

Show the value !ABCL[\$index].
End.


Both versions do exactly the same.

# Jelly, 8 bytes

O+7S‘ịØa


A monadic Link that accepts a list of characters and yields a character.

Try it online! Or see the test-suite.

### How?

O+7S‘ịØa - Link: list of characters, Message
O        - ordinals (Message)
S     - sum
‘    - increment
Øa - "abc...xyz"
ị   - index into (1-based and modular)


# Fig, $$\10\log_{256}(96)\approx\$$ 8.231 bytes

ica%26S+7C


Using Arnauld's logic. Accepts a list of characters

Try it online!

ica%26S+7C
C # str -> char code, vectorises
+7  # add 7 to each item
S    # sum
%26     # sum % 26
ca        # lowercase alphabet
i          # index intro ca using result


# Alternate $$\13\log_{256}(96)\approx\$$ 10.701 bytes

ica%26SM'lxca


Try it online!

ica%26SM'lxca
ca  # lowercase alphabet
x    # input
M'l     # map find over x where we look for each char in ca, returns index
S        # sum
%26         # sum % 26
ica            # index into ca using the result


# C (gcc), 46

t;f(char*s){for(t=0;*s;)t+=*s++-97;t=t%26+97;}


Try it online!

• 41 Oct 22, 2022 at 12:50
• You can save one more by changing char to int. Oct 22, 2022 at 18:18
• 40 bytes
– jdt
Nov 2, 2022 at 13:32

# ARM Thumb machine code, 18 bytes

61 20 04 c9 61 3a 10 44 fb d5 1a 38 fd d2 7b 30
70 47


Assembler source:

    .syntax unified
.arch armv7-a
.thumb
.globl alpha_checksum
.thumb_func
// Input: r1: null terminated UTF-32LE string
// Output: r0
// Clobbers: r0-r2
alpha_checksum:
// Initial accumulator. Start at 'a' to cancel the checksum
// loop adding '\0' - 'a' when the null terminator is reached.
movs   r0, #'a'
.Lloop:
ldmia  r1!, {r2}
// Subtract 'a' to convert to a number, set flags
// In the case of the null terminator, this will result in
// -'a', which ends the loop condition below.
subs   r2, #'a'
// Add to the checksum, without setting the flags
// Loop if the subs didn't return negative,
// which happens only with the null terminator.
bpl    .Lloop
.Lend:
// Calculate (checksum % 26) - 26 using a naive subtraction loop
.Lmodulo:
// Subtract 26
subs   r0, #26
// Loop while it was >= 26
bhs    .Lmodulo
// Add 26 to correct the modulo, and 'a' to convert to ASCII.
// Return
bx     lr


This can be called from C using a dummy parameter to place ptr in r1.

ptr is expected to be a pointer to a null terminated UTF-32LE string.

char32_t alpha_checksum(int dummy, const char32_t *ptr);

• I was curious how much it saves to take UTF32 / wchar_t input: The ldmia is only 2 bytes (04 c9), but a 11 f8 01 2b ldrb r2, [r1], #1 post-increment byte load is 4 bytes. So a char* version would be 2 bytes longer. Passing a length as another arg would also cost extra bytes, since an implicit-length C string allows folding the check into a subs we're already doing. And there's no [R1, R2] addressing mode with pre-decrement of one register. Oct 22, 2022 at 8:30
• Not that it matters for code size, but I think it would be more idiomatic to use bge or bhs to keep looping while the input character was >= 'a' (signed or unsigned), rather than branching on the sign of the subtraction result. bpl is different from bge if there's signed overflow. In this case you could think about it as doing a (potentially signed-wrapping) subtraction and then checking the sign of the result, but it seems like a bad habit vs. using the flags result of subs like it was a cmp. Probably not worth editing the answer to change, though. Nice one. Oct 22, 2022 at 8:47
• Yeah I could also do bge or bhs, I chose bpl because I was emphasizing the fact that it will end up adding a negative. Oct 22, 2022 at 13:34
• Oh that makes sense, good point. Oct 22, 2022 at 13:35

# Clojure, 65 bytes

(defn a[s](char(+(mod(apply + (map #(- % 97)(map int s)))26)97)))


Try it online!

Ungolfed:

(defn alpha-checksum [s]
(char (+ (mod (apply + (map #(- % 97) (map int s))) 26) 97)))

• You can remove the space in + ( for -1, and you can use fn instead of defn a (using an anonymous function) to save another 4, and then combine the two map statements to save a bunch more - TIO Oct 22, 2022 at 18:14
• And off of that, you can save one more byte by using a # lambda for the outer function and fn on the inner one (which allows removing a whitespace): Try it online! Oct 22, 2022 at 18:17
• And off of that (sorry lol), you can save one more byte using a trick from the other answers (changing -97 to +7): Try it online! Oct 22, 2022 at 18:19