DEV Community

dev.to staff
dev.to staff

Posted on

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('a', 'b') = 'c'
addLetters('z') = 'z'
addLetters('z', 'a') = 'a'
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!

Top comments (8)

Collapse
 
vidit1999 profile image
Vidit Sarkar

C++

// takes letters in a vector
char addLetters(vector<char> letters){
    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;
}

// overloaded function for special case : addLetters()
// return 'z'
char addLetters(){
    return 'z';
}

Python

def addLetters(*args):
  return chr((sum(map(lambda arg: (ord(arg) - ord('a') + 1),args)) - 1) % 26 + 97)
Collapse
 
maskedman99 profile image
Rohit Prasad

Python

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

def addLetters(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)

print(addLetters(var))
Collapse
 
jay profile image
Jay

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()
}
Collapse
 
khauri profile image
Khauri

Tried to Golf it with JS

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

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('a', 'b') #= 'c'
puts addLetters('z') #= 'z'
puts addLetters('z', 'a') #= 'a'
puts addLetters('y', 'c', 'b') #= 'd'
puts addLetters() #= 'z'
Collapse
 
amcmillan01 profile image
Andrei McMillan

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('z', 'a')  # = a
add_letter('a', 'b', 'c')  # = 'f'
add_letter('a', 'b')  # = 'c'
add_letter('z')  # = 'z'
add_letter('z', 'a')  # = 'a'
add_letter('y', 'c', 'b')  # = 'd'
add_letter()  # = 'z'
Collapse
 
_bkeren profile image
''

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]


Collapse
 
mrdulin profile image
official_dulin

Go:

import (
    "unicode/utf8"
)

func AddLetters(letters []rune) rune {
    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)
}