【每日一题1217】ASCII之和

:woman_mage: 给定一个字符串,请编写一个函数,统计所有字符对应的ASCII码数值的总和。

示例:
输入:“a”,返回:97。
输入:“aaa”,返回:291。

题目难度:简单
题目来源:CodeWars: ASCII Total

def solution(words: str) -> int:
    # your code

assert solution("a") == 97
assert solution("aaa") == 291
assert solution("Mary had a little lamb") == 2001
def solution(words: str) -> int:
    return sum([ord(i) for i in words])


assert solution("a") == 97
assert solution("aaa") == 291
assert solution("Mary had a little lamb") == 2001
def solution(words: str) -> int:
    # your code
    return sum(ord(i) for i in words)


assert solution("a") == 97
assert solution("aaa") == 291
assert solution("Mary had a little lamb") == 2001
def solution(words: str) -> int:
    result = 0
    for i in words:
        result += ord(i)
    return result
def solution_3(a:str):
    numic=0
    for i in a:
        numic+=ord(i)
    return numic
# print(solution_3('Mary had a little lamb'))
assert solution_3("a") == 97
assert solution_3("aaa") == 291
assert solution_3("Mary had a little lamb") == 2001
def solution(words: str) -> int:
    sum = 0
    for i in words:
        sum += ord(i)
    return sum

assert solution("a") == 97
assert solution("aaa") == 291
assert solution("Mary had a little lamb") == 2001
def solution(words: str) -> int:
    return sum(ord(i) for i in words)

assert solution("a") == 97
assert solution("aaa") == 291
assert solution("Mary had a little lamb") == 2001
def solution(words: str) -> int:
    # ord:对字符串转换为ASCII值
    result = [ord(i) for i in words]
    # sum:返回所有字符对应的ASCII码数值的总和
    return sum(result)


assert solution("a") == 97
assert solution("aaa") == 291
assert solution("Mary had a little lamb") == 2001
def solution(words: str) -> int:
    return sum(ord(i) for i in words)

assert solution("a") == 97
assert solution("aaa") == 291
assert solution("Mary had a little lamb") == 2001
def solution(words: str) -> int:
    return sum(ord(x) for x in words)
def solution(words: str) -> int:
    return sum([ord(item) for item in list(words)])

assert solution("a") == 97
assert solution("aaa") == 291
assert solution("Mary had a little lamb") == 2001
关闭