Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- def printtournament(tournament):
- for gameround in grouped(tournament, fields):
- for game in sorted(gameround, key=lambda x: x[0] if isinstance(x[0], int) else 0):
- if game[0] != 'E':
- print(game, end='')
- print()
- print()
- def printtournamenthtml(tournament):
- print('<table style="border: solid black 2px">')
- for gameround in grouped(tournament, fields):
- print('<tr style="border: solid black 2px">')
- for game in sorted(gameround, key=lambda x: x[0] if isinstance(x[0], int) else 0):
- if game[0] != 'E':
- print('<td style="border: solid black 2px">', game, '</td>', end='')
- print('</tr>')
- print()
- print('</table>')
- def grouped(iterable, n):
- return zip(*[iter(iterable)]*n)
- def allgames(teamcount):
- games = []
- for t in range(teamcount):
- for u in range(teamcount):
- if u is not t:
- game = (u, t)
- game = sorted(game, reverse=False)
- if game not in games:
- games.append(game)
- return games
- def evaluate(tournament, fieldcount, gamesperteam, teamcount):
- current_games_per_team = {}
- for i in range(teamcount):
- current_games_per_team[i] = 0
- current_games_per_team['E'] = 0
- gamehasbeenplayed = []
- current_games_per_team_round = {}
- for i in range(teamcount):
- current_games_per_team_round[i] = 0
- current_games_per_team_round['E'] = 0
- for gameround in grouped(tournament, fieldcount):
- teamhasplayed = []
- for game in (gameround):
- for t in game:
- if t in teamhasplayed and t is not 'E':
- return False
- else:
- teamhasplayed.append(t)
- if game in gamehasbeenplayed and game[0] is not 'E' and game[1] is not 'E':
- return False
- else:
- gamehasbeenplayed.append(game)
- current_games_per_team[int(game[0]) if isinstance(game[0], int) else 'E'] += 1
- current_games_per_team[int(game[1]) if isinstance(game[0], int) else 'E'] += 1
- current_games_per_team_round[int(game[0]) if isinstance(game[0], int) else 'E'] += 1
- current_games_per_team_round[int(game[1]) if isinstance(game[0], int) else 'E'] += 1
- for k, c in current_games_per_team.items():
- if c > gamesperteam and k is not 'E':
- return False
- for k1, c in current_games_per_team_round.items():
- for k2, d in current_games_per_team_round.items():
- if abs(c - d) > 1 and k1 is not 'E' and k2 is not 'E':
- return False
- return True
- def extend(gamecount, games, teams, fields, gamesperteam):
- if gamecount < games:
- for game in all_possible_games:
- tournament[gamecount] = game
- for i in range(fieldrounds):
- if i > gamecount:
- tournament[i] = ['E', 'E']
- if evaluate(tournament, fields, gamesperteam, teams):
- extend(gamecount+1, games, teams, fields, gamesperteam)
- else:
- printtournament(tournament)
- teams = 30
- fields = 4
- gamesperteam = 2
- games = int(teams/2*gamesperteam)
- fieldrounds = int(math.ceil(games/fields)*fields)
- all_possible_games = allgames(teams)
- tournament = []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement