# Daily Challenge #191 - Alphabetical Addition

### Setup

Write a function that can add letters together. Each letter will be valued based on their position in the alphabet.

Rules:

• Letters will always be lowercase.
• Letters can overflow (see second to last example of the description)
• If no letters are given, the function should return 'z'

### Examples

addLetters('a', 'b', 'c') = 'f'
addLetters('y', 'c', 'b') = 'd' // notice the letters overflowing
addLetters() = 'z'

### Tests

addLetters('a', 'b', 'c')
addLetters('z')
addLetters('a','b')
addLetters('c')
addLetters('y', 'c', 'b')
addLetters()

Good luck!

This challenge comes from steffan153 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Posted on by:

### dev.to staff

The hardworking team behind dev.to ❤️

### Discussion

C++

// takes letters in a vector
int sumLetters = 0; // holds the sum
for(char c : letters){
sumLetters += (c-'a'+1);
}

// if empty vector is passed returns 'z'
if(sumLetters==0)
return 'z';
return (sumLetters-1)%26 +97;
}

// return 'z'
return 'z';
}


Python

def addLetters(*args):
return chr((sum(map(lambda arg: (ord(arg) - ord('a') + 1),args)) - 1) % 26 + 97)


Python

var = input("Enter input: ")
var = list(var)

if len(var) == 0:
return 'z';
else:
sum = 0
for i in var:
sum += ord(i) - 96

while sum > ord('z') - 96:
sum -= ord('z') -96

return chr(sum + 96)



OCaml, takes an int list:

let add_letters letters =
letters
|> List.fold ~init:0 ~f:(fun s l -> s + Char.to_int l - 96)
|> function
| 0 | 26 -> 'z'
| i -> Char.of_int_exn (96 + (i mod 26))


Tried to Golf it with JS

function addLetters(...l) {
return String.fromCharCode(96 + (l.reduce((s, c) => s + c.charCodeAt() - 96, 0) % 26 || 26))
}


Go:

import (
"unicode/utf8"
)

if len(letters) == 0 {
r, _ := utf8.DecodeRuneInString("z")
return r
}
var (
t, tt int
base  = 96
)
for _, letter := range letters {
t += int(letter) - base
}
if tt = t % 26; tt == 0 {
tt = 26
}
return rune(tt + base)
}


## Ruby

def addLetters(*args)
sum = args
.map{ |letter| letter.ord - 96 }
.sum
sum  = sum % 26
sum = 26 if sum == 0
(sum + 96).chr
end

puts addLetters('a', 'b', 'c')# = 'f'
puts addLetters('y', 'c', 'b') #= 'd'


python

def add_letter(*letters):
letters = list(letters)
start_num = 96
total = start_num

if len(letters) == 0:
letters.append('z')

for letter in letters:
letter = str(letter).lower()
num = ord(letter)

if num > start_num:
total += (num - start_num)

if total > 122:
total = (total - 122) + start_num

print 'sum:', chr(total)

# ---------------------

add_letter('a', 'B', 'z')  # = c
add_letter('a', 'b', 'c')  # = 'f'
add_letter('y', 'c', 'b')  # = 'd'


### Javascript

let letters = '%abcdefghijklmnopqrstuvwxyz'

const addLetters =(...args) => args.length == 0 ? 'z' : letters[(args.map(argument => letters.indexOf(argument)).reduce((a,b) => a + b , 0)) % 26]



Rust solution

fn add_letters(chars: Vec<char>) -> char {
if chars.is_empty() {
return 'z';
}
let a_num = 'a' as u32;
std::char::from_u32((chars.iter().map(|&c| c as u32 - a_num + 1).sum::<u32>() % 26) + a_num - 1)
.unwrap()
}