Question

Problem 1 This program is about dictionaries. We want to use a dictionary to store frequency...

Problem 1

This program is about dictionaries. We want to use a dictionary to store frequency count of each letter in a string. Write a Python program to do the following:

(a) Ask the user to enter a string. Convert all letters to uppercase. Count the frequency of each letter in the string. Store the frequency counts in a dictionary. You should count letters only. Do not count any other characters such as digits and space. Display the dictionary.

(b) Ask the user to enter a letter. Convert it to uppercase. Check whether the letter is in the dictionary. If it is not, display the message “Letter not in dictionary”. Otherwise, display the frequency count of that letter, remove the letter from the dictionary and display the dictionary after that letter has been removed.

(c) Create a list to store the letters that are in the dictionary. Sort and display the list.

The following is an example.

Enter a string: Magee, Mississippi

Dictionary: {'M': 2, 'A': 1, 'G': 1, 'E': 2, 'I': 4, 'S': 4, 'P': 2}

Choose a letter: s

Frequency count of that letter: 4

Dictionary after that letter removed: {'M': 2, 'A': 1, 'G': 1, 'E': 2, 'I': 4, 'P': 2}

Letters sorted: ['A', 'E', 'G', 'I', 'M', 'P']

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

# asking user for string
user_string = input("Enter a string: ")

# converting to uppercase
user_string = user_string.upper()

# creating empty dictionary
freq = {}

# iterate through each character in string
for i in user_string:
    # checking if selected character is alphabet or not
    if i.isalpha():
        # checking if it is in dictionary
        if i in freq:
            # incrementing the count by 1
            freq[i] += 1
        else:
            # creating a new entry in dictionary with initial count as 1
            freq[i] = 1

# printing dictionary
print("Dictionary: ", freq)

# asking user to enter a letter
letter = input("Choose a letter: ")

# converting the letter to upper case
letter = letter.upper()

# checking if letter is in dictionary
if letter in freq:
    # printing count of that letter
    print("Frequency count of that letter: ", freq[letter])
    
    # removing the letter from dictionary
    freq.pop(letter)
    
    # printing dictionary after removing the letter
    print("Dictionary after that letter removed: ", freq)
else:
    print("Letter not in dictionary")

# creating a list with all the dictionary keys
letter_list = list(freq.keys())

# sorting the list
letter_list.sort()

# printing sorted list
print(letter_list)

Output:

Enter a string: Magee, Mississippi
Dictionary: {'M': 2, 'A': 1, 'G': 1, 'E': 2, 'I': 4, 'S': 4, 'P': 2}
Choose a letter: s
Frequency count of that letter: 4
Dictionary after that letter removed: {'M': 2, 'A': 1, 'G': 1, 'E': 2, 'I': 4, 'P': 2}
['A', 'E', 'G', 'I', 'M', 'P']

Screenshots:

Add a comment
Know the answer?
Add Answer to:
Problem 1 This program is about dictionaries. We want to use a dictionary to store frequency...
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
  • For this problem, you will write a program to form a frequency table of the letters...

    For this problem, you will write a program to form a frequency table of the letters in a text file, that is, how many times each letter appears in the file In Python 3.7. Such a frequency table might be useful for compressing a text file. Because different letters appear with different frequencies, we can compress a file by using shorter codes for common letters and longer codes for letters that appear less frequently. Dictionaries provide an elegant way to...

  • 2. Searching a String: Write a MIPS assembly language program to do the following: Read a...

    2. Searching a String: Write a MIPS assembly language program to do the following: Read a string and store it in memory. Limit the string length to 100 characters. Then, ask the user to enter a character. Search and count the number of occurrences of the character in the string. The search is not case sensitive. Lowercase and uppercase letters should be equal. Then ask the user to enter a string of two characters. Search and count the number of...

  • Write a Java Program that performs the following functionalities: and you can use if statements, cases,...

    Write a Java Program that performs the following functionalities: and you can use if statements, cases, and string methods Enter a new main sentence Find a String Find all incidents of a String Find and Replace a String Replace all the incidents of a String Count the number of words Count a letter’s occurrences Count the total number of letters Delete all the occurrences of a word Exit The program will display a menu with each option above, and then...

  • Write a program in C to make dictionary to add, delete and search words Using linked lists. MAKE THE SEARCHING IN THE pr...

    Write a program in C to make dictionary to add, delete and search words Using linked lists. MAKE THE SEARCHING IN THE program like the searching in the online dictionary(if we enter the 1 letter then it will show all the letters starting with the same number and if we enter 2 letters then it will show all the numbers starting with same two letters and so on up to the complete word.) make the following functions 1. insert 2....

  • Exercise #4: Some Operations on Strings Write a program that uses a C-string to store a...

    Exercise #4: Some Operations on Strings Write a program that uses a C-string to store a string entered from the keyboard (in lower case letters). The program will then provide 6 options about operations performed on the input string in the form of a menu, as shown in the following sample Input/Output. The 6 operations are given below: 1. print the string 2 reverse the string 3. print the length of the string 4. convert the lower letters to upper...

  • C++ Linux Compiler Letter Frequency Write a function that will take a string and return a...

    C++ Linux Compiler Letter Frequency Write a function that will take a string and return a count of each letter in the string. For example, "my dog ate my homework" contains 3 m's, 3 o's, 2 e's, 2 y's and one each of d, g, a, t, h, w, r and k. Your function should take a single string argument and return a dynamically allocated array of 26 integers representing the count of each of the letters a .. z...

  • In C Program Linux Write a program to detect the frequency of a character in a...

    In C Program Linux Write a program to detect the frequency of a character in a word. The program should ask the user for a letter followed by a space, then the word to calculate frequency. For example the user enters “g programming”. The output should be: “the letter g, occurs 2/11 times in the word”. Don’t worry about upper case letters.

  • in c++ sample output Description A dictionary is a collection of words that contain at least...

    in c++ sample output Description A dictionary is a collection of words that contain at least one definition. One way to represent a dictionary is through the use of an array, actually two arrays. For a dictionary to print the term with matching definition, each array: Must contain the same number of elements as the other array Each element in array 1 (term) must contain a match in array 2 (definition). 1. 2. Take the following array examples: Array 1=...

  • write a C program to make the dictionary USING LINKED LISTS and STORE DATA in File...

    write a C program to make the dictionary USING LINKED LISTS and STORE DATA in File HANDLING..... you have to create functions 1. insert 2. delete 3. search(if we search with any letter then the words with same first letter have been shown) 4. display 6. exit Appearance should be like proper dictionary.

  • Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that...

    Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...

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