Question

In this exercise we will practice using loops to handle collections/containers. Your job is to write...

In this exercise we will practice using loops to handle collections/containers. Your job is to write a program that asks the user to enter a sentence, then it counts and displays the occurrence of each letter.

Note: your program should count the letters without regard to case. For example, A and a are counted as the same.

Here is a sample run:

Enter a sentence: It's a very nice day today!
a: 3 times
c: 1 times
d: 2 times
e: 2 times
i: 2 times
n: 1 times
o: 1 times
r: 1 times
s: 1 times
t: 2 times
v: 1 times
y: 3 times

Notes:

  • The purpose of this problem is to practice using loops and collections/containers/strings.
  • Please make sure to submit a well-written program. Good identifier names, useful comments, and spacing will be some of the criteria that will be used when grading this assignment.
  • This assignment can be and must be solved using only the materials that have been discussed in class: loops, the index system, strings, lists and/or dictionaries. Do not look for or use alternative methods that have not been covered as part of this course.

How your program will be graded:

  • correctness: the program performs calculations correctly: 40%
  • complies with requirements (it properly uses loops, and containers/strings): 40%
  • code style: good variable names, comments, proper indentation and spacing : 20%

Language is python 3

Homework Answers

Answer #1
# first we create a empty dictionary which will store key value pairs for different alphabets
Dict = {}

# ask for input from the user
sentence = input("Enter a sentence: ")
print() #print a blank line

# Fill the Dictionary so that it contains keys from 'a' to 'z' but values of all the keys are set to 0
# we iterate from 97 to 122 because 97 to 122 corresponds to ASCII value of 'a' to 'z'
for loop in range(97, 123):
    key = chr(loop) # chr(loop+97) converts integer to character so that 97 converts to 'a', 98 converts to 'b' and so on
    Dict[key] = 0 #set value of each key = 0

#then we iterate through each character in the input string
# if character is space or is not any alphabet then we skip or in other words we fill the dictionary only for alphabets from 'a' to 'z' or 'A' to 'Z' and not blank spaces and other characters
for character in sentence:
    
    # if character is not a blank space and it lies between 'A' and 'Z' ot 'a' and 'z' then we enter the if condition
    if character != ' ' and ((character >= 'A' and character <= 'Z') or (character >= 'a' and character <= 'z')):
        
        # if character is uppercase alphabet then it needs to be converted into lower case by adding 32 to it
        # we add 32 because ASCII value of 'A' is 65 and that of 'a' is 97 so we add 32
        if character >= 'A' and character <= 'Z':
            tempChar = ord(character) # convert character to integer using ord(character)
            tempChar = tempChar + 32 # add 32 to integer
            character = chr(tempChar) # convert integer to character
            
        Dict[character] = Dict[character] + 1 #finally add the character to dictionary by incrementing its value

# then we iterate from 97 to 122 and again convert it back to character.
# if the value of this character is zero in the dictionary then we don't print it else we print character and its value
for loop in range(97, 123):
    key = chr(loop)
    if Dict[key] != 0:
        print(key, " = ", Dict[key])
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
How much money do you think you would earn in a period of 30 days if...
How much money do you think you would earn in a period of 30 days if you were paid as follows: one cent for the first day, two cents for the second day, four cents for the third day, eight cents for the fourth day, and so on (i.e. your salary doubles each day)? Do you think you would make very little money, just a few dollars, at the end of the 30-day period? Let us write a program to...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Each person in the group will be responsible for rewriting the code to solve ​one​ of...
Each person in the group will be responsible for rewriting the code to solve ​one​ of the original parts (celebrity names), ​without using arrays or loops.​ The purpose of this assignment is to practice Strings manipulation and String methods so full credit will only be given if you are utilizing the code for Strings (substring, charAt, compareTo, equals, etc). program Java this was my last assigment Take the 4th and the 5th words from part 1 and print out both...
WRITE A JAVA PROGRAM 1. Assignment Description Write a stock transaction program that calculates a customer's...
WRITE A JAVA PROGRAM 1. Assignment Description Write a stock transaction program that calculates a customer's profit (or loss) within a certain period of time. 1) First, the program need to get user's name, the stock's code, number of shares and the price he/she purchased the stock, it also asks the user for the price he/she sold the stock later on. The program then compute the following: The amount of money the customer paid for the stock. The amount of...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):         self.name = name         self.pop = pop         self.area = area         self.continent = continent     def getName(self):         return self.name     def getPopulation(self):         return self.pop     def getArea(self):         return self.area     def getContinent(self):         return self.continent     def setPopulation(self, pop):         self.pop = pop     def setArea(self, area):         self.area = area     def setContinent(self, continent):         self.continent = continent     def __repr__(self):         return (f'{self.name} (pop:{self.pop}, size: {self.area}) in {self.continent} ') TASK 2 Python Program: File: catalogue.py from Country...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...