Question

In: Computer Science

Programming Activity 7 - Guidance ================================= This assignment uses a built-in Python dictionary. It does not...

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

Solutions

Expert Solution

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


Related Solutions

[Python programming] Functions, lists, dictionary, classes CANNOT BE USED!!! This assignment will give you more experience...
[Python programming] Functions, lists, dictionary, classes CANNOT BE USED!!! This assignment will give you more experience on the use of: 1. integers (int) 2. floats (float) 3. conditionals(if statements) 4. iteration(loops) The goal of this project is to make a fictitious comparison of the federal income. You will ask the user to input their taxable income. Use the income brackets given below to calculate the new and old income tax. For the sake of simplicity of the project we will...
Python Programming Revise the ChatBot program below. There needs to be one list and one dictionary...
Python Programming Revise the ChatBot program below. There needs to be one list and one dictionary for the ChatBot to use. Include a read/write function to the program so that the program can learn at least one thing and store the information in a text document. ChatBot program for revising: # Meet the chatbot Eve print('Hi there! Welcome to the ChatBot station. I am going to ask you a series of questions and all you have to do is answer!')...
1. INTRODUCTION The goal of this programming assignment is for students to write a Python program...
1. INTRODUCTION The goal of this programming assignment is for students to write a Python program that uses repetition (i.e. “loops”) and decision structures to solve a problem. 2. PROBLEM DEFINITION  Write a Python program that performs simple math operations. It will present the user with a menu and prompt the user for an option to be selected, such as: (1) addition (2) subtraction (3) multiplication (4) division (5) quit Please select an option (1 – 5) from the...
Programming Activity 1(Python) Utilise a count-based iteration structure that will accepts the data listed below and...
Programming Activity 1(Python) Utilise a count-based iteration structure that will accepts the data listed below and produce the total purchase amount. Your final report should be similar to the one show below. Input Data: Item Description Item Price Salomon Fish $ 26.97 Ribeye Steak $ 12.98 Sweet Corn $ 4.96 Asparagus $5.92 Output: Item Description Item Price ================================= Salomon Fish $26.97 Ribeye Steak $ 12.98 Sweet Corn $ 4.96 Asparagus $ 5.92 Your total purchase: $ xx.xx
Programming assignment 9 Write a function sortWords(array) that does the following: 1. Takes as an input...
Programming assignment 9 Write a function sortWords(array) that does the following: 1. Takes as an input a cell array of strings consisting of letters only. (1 pts) 2. Returns a cell array with all the strings in alphabetical order. (5 pts) 3. The function should ignore whether letters are lower case or upper case. (2 pts) Test your function with the following: (2 pts) >> sorted=sortWords({’Hello’,’hell’,’abc’,’aa’,’aza’,’aab’,’AaBb’,’a’}) sorted = ’a’ ’aa’ ’aab’ ’AaBb’ ’abc’ ’aza’ ’hell’ ’Hello’ Note: Your function may...
Write 5 questions about Object Oriented Programming in Python. Each question should have 7 options. Please...
Write 5 questions about Object Oriented Programming in Python. Each question should have 7 options. Please provide 7 answer options for EACH question and the select answer for EACH question
ITP 100 Programming Assignment 1 This program uses the Little-Crab-3 scenario complete the following steps. Start...
ITP 100 Programming Assignment 1 This program uses the Little-Crab-3 scenario complete the following steps. Start with a fresh un-edited copy of Little-Crab-3. When finished paste each of your class source code files into word and submit through Canvas. 1.   Change the code to make the crabs turn left when the l (lowercase L) key is pressed. Make the crabs turn right when the r key is pressed. public void act()    {        if(Greenfoot.isKeyDown("l"))        {            turn(-15);        }        if (Greenfoot.isKeyDown("r"))        {            turn(15);...
Python programming question: Suppose i have a list like: ['a:10', 'b:9', 'c:8', 'd:7', 'e:6', 'f:5', 'g:4',...
Python programming question: Suppose i have a list like: ['a:10', 'b:9', 'c:8', 'd:7', 'e:6', 'f:5', 'g:4', 'h:3', 'i:2', 'j:1', 'k:0'] How do i trans form this into a dictionary or something i can plot a graph with using these keys and value pairs? thanks.
7–1:In what fundamental ways does activity-based costing differ from traditional costing methods such as job-order costing...
7–1:In what fundamental ways does activity-based costing differ from traditional costing methods such as job-order costing as described in Chapters 2 and 3? - 7–2:Why is direct labor a poor base for allocating overhead in many companies? - 7–3:Why are top management support and cross-functional involvement crucial when attempting to implement an activity-based costing system? - 7–4:What are unit-level, batch-level, product-level, customer-level, and organization-sustaining activities? - 7–5:What types of costs should not be assigned to products in an activity-based costing...
7–1:In what fundamental ways does activity-based costing differ from traditional costing methods such as job-order costing as described in Chapters 2 and 3?
  7–1:In what fundamental ways does activity-based costing differ from traditional costing methods such as job-order costing as described in Chapters 2 and 3? - 7–2:Why is direct labor a poor base for allocating overhead in many companies? - 7–3:Why are top management support and cross-functional involvement crucial when attempting to implement an activity-based costing system? - 7–4:What are unit-level, batch-level, product-level, customer-level, and organization-sustaining activities? - 7–5:What types of costs should not be assigned to products in an activity-based...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT