Python Coding

This is a coding project in Python. I’d need the .txt and .py file. I’ll provide the tester file and the CSV documents needed for the project.

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

are correct, no need for this kind of checks.
Parameters: database 1 and details for a candidate
Return value: None
Examples:
add_candidate(db, ‘VA’, ‘Clinton’, ‘DEM’, 1234567, 10)
add_candidate(db, ‘TX’, Trump’, ‘REP’, 2345678, 23)
None
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 doesn’t exist in dictionary, None otherwise
Examples:
remove_candidate(db, ‘Clinton’)
remove_candidate(db, Trump’, TX”)
remove_candidate(db, Trump’, ‘VR)
None
None
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 don’t exist in dictionary, None otherwise
Examples:
incorporate_precinctdb, ‘Stein’, ‘VA’, 1000)
incorporate_precinctdb, ‘Stein’, ‘VR’, 1000)
incorporate_precinct(db, ‘Socrates’, ‘VA’, 1000)
None
False
False
merge_votes(db, name 1, 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’)
merge_votes(db, ‘Johnson’, ‘Stein’, ‘Other’, ‘IND’)
None
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 that’s not included in this State, or numbering is an
invalid string, etc.)
Examples:
number_of_votes(db, ‘Trump’, ‘popular’, ‘tally’, ‘VA)
number_of_votes(db, ‘Clinton’, ‘electoral)
number_of_votes(db, Johnson’, ‘popular’, ‘percent’)
number_of_votes(db, ‘Johnson’, ‘POPULAR’, ‘percent)
number_of_votes(db, ‘McMullin’)
number_of_votes(db, ‘Trump’, ‘popular’, ‘tally’, VR)
number_of_votes(db, Johnson’, ‘popular’, ‘Percent’)
ז ז ז ז ו ז
1769443
227
3.28
False
731991
False
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 don’t 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 doesn’t exist or order has an
invalid value)
Examples:
popular_votes_performance(db, ‘Trump’, ‘percent’, ‘min’)
popular_votes_performance(db, ‘Trump’, ‘percent’, ‘best’)
District of Columbia
False
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 = read_votes(‘file_with_wrong_format)
db is a database 1 dictionary
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 database and a filename
Return value: True for success or False for any kind of error
Examples:
write_votes(db, ‘new_votes.csv’)
write_votes(db, ‘new_votes.csv’)
True (if db is a database1 dictionary)
False (if db is an empty dictionary or another type of variable)
read_abbreviations(filename)
Description: It reads abbreviations from a file and creates in memory a dictionary of database2
type
Parameters: a filename of a CSV formated abbreviations file as described above
Return value: the dictionary object created or False for any kind of error
Examples:
db = read_abbreviations(‘abbreviations.csv’)
db = read_abbreviations(“file_with_wrong_format”)
db is a databse2 dictionary
db = False
add_candidate(db, state, name, party, popular_votes, electoral_votes)
Description: It accepts an existing database and a candidate name along with the relevant info. If
the candidate and/or the State already exist, it updates their info, otherwise it adds a new record
for the candidate and/or the State. The function assumes the length of the input and the types
popular_votes_performance(db, ‘Clinton’, ‘tally’, ‘min’)
Wyoming
candidates_difference(db, name 1, 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 don’t contain both names don’t count,
but if the two candidates did not compete in any State, that’s 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’)
candidates_difference(db, ‘McMullin’, ‘Johnson’, ‘best’)
candidates_difference(db, Trump’, ‘Clinton’)
Utah
False
Michigan
Extra Credit
reverse_result(db, name, winner=’Trump’)
Description: It finds the minimum set of States (in ascending order of the percentage difference
of the popular vote) in which the name should have added the winner’s electoral votes in order
to reverse the overall elections outcome and be the winner. The name of the winner is a default
parameter (=’Trump’), you don’t have to compute it. Argument name can be any valid name of a
candidate except the winner. The function is not allowed to modify the dictionary db.
Parameters: database1, the name of the candidate
Return value: a list of States with full names, False if error (e.g. invalid name)
Examples:
reverse_result(db, ‘Clinton’)
[‘Michigan’, ‘Pennsylvania’, ‘
Wisconsin’]
reverse_result(db, ‘Socrates’)
False
reverse_result(db, ‘Trump’)
False
reverse_result(db, Johnson’, ‘Trump’) + [‘Maine’, ‘Utah’, ‘Wisconsin’, ‘Michigan’, Arizona’, ‘Alaska’, ‘Pennsylvania’, ‘Florida’,
‘North Carolina’, ‘lowa’, ‘Georgia’, ‘Ohio’, ‘Texas’, ‘Montana’, ‘Kansas’, ‘Indiana’, ‘South Carolina’, ‘Missouri’, ‘Nebraska’, ‘Idaho’,
‘South Dakota’, ‘Louisiana’, ‘North Dakota’, ‘Mississippi, ‘Tennessee’, ‘Arkansas’]
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
candidate’s 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 can’t 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’

}
Definitions
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 we’ll
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.

Still stressed from student homework?
Get quality assistance from academic writers!

Order your essay today and save 25% with the discount code LAVENDER