Open In App

Python – Maximum Scoring word

Given a String, the task is to write a Python program to compute maximum scoring words i.e words made of characters with a maximum positional summation.

Examples:



Input : test_str = ‘geeks must use geeksforgeeks for cs knowledge’
Output : geeksforgeeks
Explanation : Sum of characters positional values for “geeksforgeeks” word is maximum, hence result.

Input : test_str = ‘geeks love geeksforgeeks’
Output : geeksforgeeks
Explanation : Sum of characters positional values for “geeksforgeeks” word is maximum, hence result.



Method #1 : Using split() + loop + ord() + ascii_lowercase

In this, we initially split each word using split(), get positional magnitude using ord(), ascii_lowercase checks for correct pool of characters chosen for evaluation. 




# Python3 code to demonstrate working of
# Maximum Scoring word
# Using split() + loop + ord() + ascii_lowercase
import string
 
# initializing string
test_str = 'geeks must use geeksforgeeks for cs knowledge'
 
# printing original string
print("The original string is : " + str(test_str))
 
score = 0
max_sc = 0
res = ''
for wrd in test_str.split():
    score = 0
    # computing score
    for lttr in wrd:
        if lttr in string.ascii_lowercase:
            score += ord(lttr) - 96
 
    # updating maximum
    if score > max_sc:
        max_sc = score
        res = wrd
 
# printing result
print("Maximum scoring word : " + str(res))

Output:

The original string is : geeks must use geeksforgeeks for cs knowledge
Maximum scoring word : geeksforgeeks

Method #2 : Using sum() + loop + ord()

Similar to above method, only difference here being sum() is used for task of summation rather than internal loop.




# Python3 code to demonstrate working of
# Maximum Scoring word
# Using sum() + loop + ord()
import string
 
# initializing string
test_str = 'geeks must use geeksforgeeks for cs knowledge'
 
# printing original string
print("The original string is : " + str(test_str))
 
score = 0
max_sc = 0
res = ''
for wrd in test_str.split():
 
    # computing score
    # sum for cumulation
    score = sum(ord(lttr) - 96 for lttr in wrd if lttr in string.ascii_lowercase)
 
    # updating maximum
    if score > max_sc:
        max_sc = score
        res = wrd
 
# printing result
print("Maximum scoring word : " + str(res))

Output:

The original string is : geeks must use geeksforgeeks for cs knowledge
Maximum scoring word : geeksforgeeks

Time Complexity: O(n2)
Auxiliary Space: O(n)

Method 3: Using a dictionary to store scores of each word:

Step-by-step approach:




def max_scoring_word_2(test_str):
    words = test_str.split()
    scores = {}
    for word in words:
        score = 0
        for char in word:
            score += ord(char)
        scores[word] = score
    return max(scores, key=scores.get)
 
test_str = 'geeks love geeksforgeeks'
print(max_scoring_word_2(test_str))  # Output: geeksforgeeks

Output
geeksforgeeks

Time complexity: O(n*m), where n is the number of words and m is the maximum length of a word
Auxiliary Space: O(n)

Method #3: Using map() + sum() + max()




import string
 
# initializing string
test_str = 'geeks must use geeksforgeeks for cs knowledge'
 
# printing original string
print("The original string is : " + str(test_str))
 
# define function to calculate score of a word
score_func = lambda word: sum(map(lambda c: ord(c) - 96, word))
 
# split input string into a list of words
words = test_str.split()
 
# calculate the score of each word and find the word with maximum score
res = max(words, key=score_func)
 
# printing result
print("Maximum scoring word : " + str(res))

Output
The original string is : geeks must use geeksforgeeks for cs knowledge
Maximum scoring word : geeksforgeeks

Time Complexity: O(nk + m).

Auxiliary Space: O(k + m).


Article Tags :