Skip to content
This repository has been archived by the owner. It is now read-only.
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
114 lines (88 sloc) 3.07 KB
import csv
import json
import parity
from collections import namedtuple
def find_all_paths(graph, start, end, path=[]):
path = path + [start]
if start == end:
return [path]
if not graph.has_key(start):
return []
paths = []
for node in graph[start]:
if node not in path:
newpaths = find_all_paths(graph, node, end, path)
for newpath in newpaths:
return paths
def find_cycle(graph):
for startnode in graph:
for endnode in graph:
newpaths = find_all_paths(graph, startnode, endnode)
for path in newpaths:
if (len(path)==len(graph)):
if path[0] in graph[path[len(graph)-1]]:
return path
return cycles
def pairs(lst):
i = iter(lst)
first = prev =
for item in i:
yield prev, item
prev = item
yield item, first
def analyze():
Game = namedtuple('Game', 'day home_team home_score away_team away_score')
cops = []
for year in range(1869, 2011):
results = {}
lookup = {}
score_file = "data/scores/scores_{0}.csv".format(year)
conference_file = "data/teams/teams_{0}.json".format(year)
reader = csv.reader(open(score_file))
except IOError:
for row in reader:
game = Game(*row)
if game.home_team not in results:
results[game.home_team] = set()
if game.away_team not in results:
results[game.away_team] = set()
lookup[(game.away_team, game.home_team)] = game
lookup[(game.home_team, game.away_team)] = game
if int(game.home_score) > int(game.away_score):
elif int(game.home_score) < int(game.away_score):
data = json.load(open(conference_file))
for conference in data["conferences"]:
graph = {}
queue = []
seen = set()
for team in conference["teams"]:
if team in results:
graph[team] = results[team]
cop = find_cycle(graph)
if not cop:
circle = []
for winner, loser in pairs(cop):
game = lookup[(winner, loser)]
scores = [int(game.away_score), int(game.home_score)]
"winner": winner,
"loser": loser,
"winning_score": max(scores),
"losing_score": min(scores),
"conference": conference["name"],
"year": year,
"teams": circle,
return cops
if __name__ == "__main__":
You can’t perform that action at this time.