Python:每日一题 154:找到最高的得分单词

给定一串单词,你需要找到最高的得分单词。
一个字的每个字母根据它在字母表中的位置得分:a = 1,b = 2,c = 3等
您需要将最高得分的单词作为字符串返回。
如果两个单词得分相同,则返回原始字符串中最早出现的单词。
所有字母都是小写字母,所有输入都是有效的。
Test:
test.assert_equals(high('man i need a taxi up to ubud'), 'taxi')
test.assert_equals(high('what time are we climbing up the volcano'), 'volcano')

test.assert_equals(high('take me to semynak'), 'semynak')


Python源码:

def score(word):
    s = 0
    for i in word:
        s += ord(i) - 96
    return s
sentence = input()
words = sentence.split(' ')
scores = list(zip(map(score, words), words))
scores.sort(key=lambda x:x[0], reverse=True)
print(scores[0][1])

猜你喜欢

转载自blog.csdn.net/hcmdghv587/article/details/79933994