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.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
Exercise 7.1 (lettercount.py, design). Before attempting to program lettercount.py, create a file called design that includes...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Write a Python program, in a file called concat_strings.py, that includes the following functions: orderedConcat,...

    1. Write a Python program, in a file called concat_strings.py, that includes the following functions: orderedConcat, a recursive function that takes two alphabetically-ordered strings and merges them, leaving the letters in alphabetical order. Note that the strings may be different lengths. a main method that inputs two ordered strings and calls the orderedConcat method to return a resulting merged, ordered string. Your main method should print the two input strings and the result. Note: You may not use any of...

  • Need help writing four short recursive programs: Write a program called Permutations.java that takes an integer...

    Need help writing four short recursive programs: Write a program called Permutations.java that takes an integer command-line argument, n, and enumerates all permutations of the first n letters in the English alphabet. Be sure to handle exceptions of the types: i) bad user input (i.e., double, string), ii) n > 26 or n < 0. Write a program called PermutationsK.java that takes two integers, n and k, as command-line arguments and prints out all P(n,k) permutations that contain exactly k...

  • Create a new program in Mu and save it as ps4.5.2.py and take the code below...

    Create a new program in Mu and save it as ps4.5.2.py and take the code below and fix it as indicated in the comments: # Do not change the line of code below. It's at the top of # the file to ensure that it runs before any of your code. # You will be able to access french_dict from inside your # function. french_dict = {"me": "moi", "hello": "bonjour", "goodbye": "au revoir", "cat": "chat", "dog": "chien", "and": "et"} #...

  • JAVA PROJECT Part 1 - Normalize Text The first thing we will do is normalize the...

    JAVA PROJECT Part 1 - Normalize Text The first thing we will do is normalize the input message so that it’s easier to work with. Write a method called normalizeText which does the following: Removes all the spaces from your text Remove any punctuation (. , : ; ’ ” ! ? ( ) ) Turn all lower-case letters into upper-case letters Return the result. The call normalizeText(“This is some \“really\” great. (Text)!?”) should return “THISISSOMEREALLYGREATTEXT” Part 2 - Obfuscation...

  • 1. Write a C++ program called Password that handles encrypting a password. 2. The program must...

    1. Write a C++ program called Password that handles encrypting a password. 2. The program must perform encryption as follows: a. main method i. Ask the user for a password. ii. Sends the password to a boolean function called isValidPassword to check validity. 1. Returns true if password is at least 8 characters long 2. Returns false if it is not at least 8 characters long iii. If isValidPassword functions returns false 1. Print the following error message “The password...

  • The skeleton code (starter file) for the problem is pasted below: def phoneNumber(letters): result = ""...

    The skeleton code (starter file) for the problem is pasted below: def phoneNumber(letters): result = "" # ADD YOUR CODE HERE return result def loadEvents(filename): events = {} # ADD YOUR CODE HERE return events def timeline(events): # ADD YOUR CODE HERE return # DO NOT modify or remove the code below! We will use it for testing. if __name__ == "__main__": # Problem 1: Decoding Telephone Numbers ph = input("Enter the text to translate: ") print("The corresponding phone number...

  • CIST 1305 – Program Design and Development Chapter 4 Assignment #7 [Decisions/If Statements & Loops] (50...

    CIST 1305 – Program Design and Development Chapter 4 Assignment #7 [Decisions/If Statements & Loops] (50 Points) Please do the following exercises: Pastoral College is a small college in the Midwest. Design the pseudo-code for a program that accepts a student name, major field of study, and grade point average. Display a student’s data with the message “Dean’s list” if the student’s grade point average is above 3.5, “Academic probation” if the grade point average is below 2.0, and no...

  • Create a Python script file called hw12.py. Add your name at the top as a comment,...

    Create a Python script file called hw12.py. Add your name at the top as a comment, along with the class name and date. Ex. 1. a. Texting Shortcuts When people are texting, they use shortcuts for faster typing. Consider the following list of shortcuts: For example, the sentence "see you before class" can be written as "c u b4 class". To encode a text using these shortcuts, we need to perform a replace of the text on the left with...

  • Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same...

    Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same way it is used in the GeneralizedSelectionSort. You should use the attached ComparableDemo to test your program. public class ComparableDemo { public static void main(String[] args) { Double[] d = new Double[10]; for (int i = 0; i < d.length; i++) d[i] = new Double(d.length - i); System.out.println("Before sorting:"); int i; for (i = 0; i < d.length; i++) System.out.print(d[i].doubleValue( ) + ", ");...

  • Given below is a partially completed C program, which you will use as a start to...

    Given below is a partially completed C program, which you will use as a start to complete the given tasks. #define SIZE 9 #include <stdio.h> int compareAndCount (int[], int[]); double averageDifference (int[], int[]); void main() { } int compareAndCount(int arr1[SIZE], int arr2[SIZE]) { int count - @; for (int i = 0; i < SIZE; i++) { if (arri[i] > arr2[i]) count++; return count; Task 1: (10 pts) Show the design of the function compareAndCount() by writing a pseudocode. To...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT