Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 7.1 (lettercount.py, design). Before attempting to program lettercount.

ID: 3698471 • Letter: E

Question

Exercise 7.1 (lettercount.py, design). Before attempting to program lettercount.py, create a file called design that includes some analysis on how you think the problem will be solved. Examples include: a flowchart of events in the program, pseudocode, or a step by step process written in plain english. If you choose to scan your design please make sure that it is legible. Next write a program that reads in a string on the command line and returns a table of the letters of the alphabet in alphabetical order which occur in the string together with the number of times each letter occurs. Case should be ignored. A sample run of the program would look this: Enter some letters >>> The cat in the hat 2 a 2 c1 4e 2 s h 3 611 7 n 1 st4 This should involve writing a function called count.letters that takes in a string and returns a dictionary with these letters and counts.

Explanation / Answer

Design and explanation of the procedure:
Following is a step by step process for solving the problem:
1. Take some input string from the user.
2. Convert the characters in the string provided by the user to lowercase.
3. Define an empty dictionary having character as key and its frquency as its value
4. For the next character in the given string do the following
5. if the character is an alphabet, go to step 6, otherwise go to step 4.
6. if the character is present in the dictionary, increase its count
7. Else if the character is not present, insert it and make its count as 1.
8. Go to step 4 for the next character in the string. If there are no more characters, go to next step.
9. For each key in the dictionary, sorted accourding to the keys,
10. print the key and the value.

Code:

def lettercount():
print("Enter some letters >>>")
str1=raw_input() #taking input
str1=str1.lower() #converting to lower case
dict = {}
for n in str1:
if n.isalpha(): #if n is an alphabet
keys = dict.keys()
if n in keys: #if n present in dictionary increase its count
dict[n] += 1
else:
dict[n] = 1
for key in sorted(dict.iterkeys()):
print "%s: %s" % (key, dict[key])

  
lettercount()

Sample running of the program:
$python main.py
Enter some letters >>>The cat in the hat
a: 2
c: 1
e: 2
h: 3
i: 1
n: 1
t: 4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote