Python program to print words from a sentence with highest and lowest ASCII value of characters
Last Updated :
08 Feb, 2024
Given a string S of length N, representing a sentence, the task is to print the words with the highest and lowest average of ASCII values of characters.
Examples:
Input: S = "every moment is fresh beginning"
Output:
Word with minimum average ASCII values is "beginning".
Word with maximum average ASCII values is "every".
Explanation:
The average of ASCII values of characters of the word "every" = ((101+118+101+114+121)/5 =111)
The average of ASCII values of characters of the word "moment" =: (( 109+111+109+101+110+116)/6 = 109.33333)
The average of ASCII values of characters of the word "is" = ((105+115)/2 =)
The average of ASCII values of characters of the word "fresh" = ((102+114+101+115+104)/5 = 110)
The average of ASCII values of characters of the word "beginning" = ((98+101+103+105+110+110+105+110+103)/9 =105).
Therefore, the word with minimum of average of ASCII values is "beginning” and maximum of average ASCII values is "every".
Input: S = "sky is blue"
Output:
Word with minimum average ASCII values is "blue".
Word with maximum average ASCII values is "sky".
Approach 1: The idea is to use the split() function. Follow the steps below to solve the problem:
- Split all the words of the string separated by spaces using the split() function. Store it in a list, say lis[].
- Initialize four variables say maxi, mini, maxId, and minId, to store the maximum of average of ASCII values, the minimum average ASCII value, the index of the word with the maximum average ASCII value in the list lis, and the index of the word with the minimum average ASCII value in the list lis[] respectively.
- Define a function, say averageValue(), to find the average ASCII value of a string.
- Traverse the list lis[] and perform the following operations:
- For every ith word in the list lis[], and store it in a variable, say curr.
- If curr> maxi, then update maxi as maxi = curr and assign maxId = i.
- If curr< mini, then update mini as mini = curr and assign minId = i.
- After completing the above steps, print the words lis[minId] and lis[maxId] with minimum and maximum average of ASCII value of its characters.
Below is the implementation of the above approach:
Python3
def averageValue(s):
sumChar = 0
for i in range ( len (s)):
sumChar + = ord (s[i])
return sumChar / / len (s)
def printMinMax(string):
lis = list (string.split( " " ))
maxId = 0
minId = 0
maxi = - 1
mini = 1e9
for i in range ( len (lis)):
curr = averageValue(lis[i])
if (curr > maxi):
maxi = curr
maxId = i
if (curr < mini):
mini = curr
minId = i
print ( "Minimum average ascii word = " , lis[minId])
print ( "Maximum average ascii word = " , lis[maxId])
S = "every moment is fresh beginning"
printMinMax(S)
|
Output
Minimum average ascii word = beginning
Maximum average ascii word = every
Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time
Auxiliary Space: O(1), as we are not using any extra space.
Approach 2: Using ord(),statistics.mean(),sort(),extend(),index() methods
- Split the given string
- Defined a method avg to find the average of ascii values of each string using ord(),statistics.mean() and for loop
- Append the average of ASCII values to a list
- Sort the list and display the minimum average ascii word and maximum average ascii word using sort(),extend() and index() methods
Python3
import statistics
S = "every moment is fresh beginning"
def avg(a):
v = []
for i in a:
v.append( ord (i))
return statistics.mean(v)
x = S.split()
y = []
for i in x:
y.append(avg(i))
z = []
z.extend(y)
z.sort()
print ( "Minimum average ascii word = " , x[y.index(z[ 0 ])])
print ( "Maximum average ascii word = " , x[y.index(z[ - 1 ])])
|
Output
Minimum average ascii word = beginning
Maximum average ascii word = every
Time Complexity: O(N) N – length of splitted list
Auxiliary Space: O(N) N – length of sorted list
Approach#3:using split and dictionary
Algorithm
1.Split the sentence into words.
2.Convert each word into a list of ASCII values of its characters.
3.Compute the average ASCII value of each word and store it along with the word in a dictionary.
4.Find the word with the minimum and maximum average ASCII values in the dictionary.
5.Print the words with the minimum and maximum average ASCII values.
Python3
s = "sky is blue"
words = s.split()
avg_ascii_dict = {}
for word in words:
ascii_list = [ ord (c) for c in word]
avg_ascii = sum (ascii_list) / len (ascii_list)
avg_ascii_dict[word] = avg_ascii
min_word = min (avg_ascii_dict, key = avg_ascii_dict.get)
max_word = max (avg_ascii_dict, key = avg_ascii_dict.get)
print (f "Word with minimum average ASCII values is '{min_word}'." )
print (f "Word with maximum average ASCII values is '{max_word}'." )
|
Output
Word with minimum average ASCII values is 'blue'.
Word with maximum average ASCII values is 'sky'.
Time complexity: O(n * m), where n is the number of words in the sentence and m is the maximum length of a word.
Space complexity: O(n * m), as we are storing the average ASCII values of each word in a dictionary that can potentially have n entries of length m.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...