Skip to content

Latest commit

 

History

History
53 lines (29 loc) · 915 Bytes

Highest_Scoring_Word.md

File metadata and controls

53 lines (29 loc) · 915 Bytes

CodeWars Python Solutions


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.


Given Code

def high(x):
    pass

Solution 1

def high(x):
    alp = "abcdefghijklmnopqrstuvwxyz"
    counter = [sum([alp.find(i) + 1 for i in w]) for w in x.split()]
    return x.split()[counter.index(max(counter))]

Solution 2

def high(x):
    return max(x.split(), key=lambda k: sum(ord(c) - 96 for c in k))

See on CodeWars.com