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

54 Answers 54

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, 2022 at 7:55
  • \$\begingroup\$ @TheThonnu, you are right, but only the 1st one. \$\endgroup\$
    – JvdV
    Oct 23, 2022 at 8:03
  • \$\begingroup\$ Yes, I meant the first one. \$\endgroup\$
    – The Thonnu
    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\$

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, 2022 at 19:39
  • 1
    \$\begingroup\$ @TheThonnu Done! \$\endgroup\$
    – Luis Mendo
    Oct 21, 2022 at 20:13
5
\$\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\$
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\$
    – F.Carette
    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\$
    – GotCubes
    Oct 25, 2022 at 7:43
  • \$\begingroup\$ Looks like you suffer overflow, current code fail zzzzzzzzzzzzzzz (assuming another answer is correct) \$\endgroup\$
    – l4m2
    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\$ 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\$ 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\$
    – acvill
    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\$ 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\$
    – Cliff
    Nov 2, 2022 at 12:23
  • \$\begingroup\$ -1 byte: ⟨c7+⟩¦Σ26%97+c. \$\endgroup\$
    – alephalpha
    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\$
    – JSorngard
    Nov 2, 2022 at 13:22
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\$

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\$
    – The Thonnu
    Oct 21, 2022 at 18:09
  • \$\begingroup\$ The +7 is really clever! I had the exact same solution, except I used -97 instead. \$\endgroup\$
    – GotCubes
    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\$ 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\$
    – The Thonnu
    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\$
    – The Thonnu
    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\$

jq, 37 bytes

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

Try it online!

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

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, 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\$
    – MarcMush
    Nov 12, 2022 at 10:25
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\$
    – Jordan
    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\$

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

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\$
    – lyxal
    Oct 21, 2022 at 22:42
  • \$\begingroup\$ @lyxal oh wow I didn't know decrement vectorises by default \$\endgroup\$
    – math scat
    Oct 22, 2022 at 10:01
  • \$\begingroup\$ 6 bytes by porting the 05AB1E answer. \$\endgroup\$
    – user117404
    Mar 25, 2023 at 15:57
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\$ Oct 23, 2022 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, 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\$
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, 2022 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, 2022 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, 2022 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, 2022 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\$

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.