Question: In python, Here in Canada, we watch our neighbors to the south with great interest when it comes to their elections. Their electoral system is
In python,
Here in Canada, we watch our neighbors to the south with great interest when it comes to their elections. Their electoral system is very different from ours - at the end of the process, an Electoral College makes the final votes for the President and Vice President. You are provided with a file called Election2020Results.txt that contains the result data, in the format specified below.
File Format:
The file is formatted as follows:
#State,Votes,Biden,Trump,Harris,Pence Alabama, 9, 0, 9, 0, 9 Alaska, 3, 0, 3, 0, 3 Arizona, 11, 11, 0, 11, 0 . . . The first line, which has # as its first character, contains the definitions of what the following lines of data represent. You do not want to process that - just throw that line away (that is, skip processing it.) No other lines contain # (but would it matter to your code if they did?)
The data itself is separated by commas - which you can use to split the data; and there are tabs in the data, which you will want to replace. Then you'll need to strip any leading or trailing whitespace off of each element.
Tasks:
0. Read the data into an appropriate structure (you know how to use lists, tuples, sets, and dictionaries. You choose whatever you think will work best.)
1. Show the number of states won by the Republican candidates (Trump / Pence).
2. Show the number of states won by the Democratic candidates (Biden / Harris).
3. Print the names of the states where split voting was detected - that is, where both parties were voted for - in ascending alphabetical order by state name.
4. List all of the states, one name per line, that were won by the Republican party, sorted in ascending alphabetical order by the state name.
5. List all of the states, one name per line, that were won by the Democratic party, sorted in ascending alphabetical order by the state name.
6. Print the average number of Electoral College votes a state has, to two decimal places.
If you are clever in your use of the data structures you know, and the functions of Python, you can do this quite efficiently.
Sample output:
In this sample output, alphabetic characters are shown as Aaaa..., numeric characters are shown as n...
There are no trailing spaces after any line. Follow the format EXACTLY:
NUMBER OF REPUBLICAN STATES: nn NUMBER OF DEMOCRATIC STATES: nn STATES WITH SPLIT VOTES: Aaaaaa... Aaaaaa... . . . Aaaaaa... REPUBLICAN STATE LIST: Aaaaaa... . . . Aaaaaa... DEMOCRATIC STATE LIST: Aaaaaa... Aaaaaa... . . . Aaaaaa... AVERAGE ELECTORAL VOTES PER STATE: nn.nn
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
