← Back to challenges

Adding Up Letters

PythonHardarrayslogic

Instructions

Create a function add_letters that takes a list/array of letters a, and returns the "sum" of them.

To add two letters, take their number value, add them together, and convert it back together. For example, a would be 1, b would be 2, etc. So to add b and c, take 2 + 3 = 5, and then get the fifth letter of the alphabet (e).

So then d + e + f would be 4 + 5 + 6 = 15, and the fifteenth letter is o, so that's what you return.

Letters can also wrap. Like with y + c, that's 25 + 3 = 28, which doesn't exist. Consider that the 27th letter just wraps around and ends back up at a. With this logic, y + c = b. Don't just do 27 = 1 though, because you could end up with a much higher sum like 70.

Examples

add_letters(["a"]) ➞ "a"
add_letters(["a", "b"]) ➞ "c"
add_letters(["a", "b", "c"]) ➞ "f"
add_letters(["a", "b", "c", "d", "e", "f"]) ➞ "u"
add_letters(["y", "a"]) ➞ "z"
add_letters(["y", "c"]) ➞ "b"
add_letters(["z", "a"]) ➞ "a"
add_letters(["x", "y", "z"]) ➞ "w"
add_letters([]) ➞ "z"

Notes

  • An empty array should return z. The logic behind this is that if you get a sum of 0, then wrap it all the way around backwards (since the 0th letter doesn't exist), giving you 26 which = z.
  • All letters given will be lowercase.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.