30
\$\begingroup\$

Your task

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.

\$\endgroup\$
1
  • \$\begingroup\$ Sandbox \$\endgroup\$ Commented Oct 21, 2022 at 17:54

57 Answers 57

9
\$\begingroup\$

Excel (ms365), 59, 58 bytes

-1 Thanks to @TheThonnu

=CHAR(MOD(SUM(CODE(MID(A1,SEQUENCE(LEN(A1)),1))+7),26)+97) 

enter image description here

\$\endgroup\$
3
  • \$\begingroup\$ I think you can change the -97 to +7 (as some other people have done in their answers) \$\endgroup\$ Commented Oct 23, 2022 at 7:55
  • \$\begingroup\$ @TheThonnu, you are right, but only the 1st one. \$\endgroup\$ Commented Oct 23, 2022 at 8:03
  • \$\begingroup\$ Yes, I meant the first one. \$\endgroup\$ Commented Oct 23, 2022 at 11:01
6
\$\begingroup\$

JavaScript (Node.js), 51 bytes

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

Try it online!

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

Go, 68 bytes

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

Attempt This Online!

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

MATL, 10 bytes

2Y2j97-sQ) 

Try it online! Or verify all test cases.

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' 
\$\endgroup\$
2
  • \$\begingroup\$ Please can you add an explanation? \$\endgroup\$ Commented Oct 21, 2022 at 19:39
  • 1
    \$\begingroup\$ @TheThonnu Done! \$\endgroup\$ Commented Oct 21, 2022 at 20:13
5
\$\begingroup\$

05AB1E, 6 bytes

ADIkOè 

Input as a list of characters.

Try it online or verify all test cases.

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

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 
\$\endgroup\$
3
  • \$\begingroup\$ 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). \$\endgroup\$ Commented Oct 24, 2022 at 8:30
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Oct 25, 2022 at 7:43
  • \$\begingroup\$ Looks like you suffer overflow, current code fail zzzzzzzzzzzzzzz (assuming another answer is correct) \$\endgroup\$ Commented Jan 15, 2023 at 5:27
5
\$\begingroup\$

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.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 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) \$\endgroup\$ Commented Nov 1, 2022 at 9:47
  • 1
    \$\begingroup\$ (and, by the way, match is vectorized, so you can shave-off a few bytes by skipping the lapply altogether like this...) \$\endgroup\$ Commented Nov 1, 2022 at 9:55
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ Commented Nov 1, 2022 at 16:29
5
\$\begingroup\$

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 
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Why use mod 96 + decrement instead of just mod 97? \$\endgroup\$ Commented Nov 1, 2022 at 14:29
  • \$\begingroup\$ @DominicvanEssen I'm not the best at figuring out modulos just yet. Thanks for the golfing advice! \$\endgroup\$ Commented Nov 2, 2022 at 12:23
  • \$\begingroup\$ -1 byte: ⟨c7+⟩¦Σ26%97+c. \$\endgroup\$ Commented Jun 7, 2023 at 7:29
5
\$\begingroup\$

Mathematica, 60 48 bytes

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

View it on Wolfram Cloud!

-12 bytes thanks to JSorngard!

\$\endgroup\$
1
  • \$\begingroup\$ -12 bytes if we avoid StringLength with some arithmetic tricks \$\endgroup\$ Commented Nov 2, 2022 at 13:22
4
\$\begingroup\$

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

Python 3, 43 bytes

-1 byte from @Arnauld

lambda s:chr(sum(ord(c)+7for c in s)%26+97) 

Try it online!

\$\endgroup\$
7
  • \$\begingroup\$ No need for f=, just put it in the header code (saving you 2 bytes). \$\endgroup\$ Commented Oct 21, 2022 at 18:09
  • \$\begingroup\$ The +7 is really clever! I had the exact same solution, except I used -97 instead. \$\endgroup\$ Commented Oct 22, 2022 at 2:22
  • \$\begingroup\$ @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? \$\endgroup\$ Commented Oct 27, 2022 at 9:16
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ Commented Oct 27, 2022 at 9:26
  • \$\begingroup\$ @Jeroentetje3 - I found it: codegolf.meta.stackexchange.com/a/1503/114446: functions do not need to be named. \$\endgroup\$ Commented Oct 27, 2022 at 9:45
4
\$\begingroup\$

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) +7 - add seven (vectorises) S - sum ‘ - increment Øa - "abc...xyz" ị - index into (1-based and modular) 
\$\endgroup\$
4
\$\begingroup\$

Raku, 27 bytes

{chr sum(.ords X-97)%26+97} 

Try it online!

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

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' 

enter image description here

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

Vyxal, 9 bytes

øA‹∑₄%›øA 

Try it Online!

øA‹∑₄%›øA øA Letter to number (1-indexed) ‹ Decrement each value in list ∑ Sum it up ₄%› Modulo by 26 and increment øA Number to letter 
\$\endgroup\$
3
  • \$\begingroup\$ øA‹∑₄%›øA is shorter \$\endgroup\$ Commented Oct 21, 2022 at 22:42
  • \$\begingroup\$ @lyxal oh wow I didn't know decrement vectorises by default \$\endgroup\$ Commented Oct 22, 2022 at 10:01
  • \$\begingroup\$ 6 bytes by porting the 05AB1E answer. \$\endgroup\$ Commented Mar 25, 2023 at 15:57
4
\$\begingroup\$

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

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 
\$\endgroup\$
1
  • \$\begingroup\$ You don't need the ' on the -97+ (it will "vectorize" automatically); additionally, -97+ can be swapped out for 97! \$\endgroup\$ Commented Nov 4, 2022 at 14:52
4
\$\begingroup\$

Julia 1.0, 36 26 bytes

!s=sum([s...].-'a')%26+'a' 

Try it online!

-10 MarcMush

\$\endgroup\$
1
  • 1
    \$\begingroup\$ -10 bytes by adding and subtracting chars directly !s=sum([s...].-'a')%26+'a' \$\endgroup\$ Commented Nov 12, 2022 at 10:25
4
\$\begingroup\$

R, 44 43 42 bytes

letters[sum(utf8ToInt(scan(,""))+7)%%26+1] 

Try it online!

(or 38 bytes as a function in R ≥ 4.1).

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

jq -Rr, 37 34 bytes

[explode[]-97]|[add%26+97]|implode 

Try it online!

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

Ruby, 33 32 bytes

-1 byte thanks to Arnauld

->a{(a.sum{_1.ord+7}%26+97).chr} 

Attempt This Online!

\$\endgroup\$
1
  • \$\begingroup\$ @Arnauld Nice! Thanks. \$\endgroup\$ Commented Oct 21, 2022 at 18:29
3
\$\begingroup\$

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.

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

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.

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

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: // Load character, increment pointer 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 add r0, r2 // 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. adds r0, #'a' + 26 // 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); 
\$\endgroup\$
4
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Oct 22, 2022 at 8:30
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Oct 22, 2022 at 8:47
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Oct 22, 2022 at 13:34
  • \$\begingroup\$ Oh that makes sense, good point. \$\endgroup\$ Commented Oct 22, 2022 at 13:35
3
\$\begingroup\$

Charcoal, 8 bytes

§βΣES⌕βι 

Try it online! Link is to verbose version of code. Explanation:

 S Input string E Map over characters ι Current character ⌕ Find index in β Predefined variable lowercase alphabet Σ Take the sum § Cyclically indexed into β Predefined variable lowercase alphabet Implicitly print 
\$\endgroup\$
3
\$\begingroup\$

Pip, 20 bytes

C(($+(7+A*a))%26+97) 

Try It Online!

Probably could be shorter, I feel like there are just way too many parentheses.

\$\endgroup\$
2
  • \$\begingroup\$ Hint: The preset variable z should be quite useful here. \$\endgroup\$ Commented Oct 22, 2022 at 4:02
  • \$\begingroup\$ @DLosc I literally tried that and it came out longer, I must be doing something horribly wrong \$\endgroup\$ Commented Oct 22, 2022 at 5:02
3
\$\begingroup\$

Brachylog, 14 bytes

ạ+₇ᵐ+%₂₆+₉₇g~ạ 

Try it online!

Explanation

ạ String to char codes +₇ᵐ Add 7 to each code (a <-> 97 becomes 104 = 0 (mod 26)) + Sum %₂₆ Mod 26 +₉₇ Add 97 g Wrap into a list ~ạ Char code to string 
\$\endgroup\$
3
\$\begingroup\$

Pushy, 7 bytes

L7*SvOq 

Try it online!

 The input is implicitly converted into bytecodes on the stack. L7* Push the length of the input, times 7. S Push the sum of the stack. vO Send this to the 'output stack'. q Index into the ASCII lowercase alphabet (mod 26) and print the result. 

Pushing 7 times the length comes from the fact that a has bytecode 97, and \$ -97 \equiv 7 \ (\text{mod} \, 3) \$. So it's equivalent to subtracting 97 from each bytecode.

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

Java (OpenJDK 8), 132 bytes

interface T{static void main(String[]a){int i=a[0].length(),s=0;for(;i-->0;s+=a[0].charAt(i)+7);System.out.print((char)(97+s%26));}} 

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$ Commented Oct 23, 2022 at 19:23
3
\$\begingroup\$

Gema, 77 characters

?=@set{s;@add{${s;};@add{@char-int{?};7}}} \Z=@int-char{@add{@mod{$s;26};97}} 

(Yepp. Arithmetic operations are a pain in Gema.)

Sample run:

bash-5.1$ echo -n helloworld | gema '?=@set{s;@add{${s;};@add{@char-int{?};7}}};\Z=@int-char{@add{@mod{$s;26};97}}' k 

Try it online!

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.