Highest Scoring Word

jenny
انضم: 2025-08-24 15:11:06
2024-06-05 19:26:28

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.

For example, the score of `abad` is `8` (1 + 2 + 1 + 4).

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.

Image preview

jenny
انضم: 2025-08-24 15:11:06
2024-07-02 22:24:53

The solution is:

`def highest_scoring_word(s):`

` def word_score(word):`

` return sum(ord(char) - ord('a') + 1 for char in word)`

` `

` words = s.split()`

` max_score = 0`

` highest_word = ""`

` `

` for word in words:`

` score = word_score(word)`

` if score > max_score or highest_word == "":`

` max_score = score`

` highest_word = word`

` `

` return highest_word`

`# Example usage:`

`input_string = "abad ace bat cat"`

`print(highest_scoring_word(input_string)) # Output: "abad"`

jenny
انضم: 2025-08-24 15:11:06
2024-07-25 10:36:58
[[9,31],[29]]
Facebook X (Twitter) Instagram LinkedIn Telegram WhatsApp