-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestMarginQuery.cpp
More file actions
54 lines (47 loc) · 1.57 KB
/
Copy pathLargestMarginQuery.cpp
File metadata and controls
54 lines (47 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "LargestMarginQuery.hpp"
#include "QueryFactory.hpp"
#include "Game.hpp"
#include "Team.hpp"
#include <algorithm>
#include <string>
#include <sstream>
#include <cassert>
#include <iostream>
using namespace std;
// Register class with the Query Factory
REGISTER_QUERY_CLASS("largestMargin", LargestMarginQuery);
LargestMarginQuery::LargestMarginQuery()
{
_numberOfArguments = 0;
_usageMessage = "Usage: ./madness largestMargin <season>";
}
// Query for finding the team with the largest margin of victory in a season
std::vector<std::string> LargestMarginQuery::operator()(std::map<int, Team *> teams, std::vector<std::string> arguments)
{
assert(arguments.size() == 0);
std::vector<string> results;
Team *champ = NULL;
float max = -1;
// Find element with maximum winning margin
for_each(teams.begin(), teams.end(), [&champ, &max](pair<int, Team *> a) {
double margin = 0;
Team *team = a.second;
auto gamesPlayed = team->gamesPlayed();
if(gamesPlayed.size()) {
pair<double, double> init(0.0, 0.0);
// Sum the points gained and points allowed
auto scores = accumulate(gamesPlayed.begin(), gamesPlayed.end(), init, [team](pair<double, double> running, Game *game) {
running.first += (game->isWinner(team) ? game->winningScore() : game->losingScore());
running.second += (!game->isWinner(team) ? game->winningScore() : game->losingScore());
return running;
});
margin = (scores.first - scores.second)/gamesPlayed.size();
}
if(margin > max) {
champ = a.second;
max = margin;
}
});
results.push_back(champ->name());
return results;
}