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!')...
WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you...
WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you will write functions to encrypt and decrypt messages using simple substitution ciphers. Your solution MUST include: a function called encode that takes two parameters: key, a 26-character long string that identifies the ciphertext mapping for each letter of the alphabet, in order; plaintext, a string of unspecified length that represents the message to be encoded. encode will return a string representing the ciphertext. a...
C++ Programming Chapter 7 Assignment: Assignment #4 – Student Ranking : In this assignment you are...
C++ Programming Chapter 7 Assignment: Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display...
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...
In Python For this programming assignment, we are going to investigate how much "work" different sorting...
In Python For this programming assignment, we are going to investigate how much "work" different sorting routines do, based on the input size and order of the data. We will record the work done by writing output CSV (comma separated value) files and creating various plots using matplotlib. Note: for this assignment, do not use Jupyter Notebook to code your solution. Use standard .py files and save your output to .csv and .png files (see the program details below for...
Python Programming This assignment will give you practice with interactive programs, if/else statements, collections, loops and...
Python Programming This assignment will give you practice with interactive programs, if/else statements, collections, loops and functions. Problem Description A small car yard dealing in second hand cars needs an application to keep records of cars in stock. Details of each car shall include registration(rego), model, color, price paid for the car (i.e. purchase price) and selling price. Selling price is calculated as purchased price plus mark-up of 30%. For example, Toyota Corolla bought for $20,000 will have the selling...
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
Chapter 7, Problem 3PE from Introduction to Programming using Python by Y. Daniel Liang. Problem (The...
Chapter 7, Problem 3PE from Introduction to Programming using Python by Y. Daniel Liang. Problem (The Account class) Design a class named Account that contains: ■ A private int data field named id for the account. ■ A private float data field named balance for the account. ■ A private float data field named annualInterestRate that stores the current interest rate. ■ A constructor that creates an account with the specified id (default 0), initial balance (default 100), and annual...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT