Advertisement
aaronvan

gerrymanderingRatios

Feb 15th, 2019
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. Map<string, double> gerrymanderingRatios(const string &file) {
  2.     Map<string, int> countMap;  
  3.     Map<string, vector<string>> districtVotes; // district : votes
  4.     Map<string, string> districtWinner;
  5.     Map<string, double> gerryMap;       // party : ratio (returned)
  6.     double totalVotes = 0.0;
  7.     double districtCounter = 0.0;
  8.     double ratio = 0.0;
  9.    
  10.     ifstream file_in(file);
  11.     if (file_in) {
  12.         string line;
  13.         string districtName;
  14.         string partyVote;
  15.         while (file_in >> districtName, getline(file_in, line)) {
  16.             stringstream ss(line);
  17.             while (ss >> partyVote) {
  18.                 try {
  19.                     ++countMap[partyVote];
  20.                 }
  21.                 catch (out_of_range) {
  22.                     countMap[partyVote] = 1;
  23.                 }
  24.                 districtVotes[districtName].push_back(partyVote);
  25.                 ++totalVotes;
  26.             }
  27.             string winner;
  28.             for (auto looker : districtVotes) {
  29.                 sort(districtVotes[districtName].begin(), districtVotes[districtName].end());
  30.                 int max = 0;
  31.                 for (size_t i = 0; i < districtVotes[districtName].size(); ++i) {
  32.                     int counter = count(districtVotes[districtName].begin(), districtVotes[districtName].end(), districtVotes[districtName][i]);
  33.                     if (counter > max) {
  34.                         max = counter;
  35.                         winner = districtVotes[districtName][i];
  36.                     }
  37.                 }
  38.             }
  39.             districtWinner[districtName] = winner;
  40.             ++districtCounter;
  41.         }
  42.     }
  43.     file_in.close();
  44.    
  45.     Map<string, int>::iterator voteCounter = countMap.begin(); // countMap
  46.     for (; voteCounter != countMap.end(); ++voteCounter) {
  47.         Map<string, string>::iterator partyCounter = districtWinner.begin(); // districtWinner
  48.         int districtsWon = 0;
  49.         for (; partyCounter != districtWinner.end(); ++partyCounter) {
  50.             if (*voteCounter == districtWinner.get(*partyCounter))
  51.                 ++districtsWon;
  52.         }
  53.         double percentOfVotes = countMap.get(*voteCounter) / totalVotes; // total votes / # of party votes
  54.         double percentDistrictsWon = districtsWon / districtCounter; // districts won by party / # of districts
  55.         ratio = percentDistrictsWon / percentOfVotes;
  56.         gerryMap[*voteCounter] = ratio;
  57.     }
  58.     return gerryMap;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement