Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could I get this back quick, Please? Use a dictionary to count the frequency of numbers in the given text string. Only numbers should be

Could I get this back quick, Please?

Use a dictionary to count the frequency of numbers in the given text string. Only numbers should be counted. Do not count blank spaces, letters, or punctuation. Complete the function so that input like "1001000111101" will return a dictionary that holds the count of each number that occurs in the string {'1': 7, '0': 6}. This function should:

accept a string text variable through the functions parameters;

initialize an new dictionary;

iterate over each text character to check if the character is a number

count the frequency of numbers in the input string, ignoring all other characters;

populate the new dictionary with the numbers as keys, ensuring each key is unique, and assign the value for each key with the count of that number;

return the new dictionary.

def count_numbers(text):

# Initialize a new dictionary.

dictionary = ___

# Complete the for loop to iterate through each "text" character.

for ___

# Complete the if-statement using a string method to check if the

# character is a number.

if___:

# Complete the if-statement using a logical operator to check if

# the number is not already in the dictionary.

if ___:

# Use a dictionary operation to add the number as a key

# and set the initial count value to zero.

___

# Use a dictionary operation to increment the number count value

# for the existing key.

___

return dictionary

print(count_numbers("1001000111101"))

# Should be {'1': 7, '0': 6}

print(count_numbers("Math is fun! 2+2=4"))

# Should be {'2': 2, '4': 1}

print(count_numbers("This is a sentence."))

# Should be {}

print(count_numbers("55 North Center Drive"))

# Should be {'5': 2}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

More Books

Students also viewed these Databases questions

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago