Skip to content

Instantly share code, notes, and snippets.

@efischer19
Last active September 4, 2017 18:22
Show Gist options
  • Select an option

  • Save efischer19/1d8577943b2d99dcf4e92b48b3dfc0c3 to your computer and use it in GitHub Desktop.

Select an option

Save efischer19/1d8577943b2d99dcf4e92b48b3dfc0c3 to your computer and use it in GitHub Desktop.
Generating fantasy football schedules for the year
"""Output after running:
eric@oudebier:~/Desktop$ python ffl_schedule.py
Rando pairings: [('Zim', 'Td'), ('Kenny', 'Fish'), ('Jason', 'Steve'), ('Kyle', 'Brett'), ('Zane', 'Collin')]
In-division week 1: [('Td', 'Zim'), ('Jason', 'Zane'), ('Kenny', 'Kyle'), ('Brett', 'Collin'), ('Fish', 'Steve')]
In-division week 2: [('Fish', 'Kenny'), ('Kyle', 'Zane'), ('Jason', 'Zim'), ('Collin', 'Td'), ('Brett', 'Steve')]
In-division week 3: [('Jason', 'Steve'), ('Kyle', 'Zim'), ('Kenny', 'Zane'), ('Collin', 'Fish'), ('Brett', 'Td')]
In-division week 4: [('Brett', 'Kyle'), ('Jason', 'Kenny'), ('Zane', 'Zim'), ('Fish', 'Td'), ('Collin', 'Steve')]
In-division week 5: [('Collin', 'Zane'), ('Kenny', 'Zim'), ('Jason', 'Kyle'), ('Steve', 'Td'), ('Brett', 'Fish')]
Out-of-division week 1: [('Kenny', 'Td'), ('Jason', 'Fish'), ('Kyle', 'Steve'), ('Zane', 'Brett'), ('Zim', 'Collin')]
Out-of-division week 2: [('Jason', 'Td'), ('Kyle', 'Fish'), ('Zane', 'Steve'), ('Zim', 'Brett'), ('Kenny', 'Collin')]
Out-of-division week 3: [('Kyle', 'Td'), ('Zane', 'Fish'), ('Zim', 'Steve'), ('Kenny', 'Brett'), ('Jason', 'Collin')]
Out-of-division week 4: [('Zane', 'Td'), ('Zim', 'Fish'), ('Kenny', 'Steve'), ('Jason', 'Brett'), ('Kyle', 'Collin')]
First in-division weekly order: [3, 1, 4, 5, 2]
Out-of-division weekly order: [3, 1, 4, 2]
Second in-division weekly order: [1, 5, 2, 3, 4]
"""
from copy import deepcopy
import random
# 2017 schedule generated using 2016 results
# draft order, with last year's final standings(http://games.espn.com/ffl/tools/finalstandings?leagueId=214436) in parens
# Kyle(5), Kenny(10), Fish(9), Zim(7), Jason(3), Steve(1), Collin(8), Td(4), Brett(2), Zane(6)
# now, smash those digits together, we have a seed for our RNG
rng_seed = 51097318426
random.seed(rng_seed)
the_guys = [ # already divided by division
[
"Zane",
"Kyle",
"Zim",
"Jason",
"Kenny",
],[
"Fish",
"Td",
"Brett",
"Steve",
"Collin",
]
]
# First/last 5 weeks = 4 in-division matchups + 1 rando pairing
# Middle 4 weeks = 4 out-of-division (non rando) matchups
# A 'week' is a list of 5 tuples, each containing the 2 teams in a given matchup
samples = (
random.sample(the_guys[0], 5),
random.sample(the_guys[1], 5)
)
rando_pairings = zip(samples[0], samples[1])
print "Rando pairings: {}".format(rando_pairings)
weekly_pairs = []
for i, randos in enumerate(rando_pairings):
potential_teams = deepcopy(the_guys)
potential_teams[0].remove(randos[0])
potential_teams[1].remove(randos[1])
weekly_pairs.append([tuple(sorted(randos))])
"""
print "\n\n\n##### DEBUG #####\nRemoved: {}\nSelecting from: {}\n##### DEBUG #####".format(
weekly_pairs[i][0],
potential_teams
)
"""
for ii in range(2):
tracker = True
while tracker:
tracker = False
new_pair = random.sample(potential_teams[ii], 2)
other_pair = deepcopy(potential_teams[ii])
for guy in new_pair:
other_pair.remove(guy)
new_pair = tuple(sorted(new_pair))
other_pair = tuple(sorted(other_pair))
for week in weekly_pairs:
for pair in week:
if new_pair == pair or other_pair == pair:
tracker = True
weekly_pairs[i].append(new_pair),
weekly_pairs[i].append(other_pair)
print "In-division week {}: {}".format(i+1, weekly_pairs[i])
def rotate(l, n):
return l[n:] + l[:n]
for i in range(4):
rotated_list = rotate(samples[0], i+1)
pairings = zip(rotated_list, samples[1])
print "Out-of-division week {}: {}".format(i+1, pairings)
# Now, the ordering of the weeks
print "First in-division weekly order: {}".format(random.sample([1, 2, 3, 4, 5], 5))
print "Out-of-division weekly order: {}".format(random.sample([1, 2, 3, 4], 4))
print "Second in-division weekly order: {}".format(random.sample([1, 2, 3, 4, 5], 5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment