Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CSV file: This is a file containing plain text where each line in the file represents one record of information, and each piece of info

CSV file: This is a file containing plain text where each line in the file represents one record of information, and each piece of info in the record is separated by a single comma. Luckily the values won't ever contain commas themselves for this project. But they might start or end with non visible characters (e.g. space, tab). Thus, you must remove the leading and trailing spaces when you store the data in a dictionary in memory. The very first line is the header row, which names the columns but is not part of the data, and therefore should not be loaded into memory. Note: the filename extension you use has no effect on the contents; you can edit it and give it any extension you want without changing the ability of your program. In this project well be using two kinds of CSV files: votes.csv file: It contains records of votes in the following format: state, candidate, party, popular_votes, electoral_votes AL, Johnson, IND, 44467, 0 AL, Stein, IND, 9391, 0 AK, Trump, REP, 163387, 9 VA, Clinton, DEM, 1981473, 13 Note: the total number of electoral votes in the file is 531 instead of 538 because 7 votes of the electoral college were not given as promised! abbreviations.csv file: It contains abbreviations of strings in the following format: abbreviation, full_string AL, Alabama AK, Alaska VA, Virginia Johnson, Gary Johnson Stein, Jill Stein IND, Independent REP, Republican DEM, Democratic For storage efficiency all files contain string abbreviations. When we load the files into memory we will also be using the same abbreviations for memory efficiency. But when we present results as output of the program we will be using the full strings. Candidate: we will use the following representation for a candidate inside our program: a tuple containing these values in this order. Note that name and party are string abbreviations, and popular_votes and electoral_votes are integers. candidate = (name, party, popular_votes, electoral_votes) Database1: a database of votes can store all candidates votes for each State. Obviously, a candidates name may get reused in different States, but never in the same State. Not all candidate names appear in every State. Our database must be a dictionary whose keys are States, and whose values are lists of candidate values. Only candidates with positive number of popular_votes may be present in a State. Empty lists are not allowed as values. Candidates in the same State must be stored alphabetically by their abbreviated name (hint: use insert instead of append when adding items to the list because you cant use sorting functions). votes_db = { AL: [(Johnson, IND, 44467, 0), (Stein, IND, 9391, 0)], AK: [(Trump, REP, 163387, 9)], VA: [(Clinton, DEM, 1981473, 13)] ... } Database2: a database of abbreviations will store all abbreviations as a dictionary whose keys are the abbreviated strings and whose values are the equivalent full strings, like in the following sample: abbr_db = { AL: Alabama, AK: Alaska, DEM: Democratic, REP: Republican, Trump: Donald Trump ... } Functions read_votes(filename) Description: It reads votes from a file and creates in memory a dictionary of database1 type Parameters: a filename of a CSV formated votes file as described above Return value: the dictionary object created or False for any kind of error Examples: db = read_votes(votes.csv) db is a database1 dictionary db = read_votes(file_with_wrong_format) db = False write_votes(db, filename) Description: It writes the contents of a database1 dictionary back to a CSV file in the same format as it was the original input file Parameters: a database1 and a filename Return value: True for success or False for any kind of error Examples: write_votes(db, new_votes.csv) True (if db is a database1 dictionary) write_votes(db, new_votes.csv) False (if db is an empty dictionary or another type are correct, no need for this kind of checks. Parameters: database1 and details for a candidate Return value: None Examples: add_candidate(db, VA, Clinton, DEM, 1234567, 10) None add_candidate(db, TX, Trump, REP, 2345678, 23) None remove_candidate(db, name, state=None) Description: It removes a candidate from either one or all the States. Keep in mind that after removal dictionary keys cannot point to empty lists. Parameters: database1, the abbreviated name of a candidate and the abbreviated name of a State. If state is not provided it will remove the candidate from all States. Return value: False if state doesnt exist in dictionary, None otherwise Examples: remove_candidate(db, Clinton) None remove_candidate(db, Trump, TX) None remove_candidate(db, Trump, VR) False incorporate_precinct(db, name, state, popular_votes_increment) Description: It increases the number of popular votes for a certain candidate in a specific State. Parameters: database1, name of candidate, State, increment of popular votes Return value: False if state and/or candidate dont exist in dictionary, None otherwise Examples: incorporate_precinct(db, Stein, VA, 1000) None incorporate_precinct(db, Stein, VR, 1000) False incorporate_precinct(db, Socrates, VA, 1000) False merge_votes(db, name1,name2, new_name, new_party, state=None) Description: It merges the votes of two candidates in a given State. If state is not provided it will merge the two candidates in all States. If only one of the two candidates is present in a certain State, the renaming will still take place. The function assumes the inputs and the types are correct, no need for this kind of checks. Parameters: database1, the abbreviated names of two candidates, the new name after merging, the new party name, and the abbreviated State name Return value: None Examples: merge_votes(db, Johnson, Stein, Other, IND, RI) None merge_votes(db, Johnson, Stein, Other, IND) None number_of_votes(db, name, category=popular, numbering=tally, state=None) Description: It counts the number of votes for a certain candidate in one or all the States Parameters: database1, the name of the candidate, optional category is popular or electoral (default: popular), optional numbering is tally or percent (default: tally). Parameter state is optional, if omitted the counting is over the whole country. All parameters must be checked for correctness, do not assume the arguments are correct. Return value: integer if numbering is tally, float rounded to 2 decimals if numbering is percent. False if error (e.g. wrong name or name thats not included in this State, or numbering is an invalid string, etc.) Examples: number_of_votes(db, Trump, popular, tally, VA) 1769443 number_of_votes(db, Clinton, electoral) 227 number_of_votes(db, Johnson, popular, percent) 3.28 number_of_votes(db, Johnson, POPULAR, percent) False number_of_votes(db, McMullin) 731991 number_of_votes(db, Trump, popular, tally, VR) False number_of_votes(db, Johnson, popular, Percent) False popular_votes_performance(db, name, numbering, order=max) Description: It finds the State where the candidate got the maximum (or the minimum) number of popular votes, either by tally or percent. You dont have to consider the case of a tie between two States. Parameters: database1, the name of the candidate, numbering is tally or percent, optional order is max or min (default: max) Return value: full name of State, False if error (e.g. candidate doesnt exist or order has an invalid value) Examples: popular_votes_performance(db, Trump, percent, min) District of Columbia popular_votes_performance(db, Trump, percent, best) False popular_votes_performance(db, Clinton, tally, min) Wyoming candidates_difference(db, name1, name2, order=smallest) Description: It finds the State where the two candidates had the smallest or largest difference in popular_vote as percentage of State votes. States that dont contain both names dont count, but if the two candidates did not compete in any State, thats an error. Parameters: database1, the names of the two candidates, optional order is smallest or largest (default: smallest) Return value: full name of State, False if error (e.g. order has invalid value, the two candidates do not compete in any State) Examples: candidates_difference(db, McMullin, Johnson, largest) Utah candidates_difference(db, McMullin, Johnson, best) False candidates_difference(db, Trump, Clinton) Michigan

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David J. Auer

3rd Edition

0131986252, 978-0131986251

More Books

Students also viewed these Databases questions

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago