其他分享
首页 > 其他分享> > [6 kyu] Highest Scoring Word

[6 kyu] 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.

Solution :

def word_value(word):
      values = {
        'a': 1,
        'b': 2,
        'c': 3,
        'd': 4,
        'e': 5,
        'f': 6,
        'g': 7,
        'h': 8,
        'i': 9,
        'j': 10,
        'k': 11,
        'l': 12,
        'm': 13,
        'n': 14,
        'o': 15,
        'p': 16,
        'q': 17,
        'r': 18,
        's': 19,
        't': 20,
        'u': 21,
        'v': 22,
        'w': 23,
        'x': 24,
        'y': 25,
        'z': 26
      }
      value = 0
      for letter in word:
          value += values[letter]
      return value

def high(x):
    word_list = x.split()
    word_values = []
    
    for word in word_list:
        word_values.append(word_value(word))

    result = word_list[word_values.index(max(word_values))]
    
    return result
    # Code here

标签:Scoring,word,string,value,return,values,letter,Word,Highest
来源: https://blog.csdn.net/HM_773_220/article/details/104772129