In: Computer Science
Programming Activity 7 - Guidance =================================
This assignment uses a built-in Python dictionary. It does not use a dictionary implementation from the textbook collections framework. It does not require any imports/files from the textbook collections framework. This week's "examplePythonDictionary.py" example uses a built-in Python dictionary. Note that the mode() function begins by creating an empty Python 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.
=============================================================================================================================
useDictionary.py
# 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 "<your code>" 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:
# <your code>
# Part 2:
# Find the maximum of the dictionary values:
# <your code>
# Part 3:
# Create a list of the keys that have the maximum value:
modes = []
# <your code>
# Part 4:
# If no item occurs more than the others, then there is no
mode:
# <your code>
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()
If you have any doubts, please give me comment...
# 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 "<your code>" 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:
# <your code>
for v in data:
if v not in dictionary:
dictionary[v] = 0
dictionary[v] += 1
# Part 2:
# Find the maximum of the dictionary values:
# <your code>
maximum = max(dictionary.values())
# Part 3:
# Create a list of the keys that have the maximum value:
modes = []
# <your code>
for k in dictionary:
if maximum==dictionary[k]:
modes.append(k)
# Part 4:
# If no item occurs more than the others, then there is no mode:
# <your code>
if modes==data:
return "none"
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()