In: Computer Science
Write a program that counts the letters in a given string and then display the letter count in order from high to low.
The program should:
G - 2
O - 2
E - 1
L - 1
PYTHON CODE:
# your name here
# displaying the description of the program
print('***** Program to count the letters in a given string.
*****')
print('***** Then display the letter count in order from high to
low. *****\n')
# getting input from the user
inp_str=input('Enter a string: ')
# dictionary to store the character count
char_count={}
# iterating through every character in the string
for c in inp_str:
# changing the character to uppercase
c=c.upper()
# checking the character is in dictionary or
not
if c not in char_count:
char_count[c]=1
else:
# increment the
character count by 1
char_count[c]+=1
# getting list of key,value pairs from the dictionary
lst=list(char_count.items())
# sorting the list by values, if a few letters have the same
count,
# sort the display in an alphabetic order
lst.sort(key=lambda x:(-x[1],x[0]))
# displaying the character and count
for k,v in lst:
print('{0} - {1}'.format(k,v))
SCREENSHOT FOR CODING:
SCREENSHOT FOR OUTPUT: