Skip to content

Commit df780a9

Browse files
committed
team sorting
1 parent b7b3280 commit df780a9

File tree

6 files changed

+164
-13
lines changed

6 files changed

+164
-13
lines changed

.coverage

52 KB
Binary file not shown.

.github/workflows/pytest.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
python-version: "3.12"
1717
- name: Install dependencies
1818
run: |
19-
python -m pip install --upgrade pytest
19+
python -m pip install -r requirements.txt
20+
python -m pip install pytest
2021
- name: Test
2122
run: pytest

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests

src/__init__.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import json
99
import glob
1010

11-
from src.teams import getTeamPoints
11+
from src.teams import Team, getTeamCoefficientAverage, getTeamPoints
1212

1313
from .lines import parseFile
1414

@@ -42,7 +42,7 @@ def process():
4242
print("Missing JSON data file of teams, please run download first")
4343
sys.exit()
4444
with open(TEAM_JSON, 'r') as jsonFile:
45-
teams = json.load(jsonFile)
45+
teamsJsonData = json.load(jsonFile)
4646

4747
# load in files
4848
if not os.path.exists(IN_DIR):
@@ -56,7 +56,7 @@ def process():
5656
print(inputFiles)
5757

5858
if (len(inputFiles) == 0):
59-
print("No input files found")
59+
print("No input file found")
6060
sys.exit()
6161

6262
teamTasks = {}
@@ -65,7 +65,20 @@ def process():
6565
print(teamTasks)
6666

6767
teamPoints = getTeamPoints(teamTasks)
68-
print(teamPoints)
68+
69+
teams = []
70+
for teamId in teamPoints:
71+
coeffAvg = getTeamCoefficientAverage(
72+
teamsJsonData[str(teamId)]['members'])
73+
teams.append(Team(
74+
teamId,
75+
teamPoints[teamId],
76+
coeffAvg,
77+
teamsJsonData[str(teamId)]['category']
78+
))
79+
80+
teams.sort()
81+
print(teams)
6982

7083

7184
def main():

src/teams.py

Lines changed: 96 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,102 @@ def __init__(self, points, teamId, task) -> None:
1212

1313
class InvalidTaskException(Exception):
1414
def __init__(self, task, teamId) -> None:
15-
super().__init__(
16-
f"Invalid task {task} for team {teamId}")
15+
super().__init__(f"Invalid task {task} for team {teamId}")
16+
17+
18+
class InvalidStudyYearException(Exception):
19+
def __init__(self, studyYear) -> None:
20+
super().__init__(f"Invalid study year {studyYear}")
21+
22+
23+
class Team:
24+
def __init__(self, teamId, points, coefficientAverage, category) -> None:
25+
self.teamId = teamId
26+
self.pointsSum = points['sum']
27+
self.pointsCounts = points['counts']
28+
self.coefficientAverage = coefficientAverage
29+
self.category = category
30+
31+
def __repr__(self) -> str:
32+
return f"<{self.teamId}, {self.category}>"
33+
34+
def __eq__(self, obj):
35+
return self.teamId == obj.teamId
36+
37+
def __lt__(self, obj):
38+
if self.teamId == obj.teamId:
39+
return False
40+
# compare sum
41+
if self.pointsSum > obj.pointsSum:
42+
return True
43+
if self.pointsSum < obj.pointsSum:
44+
return False
45+
46+
# compare averages -> sum is same, so compare counts
47+
if self.getTotalPointsCount() < obj.getTotalPointsCount():
48+
return True
49+
if self.getTotalPointsCount() > obj.getTotalPointsCount():
50+
return False
51+
52+
# more 5 point submits
53+
if self.getPointsCount(5) > obj.getPointsCount(5):
54+
return True
55+
if self.getPointsCount(5) < obj.getPointsCount(5):
56+
return False
57+
58+
# more 3 point submits
59+
if self.getPointsCount(3) > obj.getPointsCount(3):
60+
return True
61+
if self.getPointsCount(3) < obj.getPointsCount(3):
62+
return False
63+
64+
# lower coefficient
65+
if self.coefficientAverage < obj.coefficientAverage:
66+
return True
67+
if self.coefficientAverage > obj.coefficientAverage:
68+
return False
69+
70+
# lower team id
71+
if self.teamId < obj.teamId:
72+
return True
73+
74+
return False
75+
76+
def getTotalPointsCount(self) -> int:
77+
count = 0
78+
for points in self.pointsCounts:
79+
count += self.pointsCounts[points]
80+
return count
81+
82+
def getPointsCount(self, point):
83+
if point in self.pointsCounts:
84+
return self.pointsCounts[point]
85+
return 0
86+
87+
88+
def studyYearToCoefficient(studyYear: str):
89+
match studyYear:
90+
case 'H_1':
91+
return 1
92+
case 'H_2':
93+
return 2
94+
case 'H_3':
95+
return 3
96+
case 'H_4':
97+
return 4
98+
case 'U_ALL' | None:
99+
raise InvalidStudyYearException(studyYear)
100+
case _:
101+
return 0
102+
103+
104+
def getTeamCoefficientAverage(members) -> float:
105+
memberCount = 0
106+
coeffSum = 0
107+
for member in members:
108+
memberCount += 1
109+
coeffSum += studyYearToCoefficient(member['studyYear'])
110+
return coeffSum/memberCount
17111

