Codewars - Highest Scoring Word Here is my solution for CodeWars - Highest Scoring Game


  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
  
  Example: 
  
  Input: 'man i need a taxi up to ubud'
  
  Output: 'taxi'


This is my solution:

def word_value(input_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 input_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))

    max_value = max(word_values)
    index_of_max = word_values.index(max_value)

    return word_list[index_of_max]


The solution passed the test suite but I think I can improve the part where I store all the letters matched with their value. Is there any suggestions for this and in general the entire code?

Thank you.
Indeed, the values dictionary looks unnecessary. Using a built-in function ord you could compute the letter score with ord(letter) - ord('a') + 1. One may argue that it is even faster than a dictionary lookup, but in this case the timing difference is rather marginal.



With Python you should avoid rolling explicit loops. For example, collecting the word values is more idiomatically expressed as a comprehension:

    word_values = [word_value(word) for word in word_list]


Similarly, instead of

    for letter in input_word:
        value += values[letter]


consider functools.reduce:

    value = functools.reduce(lambda x, y: x + ord(y) - ord('a') + 1, input_word, 0)