1. Read in and format the data from XXXXXXXXXXpresident.csv Write a function that takes the name of the file (the default should be XXXXXXXXXXpresident.csv ) and a general election year from the...


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.

Oct 21, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here