Python Program to find Maximum Scoring Word

In Python, finding the maximum scoring word involves calculating scores for each word based on character positions in the alphabet. A character's score equals its position (a=1, b=2, c=3, etc.), and the word with the highest total score wins.

Problem Understanding

Given an input string, we need to find the word with the maximum score where each character contributes its alphabetical position value ?

Input

input_string = "hello tutorialspoint python users and learners"
print("Input string:", input_string)
Input string: hello tutorialspoint python users and learners

In this example, "tutorialspoint" has the highest score because the sum of its character positions is maximum among all words.

Method 1: Using for Loop with ord() Function

This approach uses nested loops to calculate each word's score by converting characters to their ASCII values ?

import string

def find_max_scoring_word_method1(input_string):
    words = input_string.split()
    max_score = 0
    max_word = ""
    
    for word in words:
        current_score = 0
        for char in word:
            if char in string.ascii_lowercase:
                # Calculate position: a=1, b=2, etc.
                current_score += ord(char) - ord('a') + 1
        
        if current_score > max_score:
            max_score = current_score
            max_word = word
    
    return max_word, max_score

# Test the function
input_string = "hello tutorialspoint python users and learners"
result_word, score = find_max_scoring_word_method1(input_string)
print(f"Maximum scoring word: {result_word}")
print(f"Score: {score}")
Maximum scoring word: tutorialspoint
Score: 192

Method 2: Using sum() with Generator Expression

This method uses Python's sum() function with a generator expression for more concise code ?

import string

def find_max_scoring_word_method2(input_string):
    words = input_string.split()
    max_score = 0
    max_word = ""
    
    for word in words:
        # Calculate score using sum() and generator expression
        current_score = sum(ord(char) - ord('a') + 1 
                          for char in word 
                          if char in string.ascii_lowercase)
        
        if current_score > max_score:
            max_score = current_score
            max_word = word
    
    return max_word, max_score

# Test the function
input_string = "hello tutorialspoint python users and learners"
result_word, score = find_max_scoring_word_method2(input_string)
print(f"Maximum scoring word: {result_word}")
print(f"Score: {score}")
Maximum scoring word: tutorialspoint
Score: 192

Method 3: Using max() Function

The most Pythonic approach uses the built-in max() function with a key parameter ?

import string

def calculate_word_score(word):
    return sum(ord(char) - ord('a') + 1 
              for char in word 
              if char in string.ascii_lowercase)

def find_max_scoring_word_method3(input_string):
    words = input_string.split()
    max_word = max(words, key=calculate_word_score)
    max_score = calculate_word_score(max_word)
    return max_word, max_score

# Test the function
input_string = "hello tutorialspoint python users and learners"
result_word, score = find_max_scoring_word_method3(input_string)
print(f"Maximum scoring word: {result_word}")
print(f"Score: {score}")

# Show scores for all words
words = input_string.split()
for word in words:
    score = calculate_word_score(word)
    print(f"{word}: {score}")
Maximum scoring word: tutorialspoint
Score: 192
hello: 52
tutorialspoint: 192
python: 121
users: ?
and: 19
learners: 96

Comparison

Method Readability Performance Code Length
Nested loops Good Standard Longer
sum() with generator Better Good Medium
max() function Best Best Shortest

Conclusion

The max() function with a key parameter provides the most elegant solution for finding maximum scoring words. Use generator expressions with sum() for readable and efficient scoring calculations.

Updated on: 2026-03-27T12:54:46+05:30

279 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements