Highest Scoring Word

jenny
Μέλος
που συμμετέχουν: 2025-08-24 15:11:06
2024-06-04 11:33:33

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:21:20

Well, the solution to this 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:`

` max_score = score`

` highest_word = word`

` elif score == max_score and words.index(word) < words.index(highest_word):`

` highest_word = word`

` `

` return highest_word`

`# Example usage:`

`input_string = "abad ace bat cat"`

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

jenny
Μέλος
που συμμετέχουν: 2025-08-24 15:11:06
2024-07-25 10:37:18
[[9,31],[29]]
Facebook X (Twitter) Instagram LinkedIn Telegram WhatsApp