Highest Scoring Word
Given a string of words, you need to find the highest scoring word.
Each letter of a word scores points according to its position in the alphabet: a = 1, b = 2, c = 3 etc.
You need to return the highest scoring word as a string.
If two words score the same, return the word that appears earliest in the original string.
All letters will be lowercase and all inputs will be valid.
For example
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’)
test.assert_equals(high(‘aa b’), ‘aa’)
test.assert_equals(high(‘b aa’), ‘b’)
test.assert_equals(high(‘bb d’), ‘bb’)
test.assert_equals(high(‘d bb’), ‘d’)
test.assert_equals(high(“aaa b”), “aaa”)
Algorithm analysis
输入字符串,得到其中一个字符串的一个数据元素。需要得到每一个数据元素对应的分数。分数利用ord(c)-96(注意:这里的c的长度只能为1,即只能是一个字符),得到A到Z对应的分数。需要先拆分字符串用split(),for循环可能需要用到两次。
Algorithm details
- 拆分字符串;
- 再拆分每一个数据元素,转码并求对应的和;
- 选最大的数;
- 返回最大数索引对应的字符
code
def high(x):
words=x.split(' ')
list = []
for i in words:
scores = [sum([ord(char) - 96 for char in i])]
list.append(scores)
return words[list.index(max(list))]
Thanks for reading