- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to find Maximum Scoring Word
In Python, strings are a fundamental data type used to represent sequences of characters. They are enclosed in single quotes ('') or double quotes ("") and offer a wide range of operations and methods for manipulation
Example
Assume we have taken an input string. We will now find the maximum scoring word from an input string using the above methods.
Input
inputString = "hello tutorialspoint python users and learners"
Output
Resultant maximum scoring word from the input string: tutorialspoint
In the above input string, the sum of characters' positional values of the word tutorialspoint is having the maximum value. Hence it is the maximum-scoring word in an input string.
Method 1: Using for loop, split(), ord(), ascii_lowercase functions
In this method we are going to use combination of for loop, split(), ord(), ascii_lowercase functions to find maximum scoring word.
The ascii_lowercase is a pre-initialized string that is used as a string constant in Python 3. Python's string ascii_lowercase returns lowercase letters.
ord() function(returns the Unicode code/ASCII value of a given character as a number)
ascii_lowercase(returns lowercase letters)
string.split(separator, maxsplit) ord(character) string.ascii_lowercase
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task –.
Use the import keyword to import the string module.
Create a variable to store the input string and print the given string.
Initialize a variable with 0 to state the current word score.
Initialize another variable with 0 to store the maximum score.
Create an empty string to store the maximum scoring word from an input string.
Use the split() function(splits a string into a list. We can define the separator; the default separator is any whitespace) to split the input string into a list of words and traverse through words of that list using for loop.
Initialize the score of the current word as 0.
Use another nested for loop to traverse through each character of the current word.
Use the if conditional statement to check whether the current character is lowercase.
Add the ASCII value difference of the current character to the score.
Use the if conditional statement to check whether the score of the current word is greater than the maximum score.
Update the maximum score with the current word score if the condition is true.
Update the current word as the maximum scoring word.
Print the resultant maximum scoring word from the input string
Example
The following program returns the maximum scoring word from the input string using for loop, split(), ord(), and ascii_lowercase functions
# importing string module import string # input string inputString = "hello tutorialspoint python users and learners" # Printing input string print("Input string: ", inputString) # Storing current word score curr_score = 0 # Storing maximum score maximumScore = 0 # empty string for storing the maximum scoring word from a string maxScoreWord = '' # Splitting input string into a list of words and traversing through words of that list for word in inputString.split(): # Initializing the score of the current word as 0 curr_score = 0 # Traversing through each character of the current word for character in word: # Checking whether the character is lowercase if character in string.ascii_lowercase: # Adding the ASCII value difference of the current character to the score curr_score += ord(character) - 96 # checking whether the score of the current word is greater than the maximum score if curr_score > maximumScore: # updating the maximum score with the current word score if the condition is true maximumScore = curr_score # updating the current word as the maximum scoring word maxScoreWord = word # printing resultant maximum scoring word from the input string print("Resultant maximum scoring word from the input string: ", maxScoreWord)
Output
Input string: hello tutorialspoint python users and learners Resultant maximum scoring word from the input string: tutorialspoint
Method 2: Using for loop, sum() and ord() functions
In this method, we will learn how to use simple for loop(), sum() and ord() function to find the maximum scoring word.
sum() function(returns the sum of all items in an iterable)
ord() function(returns the Unicode code/ASCII value of a given character as a number)
split() function(splits a string into a list. We can define the separator; the default separator is any whitespace)
sum(iterable, start=0) ord(character)
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task –
Use the split function to split the input string into a list of words and traverse through words of that list using for loop.
Get the sum of differences ASCII values of each character of the word by iterating through the word and checking whether it is in lowercase letters.
Use the if conditional statement to check whether the score of the current word is greater than the maximum score.
Update the maximum score with the current word score if the condition is true.
Update the current word as the maximum scoring word.
Print the resultant maximum scoring word from the input string
Example
The following program returns the maximum scoring word from the input string for loop, sum(), and ord() functions –
# importing string module import string # input string inputString = "hello tutorialspoint python users and learners" # Printing input string print("Input string: ", inputString) # Storing current word score curr_score = 0 # Storing maximum score maximumScore = 0 # empty string for storing the maximum scoring word from a string maxScoreWord = '' # Splitting input string into a list of words and traversing through words of that list for word in inputString.split(): # getting the score of current word curr_score = sum( ord(character) - 96 for character in word if character in string.ascii_lowercase) # checking whether the score of the current word is greater than the maximum score if curr_score > maximumScore: # updating the maximum score with the current word score if the condition is true maximumScore = curr_score # updating the current word as the maximum scoring word maxScoreWord = word # printing resultant maximum scoring word from the input string print("Resultant maximum scoring word from the input string: ", maxScoreWord)
Output
Input string: hello tutorialspoint python users and learners Resultant maximum scoring word from the input string: tutorialspoint
Conclusion
In conclusion, the provided code demonstrates two different methods to find the maximum scoring word from an input string. Both methods involve iterating through the words of the input string and calculating a score based on the ASCII values of the characters. The maximum scoring word is determined by comparing the calculated scores. The code showcases the usage of string manipulation, iteration, conditional statements, and built-in functions in Python to achieve the desired result.