- 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 print words from a sentence with highest and lowest ASCII value of characters
ASCII (American Standard Code for Information Interchange), is a character encoding system that represents every character as a unique 7-bit binary code i.e. ASCII values are a numerical representation of characters. The ASCII values are 7-bit binary codes that range from 0 to 127. For Example, the ASCII code for a space character is 32, and for digit ‘1’ ASCII code is 49 and similarly ASCII codes are assigned to each character which is represented in an ASCII table.
In Python, the ASCII code of a character can be computed using a predefined function ord(), which takes a character as input and returns the ASCII code for that character.
For example, ord(‘A’) returns 65.
Problem Statement
Given a string S. Print the words that have the highest and lowest average of ASCII values of its characters.
Sample Example 1
Input
S = “today is a sunny day”
Output
Highest ASCII value = “sunny” Lowest ASCII value = “a”
Explanation
Average of ASCII values: today = (116 + 111 + 100 + 97 + 121) / 5 = 109 is = (105 + 115) / 2 = 110 a = 97 / 1 = 97 sunny = (115 + 117 + 110 + 110 + 121) / 5 = 114.6 day = (100 + 97 + 121) / 3 = 106 Thus, “sunny” has the highest average and “a” has the lowest average.
Sample Example 2
Input
S = “pink city”
Output
Highest ASCII value = “city” Lowest ASCII value = “pink”
Explanation
Explanation: Average of ASCII values: pink = (112 + 105 + 110 + 107) / 4 = 108.5 city = (99 + 105 + 116 + 121) / 4 = 110.25 Thus, “city” has the highest average and “pink” has the lowest average.
Approach 1: Brute Force Approach
The brute force solution for the problem is to split the input sentence into words and then calculate the average of ASCII values of each word to find the highest and lowest average of ASCII values of characters.
For converting the input string to a list of words we use the inbuilt split() function.
Pseudocode
procedure ascii_avg (word) sum = 0 For each character c in word sum = sum + ord(c) end for avg = sum / length(word) ans = avg end procedure procedure highLow (sentence) words = split sentence by whitespace high_word = words[0] low_word = words[0] high_avg = ascii_avg(words[0]) low_avg = ascii_avg(words[0]) for i = 1 to length(words) - 1 word = words[i] avg = ascii_avg(word) if avg > high_avg high_word = word high_avg = avg else if avg < low_avg low_word = word low_avg = avg end if end for print "Highest ASCII value:", high_word print "Lowest ASCII value:", low_word end procedure
Example: Python Implementation
In the following program, we used a split function to split sentences into words.
def ascii_avg(word): """ Returns the average ASCII value of a word. """ return sum(ord(c) for c in word) / len(word) def highLow (sentence): """ Prints the words with the highest and lowest average ASCII values in a sentence. """ words = sentence.split() high_word, low_word = words[0], words[0] high_avg, low_avg = ascii_avg(words[0]), ascii_avg(words[0]) for word in words[1:]: avg = ascii_avg(word) if avg > high_avg: high_word, high_avg = word, avg elif avg < low_avg: low_word, low_avg = word , avg print("Highest ASCII value:", high_word) print("Lowest ASCII value:", low_word) highLow("today is a sunny day")
Output
Highest ASCII value: sunny Lowest ASCII value: a
Approach 2: Using a Heap
Another approach to the problem can be to keep a heap to maintain the highest and lowest ASCII values so far. Additionally, we maintain a dictionary to map words to their ASCII values and extract the highest and lowest using a heap.
Pseudocode
procedure highLow(sentence) words = split sentence by whitespace word_avg = {} for each word in words avg = 0 for each character c in word avg = avg + ord(c) end for avg = avg / length(word) word_avg[word] = avg end for high_heap = [] low_heap = [] for word, avg IN word_avg.items() heapq.heappush(high_heap, (-avg, word)) heapq.heappush(low_heap, (avg, word)) end for high_word = heapq.heappop(high_heap)[1] low_word = heapq.heappop(low_heap)[1] print "Highest ASCII value:", high_word print "Lowest ASCII value:", low_word end procedure
Example: Python Implementation
In the following program, we use a heap to keep track of the highest and lowest ASCII values and dictionary to map values.
import heapq def highLow(sentence): """ Prints the words with the highest and lowest average ASCII values in a sentence. """ words = sentence.split() word_avg = {word: sum(ord(c) for c in word) / len(word) for word in words} high_heap = [(-avg, word) for word, avg in word_avg.items()] low_heap = [(avg, word) for word, avg in word_avg.items()] heapq.heapify(high_heap) heapq.heapify(low_heap) high_word = heapq.heappop(high_heap)[1] low_word = heapq.heappop(low_heap)[1] print("Highest ASCII value:", high_word) print("Lowest ASCII value:", low_word) highLow("today is a sunny day")
Output
Highest ASCII value: sunny Lowest ASCII value: a
Approach 3: Using Inbuilt Functions
Using inbuilt functions like ord() that returns the ASCII value of a character, max() and min() to find the maximum and minimum values, the problem can be solved.
Pseudocode
procedure highLow(sentence) words = split sentence by whitespace high_word = max(words, key=lambda w: sum(ord(c) for c in w) / len(w)) low_word = min(words, key=lambda w: sum(ord(c) for c in w) / len(w)) print "Highest ASCII value:", high_word print "Lowest ASCII value:", low_word end procedure
Example: Python Implementation
In the following program, we use the inbuilt Python function to find the words with the highest and lowest ASCII values.
def highLow(sentence): """ Prints the words with the highest and lowest average ASCII values in a sentence. """ words = sentence.split() # min() and max() are built-in functions high_word = max(words, key=lambda w: sum(ord(c) for c in w) / len(w)) low_word = min(words, key=lambda w: sum(ord(c) for c in w) / len(w)) print("Highest ASCII value:", high_word) print("Lowest ASCII value:", low_word) highLow("today is a sunny day")
Output
Highest ASCII value: sunny Lowest ASCII value: a
Time Complexity − O(nlogn)
Space Complexity − O(n)
Approach 4: Sorting the Words by Their Average ASCII Values
By sorting the words according to their average ASCII words, we can find the highest value from the last element and the lowest value from the first element.
Pseudocode
procedure highLow (sentence) words = split sentence by whitespace words_sorted = sort words by key=lambda w: sum(ord(c) for c in w) / len(w) print "Highest ASCII value:", last word in words_sorted print "Lowest ASCII value:", first word in words_sorted end procedure
Example: Python Implementation
In the following program, we sort the words based on their average ASCII values.
def highLow(sentence): """ Prints the words with the highest and lowest average ASCII values in a sentence. """ words = sentence.split() # Sorts the words in ascending order words_sorted = sorted(words, key=lambda w: sum(ord(c) for c in w) / len(w)) print("Highest ASCII value:", words_sorted[-1]) print("Lowest ASCII value:", words_sorted[0]) highLow("today is a sunny day")
Output
Highest ASCII value: sunny Lowest ASCII value: a
Time Complexity − O(nlogn)
Space Complexity − O(n)
Conclusion
In conclusion, to find the words with the highest and lowest average ASCII values, we can use any of the above approaches some of which are easy to understand but have a high time complexity of O(n^2) but using built-in functions it can be reduced to O(nlogn).