给定一串单词,你需要找到最高的得分单词。
一个字的每个字母根据它在字母表中的位置得分: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')
一个字的每个字母根据它在字母表中的位置得分: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])
本文介绍了一个简单的算法,用于从给定的一串单词中找出字母总分最高的单词,并提供了Python实现代码。该算法通过计算每个单词中字母的ASCII值来确定其得分。
1050

被折叠的 条评论
为什么被折叠?



