Skip to content

Instantly share code, notes, and snippets.

@mrshoe
Created January 19, 2016 00:09
Show Gist options
  • Save mrshoe/4f37b0f39710381910cb to your computer and use it in GitHub Desktop.
Save mrshoe/4f37b0f39710381910cb to your computer and use it in GitHub Desktop.
LEAGUE_SIZE = 12
NONCONFERENCE_GAMES = 2
class Team:
def __init__(self, number):
self.number = number
self.wins = []
self.losses = []
self.winning_pct = 0.0
self.wins_500plus = 0
self.opps_500plus = 0
self.avg_opp_winpct = 0.0
self.nonconf = False
@property
def games_played(self):
return len(self.wins) + len(self.losses)
@property
def win_count(self):
return len(self.wins)
@property
def loss_count(self):
return len(self.losses)
def played(self, other):
return other.number in ([t.number for t in self.losses] + [t.number for t in self.wins])
def __repr__(self):
return 'Alpha%02d\t%d-%d\t%0.2f\t%d\t%d\t%0.2f' % (self.number, self.win_count, self.loss_count, self.winning_pct, self.wins_500plus, self.opps_500plus, self.avg_opp_winpct)
def __cmp__(self, other):
return self.number.__cmp__(other.number)
def nonconference_team(i):
result = Team(LEAGUE_SIZE+i+1)
result.nonconf = True
result.wins = [result] * 5
result.losses = [result] * 5
result.winning_pct = 0.5
result.wins_500plus = 0
result.avg_opp_winpct = 0.45
return result
def game(team1, team2, gp_target=LEAGUE_SIZE):
if team1.number != team2.number and not team1.played(team2) and team1.games_played < gp_target and team2.games_played < gp_target:
winner, loser = (team1, team2) if team1 < team2 else (team2, team1)
if winner.nonconf is False:
winner.wins.append(loser)
if loser.nonconf is False:
loser.losses.append(winner)
league = [Team(x+1) for x in xrange(LEAGUE_SIZE)]
nonconference_teams = [nonconference_team(i) for i in xrange(NONCONFERENCE_GAMES)]
for i, team in enumerate(league):
gp_target = LEAGUE_SIZE-NONCONFERENCE_GAMES-1
for x in xrange(gp_target):
opp = league[(i+x+NONCONFERENCE_GAMES) % LEAGUE_SIZE]
game(team, opp, gp_target)
for team in league:
for n in nonconference_teams:
game(team, n)
for t in league:
t.winning_pct = float(t.win_count) / t.games_played
for t in league:
t.opps_500plus = len([w for w in (t.wins+t.losses) if w.win_count >= w.loss_count])
t.wins_500plus = len([w for w in t.wins if w.win_count >= w.loss_count])
for t in league:
opp_winpct = [x.winning_pct for x in (t.wins + t.losses)]
t.avg_opp_winpct = sum(opp_winpct) / len(opp_winpct)
for t in sorted(league, key=lambda x: -1*x.winning_pct):
print t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment