Mastery Project 2
Always backup your work to a system that you don’t control, such as OneDrive or Google Drive, in case something goes wrong with Canvas. These put a timestamp on your work that can be used to prove when it was done. We cannot accept timestamps from your local machine.
You are responsible for making sure your work is submitted by the due date on Canvas, so it is strongly advised that you verify the submission once you’ve turned it in. We accept Mastery Projects up to 24 hours late, for a 20% penalty.
As a reminder, while working on this project, you may use anything on the Canvas section for I210, as well as Zybooks. You may use any notes (physical or online) taken for I210.
Using code you found online from outside of this class or code that you did not write that is not Zybooks code or from our Canvas section will constitute academic misconduct. If you are unsure, ask first.
If you have questions about what we want you to do in this Mastery Project, ask in Piazza. If you get stuck writing your code and need help, come to an Office Hour. Start early so that there’s as much time as possible to get help!
Mastery Project 2 requires you to use pieces from Lesson 11 – Lesson 21, so you won’t be able to complete all elements until you’ve done this material. Keep an eye out for pieces that you’ll need!
In this project, we use real-world data from the MIT Election Data and Science Lab. The dataset you will be using is a report of the general elections from 1976 until 2016. Make sure you open the data in a spreadsheet program and/or in your code editor so you can see what you’re working with before manipulating the data with code.
There are 4 parts to Mastery Project 2:
1. Read in and format the data from 1976-2016-president.csv
Write a function that takes the name of the file (the default should be 1976-2016-president.csv) and a general election year from the user. The function will read the CSV data in using a DictReader and return a nested list where each element is a tuple. After reading in the whole file, use a list comprehension to read the rows for the given year.
When the data is loaded, let the user know the action is complete with a message. It’s a good idea when working with files to also include how large the dataset is that we are working with (in this case, how many rows).
2. Create a module for sorting and displaying the data
Write a module for importing the data that includes the following functions:
a) A function (table_print), that prints the data in the table formatted with two columns of the given width and the given header
(hint: see lesson 18)
b) A function that uses insertion sort or selection sort, to sort a list. The input is a nested list (list of lists) and a column (col). The function sorts the rows of that list based on the values in the column col.
(hint: see lesson 16)
c) A function named tallied_data() that takes in a nested list (Use this on the data from part 1, but it should be able to be used to solve similar problems), indices for two columns and returns a tallied list. The inputs are:
i) a nested list,
ii) an index for the ‘reference column’/ ‘category’ (col_ref)
iii) another index for the column to be tallied (col_tally)
iv) this function returns a list of tuples where each element is a tuple with two elements, the first one is the reference/category and the second one is the tallied sum for all the rows that have the reference. This list will be returned sorted in descending order based on the tallied numbers.
(hint: see Lesson 19)
For example using the tallied_data() function from part c:
data = [[Indiana, 2, 'Clinton', 4], [‘Colorado’, 2, ‘Bush’, 56], [‘Colorado’, 2, 'Clinton', 100], [‘Illinois’, 3, ‘Gore’, 8], [Oregon, 3, 'Clinton', 4], [Indiana, 4, ‘Bush’, 2]]
print(tallied_data(data, 2, 3)) will display [('Clinton', 108), ('Bush', 58), ('Gore', 8)]
print(tallied_data(data, 0, 3)) will display [(‘Colorado’, 156), (‘Illinois’, 8), (‘Indiana’, 6), (‘Oregon’, 4)]
3. Identify a focus to refine the data
Ask the user for the file name, and the general election year they are interested in. Make sure to use error handling if the user enters a wrong file name or a wrong year (either not a general election year, or not in the range 1976-2016, or not a valid number at all).
We want to be able to write functions to answer the following 4 questions using the data:
a. Given an election year, how many votes did each candidate receive total (in the whole country)? The result should be printed in a descending order in a table.
b. Given an election year, how many votes did each party receive total (in the whole country)? The result should be printed in a descending order in a table.
c. Given an election year, how many votes were “write-in” votes?
d. Given an election year, who won the popular vote?
Ask the user which question they want to answer and then print a table relevant to the question and print a statement if necessary to answer the question.
4. BONUS: Save the results to a file
Extend your code to print more information and save the results to a file. Given an election year, what was the outcome of the popular vote in that year? Print out the results showing each candidate, their party, the total number of votes they received, and what percentage of the votes they received. Format the data so it is human readable within the file. The data should be in plain text and not formatted as CSV, however, we do expect labels and some amount of formatting -- someone reading the file should be able to understand what each set of results is showing. Ask the user if they want to continue to print to the console and output the file, or just output the file.
CS 340 Milestone One Guidelines and Rubric Overview: For this assignment, you will implement the fundamental operations of create, read, update,
Retail Transaction Programming Project Project Requirements: Develop a program to emulate a purchase transaction at a retail store. This
7COM1028 Secure Systems Programming Referral Coursework: Secure
Create a GUI program that:Accepts the following from a user:Item NameItem QuantityItem PriceAllows the user to create a file to store the sales receip
CS 340 Final Project Guidelines and Rubric Overview The final project will encompass developing a web service using a software stack and impleme