Back to Freecodecamp

Challenge 204: Sum the Letters

curriculum/challenges/english/blocks/daily-coding-challenges-python/698a1a73ade5ac0e19180fa7.md

latest1.8 KB
Original Source

--description--

Given a string, return the sum of its letters.

  • Letters are A-Z in uppercase or lowercase
  • Letter values are: "A" = 1, "B" = 2, ..., "Z" = 26
  • Uppercase and lowercase letters have the same value.
  • Ignore all non-letter characters.

--hints--

sum_letters("Hello") should return 52.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(sum_letters("Hello"), 52)`)
}})

sum_letters("freeCodeCamp") should return 94.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(sum_letters("freeCodeCamp"), 94)`)
}})

sum_letters("The quick brown fox jumps over the lazy dog.") should return 473.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(sum_letters("The quick brown fox jumps over the lazy dog."), 473)`)
}})

sum_letters("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ex nisl, pretium eu varius blandit, facilisis quis eros. Vestibulum ante ipsum primis in faucibus orci.") should return 1681.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(sum_letters("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ex nisl, pretium eu varius blandit, facilisis quis eros. Vestibulum ante ipsum primis in faucibus orci."), 1681)`)
}})

sum_letters("</404>") should return 0.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(sum_letters("</404>"), 0)`)
}})

--seed--

--seed-contents--

py
def sum_letters(s):

    return s

--solutions--

py
def sum_letters(s):
    total = 0
    for char in s:
        upper = char.upper()
        if 'A' <= upper <= 'Z':
            total += ord(upper) - ord('A') + 1
    return total