Skip to content

Instantly share code, notes, and snippets.

@jerinisready
Created April 20, 2021 08:21
Show Gist options
  • Save jerinisready/0beedf891f485f713519c38da7dcebaa to your computer and use it in GitHub Desktop.
Save jerinisready/0beedf891f485f713519c38da7dcebaa to your computer and use it in GitHub Desktop.
Here is how to schedule games between different teams. This scripts considers each team into "Grade A" or "Grade B". Games between "Group A" teams will have "event_weightage_score" of 100, Games between "Group B" teams will have "event_weightage_score" of 60, Games between teams from "Group A" and "Group B" will have "event_weightage_score" of 8…
import itertools
from datetime import date, timedelta
class Team:
grade = None
name = None
def __init__(self, name, grade):
self.name = name
self.grade = grade
class Game:
team_01: Team = None
team_02: Team = None
def __init__(self, team_01: Team, team_02: Team):
self.team_01 = team_01
self.team_02 = team_02
@property
def score(self):
if self.team_01.grade == self.team_02.grade:
if self.team_01.grade == 'A':
return 100
return 60
return 80
def book_on_best_date(self, event_dates):
"""
Since everything is sorted, we do not have any headache now
"""
for event in event_dates: # assuming event is sorted in priority order
if event.is_booked:
continue
event.is_booked = True
event.game = self
return event
class EventDate:
date = None
is_booked = False
game = None
def __init__(self, date):
self.date = date
@property
def is_weekend(self):
return self.date.strftime('%A') in ['Saturday', 'Sunday']
def sort_match_events(start_date, number_of_matches):
"""
this will generate a sorted list of EventDate objects with a date. One Game can schedule in each date.
these EventDates will be sorted with priority, (high priority if weekend.)
Logic:
is_weekend is boolean; which is equal to 0 or 1; so reversing the whole sorting because we want is_weekend = True
first in list.
"""
match_dates = []
# get all dates
for i in range(0, number_of_matches + 1):
dt = start_date + timedelta(days=i)
match_dates.append(EventDate(date=dt))
match_dates.sort(key=lambda ed: ed.is_weekend, reverse=True) # high priority dates first ; weekends
return match_dates
def get_team_combinations(team_list):
"""
get all combinations of games.
"""
team_combinations = [a for a in itertools.combinations(team_list, 2)]
return team_combinations, len(team_combinations)
def sort_games(team_combinations):
"""
Get sorted list of Game() object with score as priorty.
each game will have two teams.
Logic:
score is int; which is equal to 100, 80, or 60; so reversing the whole sorting because we want high score
first in list.
"""
games = []
for team_1, team_2 in team_combinations:
games.append(Game(team_1, team_2))
games.sort(key=lambda game: game.score, reverse=True) # highly scored first
return games
def display_schedule(events):
events = sorted(events, key=lambda event: event.date)
index = 1
for e in events:
print(
f"Match {f'{index}'.zfill(2)}",
f"{e.game.team_01.name} vs {e.game.team_02.name},",
f"{e.game.score},",
f"{e.date.strftime('%a, %d %h %Y')}",
sep="\t"
)
index += 1
def main():
start_date = date.today() + timedelta(days=1) # starting from tomorrow.
# data
teams = [
Team("India", grade="A"),
Team("Aus", grade="A"),
Team("Eng", grade="A"),
Team("Zim", grade="B"),
Team("Ban", grade="B"),
Team("Afg", grade="B"),
]
team_combinations, number_of_matches = get_team_combinations(teams)
match_events = sort_match_events(start_date, number_of_matches)
games = sort_games(team_combinations)
"""
Since everything is sorted, we do not have any headache now
we have games with high priority, is first in list
we have event dates with high priority, is first in list
so just assign first game to first date, second date to second in list.
"""
events = []
for game in games: # high priority games are booked first.
event = game.book_on_best_date(match_events)
events.append(event)
display_schedule(events)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment