10
\$\begingroup\$

The challenge is simple: Read 3 letters from a system input¹ and convert them to numbers (A-1, B-2, C-3...Z-26)². Print each of those numbers in the order the letters were inputted in, each on a new line and then the sum of all 3 numbers, on its own line as well.

NOTE: There will be no extra input characters, only 3 letters. However, the code must be case insensitive.

This follows the standard rules of code golf, and the shortest byte count wins.

¹If your language does not support input, you may use from a file, or predefined variable instead. However, the program should be able to run any and all possibilities.

²The program should be able to take lowercase AND uppercase, and allow for a mix of lowercase and uppercase (e.g wOq or ZNu)

\$\endgroup\$
2
  • 5
    \$\begingroup\$ Welcome to Code Golf and nice first question! For future reference, we recommend using the Sandbox to get feedback on challenge ideas before posting them to main. I'd suggest using our I/O defaults rather than specifying your own. \$\endgroup\$ Jul 25 at 0:45
  • \$\begingroup\$ I will check it out. It's a pretty simple question, but I thought it might be a fun little challenge to solve if you dont have a ton of time for something more complex. \$\endgroup\$
    – RMCodes
    Jul 25 at 0:48

32 Answers 32

4
\$\begingroup\$

Vyxal KJ, 6 bytes

32%₌⁋∑

Try it Online!

-2 thanks to Aaron

Explained

32%₌⁋∑
      # the K flag tells the program to treat letters as their ascii value. This is an ode to Keg, my first golfing language
32%   # modulo each ascii value by 32
   ₌⁋∑ # join ^ on newlines and also get the sum of ^
       # the J flag prints the entire stack joined on newlines
\$\endgroup\$
4
  • \$\begingroup\$ Impressive. I still don't understand how people learn these languages. I don't understand any part of what it does, just that it works 😂 \$\endgroup\$
    – RMCodes
    Jul 25 at 1:08
  • 1
    \$\begingroup\$ @WiredUpDIY I mean, the fact that I created Vyxal probably has something to do with the fact that I know it well 😜. Learning a golfing language takes time and practice as one would need to spend if they were learning a practical language \$\endgroup\$
    – lyxal
    Jul 25 at 1:24
  • \$\begingroup\$ @wiredupdiy actually, Vyxal's pretty easy to understand (It's one of the few I understood), it was designed to be understandable and relatable to practical languages too. \$\endgroup\$
    – math
    Jul 25 at 7:36
  • \$\begingroup\$ 6 bytes lol get golfd \$\endgroup\$ Jul 26 at 15:52
5
\$\begingroup\$

Jelly, 7 bytes

O%32Ṅ€S

A full program that accepts a string of three letters and prints their alphabetic indices followed by their sum on four lines.

Try it online!

How?

O%32Ṅ€S - Main Link: list of characters, C
O       - cast to ordinals  A:65 ... Z:90 / a:97 ... z:122
 %32    - modulo 32         A:1  ... Z:26 / a:1  ... z:26
    Ṅ€  - print each followed by a new line
      S - sum
        - implicit print

9 if it had to be the first three of a string of letters (Try it online!):

ḣ3O%32Ṅ€S

...or 12 if it had to both handle non-alphabetic characters, and take the first three letters (Try it online!):

fØẠḣ3O%32Ṅ€S
\$\endgroup\$
0
4
\$\begingroup\$

Factor + combinators.extras, 42 35 bytes

0 [ read1 32 mod dup . + ] thrice .

Try it online!

-7 bytes thanks to @MarcMush

  • 0 Push 0, our sum, to the data stack.
  • [ ... ] thrice Call a quotation three times.
  • read1 Read one code point from standard input.
  • 32 mod Modulo 32.
  • dup . Output on its own line non-destructively.
  • + Add it to the sum.
  • . Print the sum.
\$\endgroup\$
1
3
\$\begingroup\$

APL (Dyalog Extended), 10 bytes

Full program. Prompts via stdin and prints to stdout