18112

19113
def appendTeamsData(teamId: int, task: str, points: int, teamTasks: dict) -> None:
@@ -55,8 +149,3 @@ def getTeamPoints(teamTasks: dict) -> dict:
55149
'counts': pointsCount
56150
}
57151
return teamPoints
58-
59-
60-
# def orderTeams(teamPoints: dict):
61-
# teams = []
62-
# for ()

src/teams_test.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
2+
import random
23

3-
from src.teams import DuplicatePointsEntryException, InvalidPointsException, InvalidTaskException, appendTeamsData, getTeamPoints
4+
from src.teams import DuplicatePointsEntryException, InvalidPointsException, InvalidTaskException, Team, appendTeamsData, getTeamPoints
45

56

67
def test_simple():
@@ -107,3 +108,49 @@ def test_points_sum():
107108
}
108109
}
109110
}
111+
112+
113+
def get_test_teams():
114+
return [
115+
Team(8, {'sum': 15, 'counts': {5: 3}}, 4, 'A'),
116+
Team(2, {'sum': 8, 'counts': {5: 1, 3: 1}}, 4, 'B'),
117+
Team(1, {'sum': 8, 'counts': {5: 1, 2: 1, 1: 1}}, 4, 'A'),
118+
Team(4, {'sum': 8, 'counts': {3: 2, 2: 1}}, 4, 'B'),
119+
Team(3, {'sum': 4, 'counts': {3: 1, 1: 1}}, 4, 'A'),
120+
Team(7, {'sum': 4, 'counts': {2: 2}}, 1, 'A'),
121+
Team(5, {'sum': 4, 'counts': {2: 2}}, 4, 'B'),
122+
Team(6, {'sum': 4, 'counts': {2: 2}}, 4, 'A')
123+
]
124+
125+
126+
def test_team_compare_gt():
127+
teams = get_test_teams()
128+
for i in range(0, 7):
129+
assert not teams[i] < teams[i]
130+
assert not teams[i] > teams[i]
131+
for j in range(i+1, 7+1):
132+
assert teams[i] < teams[j]
133+
assert not (teams[i] > teams[j])
134+
135+
136+
def test_team_sort():
137+
teams = [
138+
Team(8, {'sum': 15, 'counts': {5: 3}}, 4, 'A'),
139+
Team(2, {'sum': 8, 'counts': {5: 1, 3: 1}}, 4, 'B'),
140+
Team(1, {'sum': 8, 'counts': {5: 1, 2: 1, 1: 1}}, 4, 'A'),
141+
Team(4, {'sum': 8, 'counts': {3: 2, 2: 1}}, 4, 'B'),
142+
Team(3, {'sum': 4, 'counts': {3: 1, 1: 1}}, 4, 'A'),
143+
Team(7, {'sum': 4, 'counts': {2: 2}}, 1, 'A'),
144+
Team(5, {'sum': 4, 'counts': {2: 2}}, 4, 'B'),
145+
Team(6, {'sum': 4, 'counts': {2: 2}}, 4, 'A')
146+
]
147+
sorted(teams, key=lambda team: team.teamId)
148+
print(teams)
149+
assert teams[0].teamId == 8
150+
assert teams[1].teamId == 2
151+
assert teams[2].teamId == 1
152+
assert teams[3].teamId == 4
153+
assert teams[4].teamId == 3
154+
assert teams[5].teamId == 7
155+
assert teams[6].teamId == 5
156+
assert teams[7].teamId == 6

0 commit comments

Comments
 (0)