15
\$\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
  • 6
    \$\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, 2021 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\$
    – REMCodes
    Jul 25, 2021 at 0:48

40 Answers 40

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

Acc!!, 122 bytes

Count i while i-4 {
(_/80+_%80)*80+N%32
_/80^(i/3)
Count t while _%80/10*0^t {
Write _%80/10+48
}
Write _%10+48
Write 10
}

Try it online!

Explanation

We conceptually partition the accumulator into two sections, _/80 and _%80. The latter holds each letter's index; the former holds the running sum of letter indices. (The number 80 is chosen because it's both divisible by 10 and larger than any number we'll have to handle.) We loop four times: each time, we read a new character into _%80 and add the previous character's index to _/80, but on the fourth iteration we then divide the accumulator by 80, effectively moving the sum into the _%80 slot. We then output the one-or-two-digit number in _%80, followed by a newline.

# Loop four times
Count i while i - 4 {
    # Set _ to: sum of _/80 and _%80, times 80, plus character code from stdin mod 32
    (_ / 80 + _ % 80) * 80 + (N % 32)
    # If i = 3, divide _ by 80; if i < 3, no-op
    _ / 80 ^ (i / 3)
    # Loop once if _%80 >= 10
    Count t while (_ % 80) / 10 * (1 - t) {
        # Print the tens digit of _%80
        Write (_ % 80) / 10 + 48
    }
    # Print the ones digit of _%80
    Write (_ % 80) % 10 + 48
    # Print a newline
    Write 10
}
\$\endgroup\$
1
\$\begingroup\$

Zsh, 38 bytes

for i;echo $[j=##$i:u-64,k+=j,j]
<<<$k

Try it online!

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

Fig, \$8\log_{256}(96)\approx\$ 6.585 bytes

SM',%32C

Try it online!

Sorta-port of 05AB1E.

SM',%32C # Takes input as a list of chars
 M'      # Map each char
       C # Get the charcode
    %32  # Modulo 32
   ,     # And print (returning the thing printed)
S        # Sum the resulting list
         # Which gets implicitly printed
\$\endgroup\$
1
\$\begingroup\$

Thunno N, \$ 7 \log_{256}(96) \approx \$ 5.76 bytes

O32%DST

Attempt This Online!

Explanation

O32%DST  # Implicit input
O        # Ordinals
 32%     # Mod 32
    D    # Duplicate
     ST  # Append sum
         # N flag joins by newlines
         # Implicit output
\$\endgroup\$
1
\$\begingroup\$

Java 8 (OpenJDK 8), 75 107 bytes

-32 bytes.
I don't know why i didn't think sooner of using peek even though it has been invented specifically for this kind of case!

Using only a single inline Java Stream, takes String as parameter.

s->System.out.print(s.chars().map(e->e%32).peek(System.out::println).sum())

Try it online!

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

JavaScript (Node.js), 51 55 bytes

-4 bytes thanks to @xigoi !

For the first time, i got the shortest JS answer!

s=>Buffer(s).map(e=>(a+=e%=32,e),a=0).join`
`+`
`+a

Try it online!


Alternative solution using an accumulator for the resulting string to try to get rid of the join and the double newline (56 bytes, improved with @xigoi's tip too) :

s=>Buffer(s).map(e=>(a+=(e%=32)+`
`,b+=e),a="",b=0)&&a+b

Alternative way to calculate the sum and regroup before joining (56 bytes. I wonder if the 2 join could be declared only once and called 2 times with different parameters) :

s=>[...r=Buffer(s).map(e=>e%32),eval(r.join`+`)].join`
`
\$\endgroup\$
3
  • 1
    \$\begingroup\$ 53 bytes \$\endgroup\$
    – xigoi
    Mar 21 at 19:43
  • 1
    \$\begingroup\$ Actually, 51 bytes \$\endgroup\$
    – xigoi
    Mar 21 at 19:46
  • 1
    \$\begingroup\$ @xigoi Thank you for your tip! I updated my answer :) \$\endgroup\$
    – Fhuvi
    Mar 21 at 22:01
0
\$\begingroup\$

CSASM v2.5.1, 155 bytes

func main:
in ""
conv ~arr:char
dup
dup
lda 0
sta $1
.lbl a
ldelem $1
conv i32
push 31
and
dup
print.n
push $a
add
pop $a
inc $1
push $1
push 3
sub
brtrue a
push $a
print.n
ret
end

A simple loop which goes over each character.

Explanation:

func main:
    ; Get the input as a string, then convert it to an array of characters
    in ""
    conv ~arr:char

    ; Duplicate the array twice
    dup
    dup

    ; Initialize $a and $1 to 0
    lda 0
    sta $1
    
.lbl a
    ; Load the character at index $1, convert it to an integer, bitwise-AND it with 31, duplicate it, then print it
    ldelem $1
    conv i32
    push 31
    and
    dup
    print.n

    ; Add the duplicated value to $a
    push $a
    add
    pop $a

    ; Increment $1 and break from the loop when $1 == 3
    inc $1
    push $1
    push 3
    sub
    brtrue a

    ; Print $a, which now contains the sum of the characters
    push $a
    print.n

    ret
end
\$\endgroup\$
0
\$\begingroup\$

Alice, 28 bytes

!3wi' %.?+!/ O \t.$K?/ O \@

Try it online!

The trailing new line is required for the ordinal mode to work.

!          Initialises the sum to 0
3w         Main loop with 3 iterations
i' %       Reads one character from the input as its ascii code, modulus 32 (32 is the ascii code of <space>)
.?+!       Adds the index of the character to the sum
/ O \      Prints the current character index
t.$K       End of the loop
?/ O \@    Prints the sum and exits
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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