Word Histogram
Histogram is a graphical representation drawn based on the
frequency of occurrence of things. Histogram for a word is drawn based on the
number of occurrences of each character in the word. An inverted dictionary is
used to maintain the list of characters that has same frequency. Write an
algorithm and the subsequent Python code to compute and print the frequency of
occurrence of character as a dictionary (ch:count). Make the entire process to
be case insensitive. For example, if the input word is ‘parrot’, then the
dictionary with characters and frequency of occurrence is
{'a': 1,
'o': 1, 'p': 1, 'r': 2, 't': 1}
and the
inverted dictionary is
{1: ['a',
'o', 'p', 't'], 2: ['r']}
Print
dictionary in sorted order by key. Sort the values of each key in the inverted
dictionary.
[Hint:
use pprint function for printing dictionary in sorted order.
Syntax
for pprint is
pprint(dictionary
name)
Include
the line “from pprint import pprint” in the top of your program
Check for
boundary conditions and print 'Invalid input' if conditions are not met.
Input
Format:
First
line contains the input word
Output
Format:
Dictionary
of characters in the word as keys and count as values in sorted order by key.
Inverted
dictionary in sorted order by key.
Boundary
Conditions:
Given
word should only contain alphabets
Input:
the
word
Processing:
from
pprint import pprint
flag
= True
letter
= {}
inv_let
= {}
temp_list
= []
if
word.isalpha() == False:
flag = False
else:
for i in word:
letter.setdefault(i,0)
letter[i] += 1
for v in set(letter.values()):
for k in letter.keys():
if letter[k] == v:
temp_list.append(k)
temp_list.sort()
inv_let[v] = temp_list
temp_list = []
Output:
print
the inverted sorted dictionary if the input is valid else print invalid input
Algorithm:
Step1.
import the pprint function from the pprint module
Step2.
Get the word from the user
Step3.
Check if the word contains only alphabets if yes then proceed to step4 else
assign flag as False.
Step4.
Assign flag as True and initialize letter,inv_let as dictionaries and temp_list
as an empty list.
Step5.
Let i iterate through the word
Step5.1.
Assign i as the key and 0 as the default value and add these to the letter
dictionary
Step5.2
Increment the value by 1.
Step6.
Let v iterate through the set of values in the letter dictionary
Step6.1
let k iterate through the keys in the letter dictionary
Step6.1.1
if the value obtained by accessing the key k in letter dictionary is v then
append k in the temp_list
Step6.2
let v be the key of inv_let dictionary and temp_list be the value
Step6.3
reinitialize temp_list as an empty list
Step7.
If flag is equal to True display inv_let in the sorted order else display
invalid input.
Step8.
End
Program:
from pprint import pprint
word = input()
word = word.lower()
if word.isalpha() == False:
flag = False
else:
flag = True
letter = {}
inv_let = {}
temp_list = []
for i in word:
letter.setdefault(i,0)
letter[i] += 1
for v in set(letter.values()):
for k in letter.keys():
if letter[k] == v:
temp_list.append(k)
temp_list.sort()
inv_let[v] = temp_list
temp_list = []
if flag == True:
pprint(letter)
pprint(inv_let)
else:
print('Invalid input')
from pprint import pprint
word = input()
word = word.lower()
if word.isalpha() == False:
flag = False
else:
flag = True
letter = {}
inv_let = {}
temp_list = []
for i in word:
letter.setdefault(i,0)
letter[i] += 1
for v in set(letter.values()):
for k in letter.keys():
if letter[k] == v:
temp_list.append(k)
temp_list.sort()
inv_let[v] = temp_list
temp_list = []
if flag == True:
pprint(letter)
pprint(inv_let)
else:
print('Invalid input')
No comments:
Post a Comment