-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfs.py
166 lines (152 loc) · 5.83 KB
/
dfs.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import csv
import sys
import argparse
import networkx as nx
from tabulate import tabulate
from datetime import datetime
def read_input(till_round):
G = nx.Graph()
teams_data = {}
matches = []
is_round = True
with open("matches.csv", "r") as file:
reader = csv.reader(file)
next(reader) # skip header row
for row in reader:
if isinstance(till_round,int):
round_num = int(row[0].strip())
is_round = True
if round_num > till_round:
continue
else:
match_date = row[1]
match_date = datetime.strptime(match_date, "%d/%m/%Y")
if not isinstance(till_round,datetime):
try:
till_round = datetime.strptime(till_round, "%d/%m/%Y")
except:
print("The date does not must be in the following format '%MM%MM%YYYY' \nuse --help for help ")
sys.exit()
is_round = False
if match_date > till_round:
break
try:
if not is_round:
round_num = int(row[0].strip())
else:
match_date = row[1]
home_team = row[2]
away_team = row[3]
home_score = int(row[4])
away_score = int(row[5])
result = row[6]
except:
continue
matches.append(
[
round_num,
match_date,
home_team,
away_team,
home_score,
away_score,
result,
]
)
# add match data to home team's list of matches
if G.has_edge(home_team, away_team):
old = G.get_edge_data(home_team, away_team)["match_index"]
old.append(len(matches) - 1)
G.add_edge(home_team, away_team, match_index=old)
else:
G.add_edge(home_team, away_team, match_index=[len(matches) - 1])
if home_team not in teams_data:
teams_data[home_team] = {
"name": home_team,
"match_played": 0,
"w": 0,
"d": 0,
"l": 0,
"gf": 0,
"ga": 0,
"gd": 0,
"points": 0,
}
if away_team not in teams_data:
teams_data[away_team] = {
"name": away_team,
"match_played": 0,
"w": 0,
"d": 0,
"l": 0,
"gf": 0,
"ga": 0,
"gd": 0,
"points": 0,
}
# all_edges = list(G.edges()) + [(v, u) for u, v in G.edges()]
# for i in all_edges:
# print(i)
return [G, matches, teams_data]
def dfs(until_round):
G, matches, teams_data = read_input(until_round)
# unvisited = list(input[0].nodes)
for teams in nx.edge_dfs(G):
match_index = G.get_edge_data(teams[0], teams[1])["match_index"]
# print(teams)
# update each match data in the team hash
for match in match_index:
# update home team
teams_data[teams[0]]["match_played"] += 1
# update away team
teams_data[teams[1]]["match_played"] += 1
teams_data[matches[match][2]]["gf"] += matches[match][4]
teams_data[matches[match][2]]["ga"] += matches[match][5]
teams_data[matches[match][2]]["gd"] = (
teams_data[matches[match][2]]["gf"] - teams_data[matches[match][2]]["ga"]
)
teams_data[matches[match][3]]["gf"] += matches[match][5]
teams_data[matches[match][3]]["ga"] += matches[match][4]
teams_data[matches[match][3]]["gd"] = (
teams_data[matches[match][3]]["gf"] - teams_data[matches[match][3]]["ga"]
)
if matches[match][6] == "H":
teams_data[matches[match][2]]["w"] += 1
teams_data[matches[match][2]]["points"] += 3
teams_data[matches[match][3]]["l"] += 1
if matches[match][6] == "A":
teams_data[matches[match][3]]["w"] += 1
teams_data[matches[match][3]]["points"] += 3
teams_data[matches[match][2]]["l"] += 1
if matches[match][6] == "D":
teams_data[matches[match][3]]["d"] += 1
teams_data[matches[match][2]]["points"] += 1
teams_data[matches[match][3]]["points"] += 1
teams_data[matches[match][2]]["d"] += 1
res = list(teams_data.values())
res.sort(reverse=True, key=lambda i: i["points"])
# for t in res:
# print(t)
headers = res[0].keys()
table_data = [list(row.values()) for row in res]
print(tabulate(table_data, headers=headers, tablefmt="fancy_grid"))
def main():
parser = argparse.ArgumentParser(
description="""
display the standing of the league for a given round or date
"""
)
parser.add_argument("-r", "--round", type=int, help="round number")
parser.add_argument("-d", "--date", type=str, help="standing date")
args = parser.parse_args()
r = args.round
date = args.date
if r is not None and date is None:
dfs(r)
elif r is None and date is not None:
dfs(date)
else:
print("use --help for help ")
sys.exit()
if __name__ == "__main__":
main()