Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3835398 • Letter: #

Question

# 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()

Explanation / Answer

Modified code.

# 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:
for value in data:
if value in dictionary:
dictionary[value] += 1
else:
dictionary[value] = 1
# Part 2:
# Find the maximum of the dictionary values:
max_value = 2
for key in dictionary:
if dictionary[key] > max_value:
max_value = dictionary[key]
# Part 3:
# Create a list of the keys that have the maximum value:
modes = []
for key in dictionary:
if dictionary[key] == max_value:
modes.append(key)
# Part 4:
# If no item occurs more than the others, then there is no mode:
if not modes:
modes = 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()

# code link: https://paste.ee/p/02GSO