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

49 Answers 49

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\$
    – The Thonnu
    Oct 23 at 7:55
  • \$\begingroup\$ @TheThonnu, you are right, but only the 1st one. \$\endgroup\$
    – JvdV
    Oct 23 at 8:03
  • \$\begingroup\$ Yes, I meant the first one. \$\endgroup\$
    – The Thonnu
    Oct 23 at 11:01
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\$
    – The Thonnu
    Oct 21 at 19:39
  • 1
    \$\begingroup\$ @TheThonnu Done! \$\endgroup\$
    – Luis Mendo
    Oct 21 at 20:13
5
\$\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\$
4
\$\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\$
4
\$\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\$
4
\$\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\$
2
  • \$\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\$
    – F.Carette
    Oct 24 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\$
    – GotCubes
    Oct 25 at 7:43
4
\$\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\$
2
  • 1
    \$\begingroup\$ Why use mod 96 + decrement instead of just mod 97? \$\endgroup\$ Nov 1 at 14:29
  • \$\begingroup\$ @DominicvanEssen I'm not the best at figuring out modulos just yet. Thanks for the golfing advice! \$\endgroup\$
    – Cliff
    Nov 2 at 12:23
4
\$\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\$
    – JSorngard
    Nov 2 at 13:22
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\$
    – coltim
    Nov 4 at 14:52
3
\$\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\$
    – The Thonnu
    Oct 21 at 18:09
  • \$\begingroup\$ The +7 is really clever! I had the exact same solution, except I used -97 instead. \$\endgroup\$
    – GotCubes
    Oct 22 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\$ Oct 27 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\$
    – The Thonnu
    Oct 27 at 9:26
  • \$\begingroup\$ @Jeroentetje3 - I found it: codegolf.meta.stackexchange.com/a/1503/114446: functions do not need to be named. \$\endgroup\$
    – The Thonnu
    Oct 27 at 9:45
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\$

jq, 37 bytes

explode|map(.-97)|[add%26+97]|implode

Try it online!

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

Raku, 27 bytes

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

Try it online!

\$\endgroup\$
3
\$\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\$
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\$ Oct 23 at 19:23
3
\$\begingroup\$

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

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

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

output

\$\endgroup\$
3
\$\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\$ Nov 1 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\$ Nov 1 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\$
    – acvill
    Nov 1 at 16:29
3
\$\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\$
    – MarcMush
    Nov 12 at 10:25
2
\$\begingroup\$

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)
\$\endgroup\$
4
  • 1
    \$\begingroup\$ -1? Please explain? It works fine for all test cases. \$\endgroup\$
    – The Thonnu
    Oct 21 at 18:56
  • \$\begingroup\$ My guess is that the downvote is for the challenge poster immediately posting their own solution. \$\endgroup\$
    – Adám
    Oct 23 at 6:42
  • \$\begingroup\$ @Adám thanks. I thought that was fine. I won't do that in future, then. \$\endgroup\$
    – The Thonnu
    Oct 23 at 6:44
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – Adám
    Oct 23 at 6:45
2
\$\begingroup\$

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?)

\$\endgroup\$
2
\$\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\$
    – Jordan
    Oct 21 at 18:29
2
\$\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
2
\$\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\$
2
\$\begingroup\$

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

C (gcc), 46

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

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 41 \$\endgroup\$
    – AZTECCO
    Oct 22 at 12:50
  • \$\begingroup\$ You can save one more by changing char to int. \$\endgroup\$
    – Steffan
    Oct 22 at 18:18
  • \$\begingroup\$ 40 bytes \$\endgroup\$
    – jdt
    Nov 2 at 13:32
2
\$\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\$ Oct 22 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\$ Oct 22 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\$
    – EasyasPi
    Oct 22 at 13:34
  • \$\begingroup\$ Oh that makes sense, good point. \$\endgroup\$ Oct 22 at 13:35
2
\$\begingroup\$

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)))
\$\endgroup\$
3
  • \$\begingroup\$ 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 \$\endgroup\$
    – Steffan
    Oct 22 at 18:14
  • \$\begingroup\$ 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! \$\endgroup\$
    – Steffan
    Oct 22 at 18:17
  • \$\begingroup\$ 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! \$\endgroup\$
    – Steffan
    Oct 22 at 18:19
2
\$\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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.