A tough one

You have been hired by a presidential campaign (either Hillary Clinton or Donald Trump) to write programs that analyze 2012 election results and help come up with a winning strategy for your candidate.
Background Information:

The US election rules are as follows. Each of the 51 states has a fixed number of electoral votes. The winner of the popular vote for a state gets all the electoral votes for the state. For example, NY has 29 electoral votes, and Clinton would get all 29 even if she wins by 50.01% to 49.99%. There are a total of 538 electoral votes, and you need 270 to become president.

There are several standard terms used in this context. A percentage point is the unit for the difference between two percentages. The margin of victory of A over B refers to the number of percentage points of A's lead, and is negative if A lost. For example, if A defeated B by a 51% - 47% vote (the remaining 2% went to other candidates), A's margin of victory is 4 points and B's margin of loss is 4 points (or a margin of victory of -4 points).

Remarks:

The information above is somewhat simplified from the real election rules in the following ways:
Washington DC is not a state, but it gets [a reduced number of] electoral votes. Thus we will incorrectly refer to it as a state for this program.
NE and ME use slightly different rules to allocate electoral votes, but we will not worry about that.

Input:
You have available to you two files:

elect12.csv

This file contains the popular vote results from the 2012 election. There is one row per state (including DC), and the file is sorted by state name. Each row has 5 fields in order:
Popular vote for Obama
Popular vote for Romney
Popular vote for all other candidates
Total popular vote in state
The state's abbreviation
The first 4 columns are integers, while the last one is a string (the string happens to be 2 characters, but that shouldn't affect you for this program).

ev10.csv

This file contains the number of electoral votes in each state. Like above, each row corresponds to a state, but the file is sorted differently (by electoral vote). Each row has 2 fields in order:
Number of electoral votes for state The state's abbreviation

Although you know that both files have 51 rows, your programs described below should work for an arbitrary number of rows. Thus, you may not store all the information in variables, and need to make multiple passes through the files. Obviously, your program must actually process the file and not use facts that you know happened (e.g., it shouldn't assume Obama won). Indeed, you may wish to edit the files when testing your program. However, you may assume that the two major candidates placed first and second in every state. You may also assume that the two input files are consistent - i.e., both contain the same states (though not in the same order).


Task 0:

Print the national results, along with the [unweighted] average margin of victory for the winner (P). For example (in a 3 state nation), if P had won the popular vote by 10, 20, and -15 points, P's average margin would be 5. Also determine the states where P had the best and worst winning margins.
Print a message to the screen as follows (N should be a double):
P won the national election with N% of the popular vote and a margin of N points.
P received N electoral votes.
P had an unweighted average victory margin of N points.
P's best state was XX, where he won by N points.
P's worst state was XX, where he lost by N points.
If the election resulted in an electoral vote tie, output the same results as above but replacing the first line with:
The election resulted in a tie!
In that case, you may substitute either candidate for P in the remaining 4 lines.

Task 1:

The Clinton/Trump campaigns want to target advertising to specific regions in the nation. To do this, they want you to analyze the data for particular regions of the nation. To do this, you should first prompt the user to enter the number of states in the region, reprompting if the user enters a non-positive number (you don't need to check that there are fewer than 51 states in the region since this program is intended to work for arbitrary files). Then, prompt for each of the state codes one at a time. The user will not necessarily enter them in sorted order. Finally, print the following message to the screen:
{Obama,Romney} won the popular vote by N points in the region.
Note that the {} above is standard notation to indicate that you print one of the options.

If at any point an illegal state is entered, you should just print the following error message:
XX is not a legal state
In that case, you should reprompt for the entire region (but not region size) again. You do not need to check that the entered states are distinct.
Last edited on
I have the files but its not letting me put it here.
Topic archived. No new replies allowed.