Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# This program uses a Python dictionary to find the mode(s) of a data set. # The mode of a data set is its most

# This program uses a Python dictionary to find the mode(s) of a data set.

# The mode of a data set is its most frequently occurring value. # A data set may have more than one mode. # Examples: # mode of [1,2,3,4,5,6,7] is none # mode of [1,2,3,4,5,6,7,7] is 7 # modes of [1,2,2,2,3,3,4,5,6,7,7,7] are 2 and 7

# Replace any "" comments with your own code statement(s) # to accomplish the specified task. # Do not change any other code.

# This function returns a list containing the mode or modes of the data set. # Input: # data - a list of data values. # Output: # returns a list with the value or values that are the mode of data. # If there is no mode, the returned list is empty. def mode(data): dictionary = {}

# Part 1: # Update dictionary so that each dictionary key is a value in data and # each dictionary value is the correspinding number of times that value occurs: #

# Part 2: # Find the maximum of the dictionary values: #

# Part 3: # Create a list of the keys that have the maximum value: modes = [] #

# Part 4: # If no item occurs more than the others, then there is no mode: #

return modes

data1 = [1,2,3,4,5,6,7] print(data1) print("mode:", mode(data1)) print()

data2 = [1,2,3,4,5,6,7,7] print(data2) print("mode:", mode(data2)) print()

data3 = [1,2,2,2,3,3,4,5,6,7,7,7] print(data3) print("mode:", mode(data3)) print()

data4 = ["blue", "red", "green", "blue", "orange", "yellow", "green"] print(data4) print("mode:", mode(data4)) print()

_______________________________________________________________________________________________________________________________

Programming Activity 7 - Guidance ================================= Note that the mode() function begins by creating an empty dictionary. You must use this dictionary in the following parts. Part 1 ------ In this part you will add entries to the dictionary. Use a "for" loop to iterate through the values in the data list. For each value, use it as a dictionary key to see if it is already in the dictionary. If it is already in the dictionary, add one to that dictionary entries value. Each dictionary value contains the number of times that value occurs in the data. You reference the current value for a key via dictionary[key]. If it is not in the dictionary, add it by assigning an entry for it with a value of 1. Part 2 ------ Python has a built-in max() function that finds the maximum in any iterable object. Use max() on the list of dictionary values to obtain the maximum number of times a value occurs. Assign this to a variable called maxTimes. You will make use of maxTimes in part 3. Part 3 ------ Note that this part begins by creating an empty modes list. Use a "for" loop to loop through the dictionary keys. The default "for" iterator for a Python dictionary iterates through its keys. For each key, see if its associated dictionary value is equal to maxTimes. If it is equal, append that key to the modes list. Part 4 ------ If no item in the data set is repeated, then your modes list at this point will be the same as your starting data list. However, this case actually should mean there is no mode. Actually, every item is a mode with a frequency of 1. But, we want to return an empty modes list for this case. If the modes list and the data list have the same length, reset modes to an empty list. Note that modes is already being returned at the end of the function.

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 Design And Relational Theory Normal Forms And All That Jazz

Authors: Chris Date

1st Edition

1449328016, 978-1449328016

More Books

Students also viewed these Databases questions

Question

It would have become a big deal.

Answered: 1 week ago

Question

1. What are the major sources of stress in your life?

Answered: 1 week ago