+⌿⎕←⍪⎕A⍳⌈⍞

Try it online!

 prompt via stdin

 uppercase

⎕A⍳ indices in uppercase Alphabet

 make into column

⎕← send to stdout

+⌿‌ sum (and implicitly print to stdout)

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

Elixir, 70 bytes

&(IO.puts Enum.reduce &1,0,fn b,c->IO.puts b=b-(b>91&&96||64)
c+b
end)

Try it online!

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

Perl 5 + -pF, 27 bytes

-6 bytes thanks to @Sisyphus!

$\+=$;*say$;=31&ord for@F}{

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Nice! Seeing it I'm sure I've seen that method in the last, but it never sticks! Thank you @Sisyphus! \$\endgroup\$ Jul 26 at 7:23
3
\$\begingroup\$

Python 3, 67 63 53 bytes

-10 bytes by dingledooper

i=[ord(c)%32for c in input()]
*map(print,i+[sum(i)]),

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Thats pretty much as condensed as python can get because it needs so much formatting. +1 😉 \$\endgroup\$
    – RMCodes
    Jul 25 at 21:05
  • 2
    \$\begingroup\$ 53 bytes is the best I could do. Still quite verbose, but that's Python for you. \$\endgroup\$ Jul 25 at 21:39
  • 2
    \$\begingroup\$ that map trick is nasty :) \$\endgroup\$
    – movatica
    Jul 26 at 20:41
3
\$\begingroup\$

brainfuck, 38 bytes

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

Try it online!

Explanation:

The first 13 plus signs put the 'newline' character into cell 0. Then, I initialize cell 1 as a counter with a value of 3. Then there's the main loop. This loop will get a character in cell 2, display it, display the 'newline' character in cell 0, add cell 2 to cell 3, which holds the running sum, and repeat until cell 1 has a value of zero. When the loop is done, it displays the value in cell 3, which, if you've been following, holds the running sum of the characters. However, there is a problem with this last part: it displays the sum of the three characters as an ascii character, not an integer. I don't know if this is actually an issue since I technically displayed the sum, but just in case, I added a (long) routine to the end of the code that displays the sum as an integer.

brainfuck, 242 bytes

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

Try it online!

But wait, there's more. The routine I used only supports displaying eight bit numbers. This means that if I input 'abc' into the program, it displays:

a
b
c
39

If you didn't catch it, the sum of the ascii values of 'a','b', and 'c' is not 39. However, it's equivalent if you add 255. This is because the value of the cell was only calculated to 8 bits by the routine I used. At this point, brainfuck had had its effect on my brain, and I didn't really feel like rewriting any more code in it. If I need to change my submission because of this issue or any other issue, let me know in the comments.

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

Jelly, 9 bytes

ŒuO_64Ṅ€S

Try it online!

Full program which takes a three-character string of letters as the first command line argument. 10 bytes to read from STDIN

How it works

ŒuO_64Ṅ€S - Main link. Takes [a,b,c] (a string) on the left
Œu        - Convert to uppercase
  O       - Ordinal
   _64    - Subtract 64 (A -> 1, B - 2 etc.)
       €  - Over each:
      Ṅ   -   Print with a trailing newline
        S - Take the sum, and print implicitly
\$\endgroup\$
4
2
\$\begingroup\$

Keg, 20 bytes

0&(3|?84*%:.
,&+&)&.

Try it online!

Why....why did I do this to myself?

Explained

0&

First of all, we put 0 into the register, because it is intialised as None. The register will be used to track the sum of the three letters.

(3|

Three times:

?84*%

Take the next letter and modulo its ASCII value by 32. This trick is stolen from Jonathan Allen's Jelly answer, so go upvote that too.

:.
,

Print the result of that modulo, followed by a newline (yes, newlines really do push newlines in Keg. This entire language is cursed, what did you expect?)

&+&

Push the value of the register, add the modulo'd index and then place that back in the register

)&.

Close the for loop and print the register raw.

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

JavaScript (Node.js), 61 bytes

x=>[...[a,b,c]=[...x].map(z=>parseInt(z,36)-9),a+b+c].join`
`

Try it online!

x=>[                   // Define a function taking a parameter x, returning an array of...
  ...                  // Iterating over...
    [a,b,c]=           // assign a, b and c to...
    [...x].map(z=>     // x mapped to...
      parseInt(z,36)-9 // parse as base36, -9 to get charcode
    ),                 
  a+b+c                // and the sum of all three
].join`\n`             // Joined by newline

The trick is that parseInt is case-insensitive, saving a lot of bytes.

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

Ruby, 36 Bytes

p $*.map{|c|p c.downcase.ord-96}.sum
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Nice answer. I've not seen args passed like this as a default before though, usually I'd expect to pass a whole string of three chars... Regardless, you can save a couple of bytes in your solution using upcase...-64: Try it online! \$\endgroup\$ Jul 25 at 15:37
  • \$\begingroup\$ 31 bytes (inspired by A username's JS answer) \$\endgroup\$
    – xigoi
    Jul 25 at 16:43
  • \$\begingroup\$ 27 bytes \$\endgroup\$
    – MarcMush
    Jul 25 at 19:11
  • \$\begingroup\$ 22 bytes, though I'm not convinced this input format is legit (at a quick glance, I couldn't find it under our default I/O methods). 24 bytes reading a string from STDIN. \$\endgroup\$
    – Dingus
    Jul 26 at 0:00
2
\$\begingroup\$

Julia 1.0, 38 bytes

r=read(stdin).%32
println.([r;sum(r)])

Try it online!

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

TI-BASIC, 135 132 bytes (on-calc) / 145 135 bytes (as text)

-3 bytes thanks to MarcMush!

Input Str1
For(I,1,3
int(.5inString(" AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz",sub(Str1,I,1
Disp Ans
A+Ans→A
End
A

Explanation

  • Input Str1: Take text input into string variable Str1.
  • For(I,1,3: Start a for-loop with variable I from 1 to 3 (TI-BASIC strings and lists are 1-indexed).
  • int(.5inString(" AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz",sub(Str1,I,1: Find the position of the current character of Str1 in the alphabet string, divide it by 2 and find the floor of it to get the number of the uppercase or lowercase letter. Parentheses at the end of a line in TI-BASIC don't need to be closed, so using .5 in front of inString( rather than /2 behind it saves a couple bytes' worth of parentheses.
  • Disp Ans: Display what the previous line returned (Ans).
  • A+Ans→A: Add Ans to the sum (Disp doesn't return anything so Ans remains the same).
  • End: End of For( loop.
  • A: Implicitly print the sum (variable A) at the end of the program.
\$\endgroup\$
2
  • \$\begingroup\$ nice first answer! you can save 3 bytes (I think) by removing your first line \$\endgroup\$
    – MarcMush
    Jul 25 at 20:16
  • \$\begingroup\$ @MarcMush oh cool, thanks! \$\endgroup\$
    – orangey
    Jul 25 at 20:28
2
\$\begingroup\$

SimpleTemplate 0.84, 107 77 bytes

This code expects 1 argument passed to the compiler, with the 3 characters used for input.

Outputs the text directly and doesn't return anything.

{@eachargv.0}{@callord intoZ _}{@set%A.[__]Z 32}{@/}{@set+Y A}{@echoj"\n"A,Y}

This is quite a messy mess...

But you can try it on http://sandbox.onlinephpfunctions.com/code/825bbd653f036fce8cc12f639c982ec5eed793ea (Please don't use PHP 8.0.0)

Thanks MarcMush for the idea that saved me 30 bytes.


Ungolfed:

This should be a lot easier to read.

{@set numbers as 0, 0, 0}

{@each argv.0 as letter key key}
    {@call ord into tmp letter}
    {@set% numbers.[key] tmp 32}
{@/}

{@set+ sum numbers}
{@echo separator "\n" numbers, sum}

Explanation:

I will explain how the golfed version works.

  • {@eachargv.0} - loops through the first argument. argv is an array with all passed arguments.
    The ungolfed version defines variables for the key as value: {@each argv.0 as letter key key}.
    The default names are _ for the value and __ for the key.

  • {@callord intoZ _} - calls the function ord passing _ as a value and storing into Z.
    Z is the same as tmp in the ungolfed version.

  • {@set%A.[__]Z 32} - set is used to define a variable, and you can pass it a value.
    If you add a % before the variable, it calculates the remainder of the values (in this case, Z divided by 32). By magic, this handles letters A-Z and a-z, and maps them to A>1, B>2 ... Z>26.
    The result is stored into A.[__] (numbers.[key] in the ungolfed version).

  • {@/} - closes the @each loop

  • {@set+Y A} - set is used to define a variable, and you can pass it a value. If you add a + after, it indicates that it will receive the sum of all the passed values.
    In this case, it will sum all the numbers (A) and store into Y. The ungolfed version has {@set+ sum numbers}, which demonstrates a lot better the way it works.

  • {@echoj"\n"A,Y} - Outputs A and Y, joined by "\n".
    The ungolfed version has {@echo separator "\n" numbers, sum}.
    @echoj is just a shorthand for @echo separator.
    All values are separated by "\n", regardless of their type. This doesn't output a newline at the end!

\$\endgroup\$
3
  • \$\begingroup\$ I don't know anything about this language but can you do letter > ord > %32 (or &31) instead of letter > upper > ord > -64 ? I guess it would save quite some bytes \$\endgroup\$
    – MarcMush
    Jul 25 at 23:18
  • \$\begingroup\$ @MarcMush I could try, yes, but the math is so incredibly rudimentar... To be honest, it was an afterthought. But luckly, I did implement the % operator, so, I will play around with it. \$\endgroup\$ Jul 25 at 23:36
  • 1
    \$\begingroup\$ @MarcMush I've managed to use your idea, and saved 30 bytes. It looks even uglier, but works. Thank you! \$\endgroup\$ Jul 26 at 0:04
2
\$\begingroup\$

PHP -F, 55 bytes

for(;$c=ord($argn[$i++])%32;$r+=$c)echo"$c
";echo"$r
";

Try it online!

Quite straightforward solution..

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

Grok, 35 bytes

j
:zhq
hx{y1py1pw*25xxY+zPY%*48py1p

Try it Online!

Gets input, then starts moving left through the main program, which juggles the register, where the running sum is stored, and the stack, where the input and some calculations are stored. Once all letters are printed and added, execution moves up to the middle row, where the sum is printed and execution ends.

\$\endgroup\$
1
  • \$\begingroup\$ +1 for a cool language name \$\endgroup\$
    – roblogic
    Aug 26 at 5:20
2
\$\begingroup\$

Wolfram Language (Mathematica), 38 bytes

(Print@Column[y=LetterNumber@#];Tr@y)&

Doesn't seem to run in Try it online!, so here are some screenshots:

enter image description here

enter image description here

And here is a superior 32-byte approach given by att in the comments:

Print/@##@*+##&@@LetterNumber@#&

Try it online!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ 33 bytes \$\endgroup\$
    – att
    Jul 25 at 5:34
  • \$\begingroup\$ @att You should post that as a separate answer. It's not a variant of my 38-byte answer; it's a different and superior approach. Is my 30-byte alternative allowed under Code Golf rules? \$\endgroup\$
    – theorist
    Jul 25 at 6:01
  • \$\begingroup\$ The alternative is not one of the default submission formats. \$\endgroup\$
    – att
    Jul 25 at 6:17
  • \$\begingroup\$ 32 bytes \$\endgroup\$
    – att
    Aug 26 at 0:38
1
\$\begingroup\$

C (gcc), 60 bytes

i,j;f(char*s){for(i=0;printf("%d\n",j=*s&31?:i),*s++;i+=j);}

Try it online!

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

Pyth, 11 bytes

j+=%R32CMQs

Try it here!

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

Red, 58 57 bytes

-1 byte thanks to dingledooper!

func[a][s: 0 foreach c a[s: s + probe 1 * c % 32]print s]

Try it online!

Takes a 3-letter string as input. Iterates over the string, converts each character to an integer (1 * c) and maps it to 1-26 by % 32. Prints this at each step using probe (it returns a value) and updates the sum, which is printed at the end.

Full program (works in Red console), 53 52 bytes

-1 byte thanks to dingledooper!

s: 0 foreach c input[s: s + probe 1 * c % 32]print s
\$\endgroup\$
2
  • 1
    \$\begingroup\$ It appears that you can remove those parentheses to save a byte. \$\endgroup\$ Jul 26 at 8:27
  • \$\begingroup\$ @dingledooper Thanks, indeed! The parentheses remained from an earlier version. \$\endgroup\$ Jul 26 at 8:47
1
\$\begingroup\$

Charcoal, 15 bytes

≔ES﹪℅ι³²θ⊞θΣθIθ

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

≔ES﹪℅ι³²θ

Map the input string into ASCII ordinals and reduce modulo 32.

⊞θΣθ

Append the sum.

Iθ

Output everything in decimal.

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

Excel, 65 bytes

=LET(n,ROW(1:4),a,MOD(CODE(MID(A1&" ",n,1)),32),IF(n=4,SUM(a),a))

Link to Spreadsheet

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

Japt -R, 9 bytes

Ëc %H
pUx

Try it

Ëc %H\npUx     :Implicit input of character array
Ë              :Map
 c             :  Charcode
   %H          :  Mod 32
     \n        :Assign to variable U
       p       :Push
        Ux     :  U reduced by addition
               :Implicit output joined with newlines
\$\endgroup\$
1
\$\begingroup\$

Google Sheets, 56

Ending parens discounted.

  • Input is in A1
  • B1
=ArrayFormula(-64+CODE(MID(UPPER(A1),{1;2;3},1)))
  • B4
=SUM(B1:B3)
\$\endgroup\$
1
  • \$\begingroup\$ Creative! I like your unorthodox method! Have an upvote! \$\endgroup\$
    – RMCodes
    Aug 13 at 15:31
1
\$\begingroup\$

05AB1E, 8 bytes

Ç32%€=O?

Try it online or verify the entire alphabet at once.

Explanation:

Ç         # Get the ASCII values of the characters in the (implicit) input-string
 32%      # Modulo each by 32
    €     # For-each over these integers:
     =    #  Print the integer with trailing newline (without popping)
      O   # Take the sum of the list
       ?  # And output it (without newline) as well
\$\endgroup\$
1
\$\begingroup\$

MathGolf, 7 bytes

$♥%_Σ▐n

Input as a list of characters.

Try it online.

Explanation:

$        # Get the ordinal value of each character in the (implicit) input-list
 ♥%      # Modulo-32
   _     # Duplicate this list of integers
    Σ    # Sum the duplicated list
     ▐   # Append it to the original list
      n  # Join this list by newlines
         # (after which the entire stack is output implicitly as result)
\$\endgroup\$
1
\$\begingroup\$

Using 3 inividual chars as input: (Not sure if allowed)

Java (JDK), 73 bytes

(a,b,c)->System.out.print(a%32+"\n"+b%32+"\n"+c%32+"\n"+(a%32+b%32+c%32))

Try it online!

Using a string as input:

Java (JDK), 110 bytes

s->{byte[]a=s.getBytes();System.out.print(a[0]%32+"\n"+a[1]%32+"\n"+a[2]%32+"\n"+(a[0]%32+a[1]%32+a[2]%32));};

Try it online!

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

Zsh, 43 bytes

for i;{j=$[##$i:u-64];<<<$j;((k+=j))}
<<<$k

try it online!

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

jq, 22 bytes

[explode[]%32]|.[],add

Try it online!

quite useful that .[] prints numbers with newlines.

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