Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. import math
  2.  
  3.  
  4. def printtournament(tournament):
  5. for gameround in grouped(tournament, fields):
  6. for game in sorted(gameround, key=lambda x: x[0] if isinstance(x[0], int) else 0):
  7. if game[0] != 'E':
  8. print(game, end='')
  9. print()
  10. print()
  11.  
  12.  
  13. def printtournamenthtml(tournament):
  14. print('<table style="border: solid black 2px">')
  15. for gameround in grouped(tournament, fields):
  16. print('<tr style="border: solid black 2px">')
  17. for game in sorted(gameround, key=lambda x: x[0] if isinstance(x[0], int) else 0):
  18. if game[0] != 'E':
  19. print('<td style="border: solid black 2px">', game, '</td>', end='')
  20. print('</tr>')
  21. print()
  22. print('</table>')
  23.  
  24.  
  25. def grouped(iterable, n):
  26. return zip(*[iter(iterable)]*n)
  27.  
  28.  
  29. def allgames(teamcount):
  30. games = []
  31. for t in range(teamcount):
  32. for u in range(teamcount):
  33. if u is not t:
  34. game = (u, t)
  35. game = sorted(game, reverse=False)
  36. if game not in games:
  37. games.append(game)
  38. return games
  39.  
  40.  
  41. def evaluate(tournament, fieldcount, gamesperteam, teamcount):
  42. current_games_per_team = {}
  43.  
  44. for i in range(teamcount):
  45. current_games_per_team[i] = 0
  46. current_games_per_team['E'] = 0
  47. gamehasbeenplayed = []
  48. current_games_per_team_round = {}
  49. for i in range(teamcount):
  50. current_games_per_team_round[i] = 0
  51. current_games_per_team_round['E'] = 0
  52.  
  53. for gameround in grouped(tournament, fieldcount):
  54. teamhasplayed = []
  55.  
  56. for game in (gameround):
  57. for t in game:
  58. if t in teamhasplayed and t is not 'E':
  59. return False
  60. else:
  61. teamhasplayed.append(t)
  62. if game in gamehasbeenplayed and game[0] is not 'E' and game[1] is not 'E':
  63. return False
  64. else:
  65. gamehasbeenplayed.append(game)
  66.  
  67. current_games_per_team[int(game[0]) if isinstance(game[0], int) else 'E'] += 1
  68. current_games_per_team[int(game[1]) if isinstance(game[0], int) else 'E'] += 1
  69. current_games_per_team_round[int(game[0]) if isinstance(game[0], int) else 'E'] += 1
  70. current_games_per_team_round[int(game[1]) if isinstance(game[0], int) else 'E'] += 1
  71.  
  72. for k, c in current_games_per_team.items():
  73. if c > gamesperteam and k is not 'E':
  74. return False
  75.  
  76. for k1, c in current_games_per_team_round.items():
  77. for k2, d in current_games_per_team_round.items():
  78. if abs(c - d) > 1 and k1 is not 'E' and k2 is not 'E':
  79. return False
  80. return True
  81.  
  82. def extend(gamecount, games, teams, fields, gamesperteam):
  83. if gamecount < games:
  84. for game in all_possible_games:
  85. tournament[gamecount] = game
  86. for i in range(fieldrounds):
  87. if i > gamecount:
  88. tournament[i] = ['E', 'E']
  89. if evaluate(tournament, fields, gamesperteam, teams):
  90. extend(gamecount+1, games, teams, fields, gamesperteam)
  91. else:
  92. printtournament(tournament)
  93.  
  94.  
  95. teams = 30
  96. fields = 4
  97. gamesperteam = 2
  98. games = int(teams/2*gamesperteam)
  99. fieldrounds = int(math.ceil(games/fields)*fields)
  100. all_possible_games = allgames(teams)
  101. tournament = []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement