Question
Please solve as soon as possible, nee help with computer science project Definitions CSV file : This is a file containing plain text where each
Please solve as soon as possible, nee help with computer science project
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 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
a
s 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 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
a
s described above
Return value
:
the dictionary object created or False for any kind of error
Examples
:
db = read_abbreviations(abbreviations.csv)
db is a databse2 dictionary
db = read_abbreviations(file_with_wrong_format)
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
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
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 winners 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 dont 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', 'Iowa', 'Georgia', 'Ohio', 'Texas', 'Montana', 'Kansas', 'Indiana', 'South Carolina', 'Missouri', 'Nebraska', 'Idaho',
'South Dakota', 'Louisiana', 'North Dakota', 'Mississippi', 'Tennessee', 'Arkansas']
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started