1 Replies - 454 Views - Last Post: 06 February 2012 - 07:32 AM Rate Topic: -----

Topic Sponsor:

#1 ekke85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-February 12

how to calculate sports fixtures

Posted 05 February 2012 - 06:22 AM

Hi, I am kinda new to python and is trying to write a little program to work out sports fixtures but i am a bit stuck. I want to take say 4 players and work out a fixture like this:

round1
player1 vs player3
player2 vs player4

round2
player1 vs player2
player3 vs player4

round3
player1 vs player4
player2 vs player3

So i know how to work out the amount of rounds and amount of matches like this:
teams = open('teamlist.txt', 'r')
teams = teams.read()
teams = teams.strip()
ghostPlayer = False
teamCounter = 0

for line in teams.split("\n"):
  teamCounter += 1

if teamCounter % 2 == 1:
   ghostPlayer = True
   teamCounter += 1
   print "odd"
 else:
   print "even"
 
if ghostPlayer == False:
     print "GhostPlayer = %s, there is %s players" % (ghostPlayer, teamCounter)
else:
     print "There is one GostPlayer(%s) and %s real players" % (gostPlayer, (teamCounter - 1))     
 rounds = teamCounter - 1
 matches = teamCounter / 2



My problem really is that i don't know how to do the actual fixture bit, it would be very much appreciated if someone can help me

Is This A Good Question/Topic? 0
  • +

Replies To: how to calculate sports fixtures

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 443
  • View blog
  • Posts: 792
  • Joined: 08-June 10

Re: how to calculate sports fixtures

Posted 06 February 2012 - 07:32 AM

The itertools module includes the combinations function to assist in building the set of linear combinations for a list:

>>> import itertools
>>> for matchup in itertools.combinations(['team1','team2','team3','team4'], 2):
...     print matchup
... 
('team1', 'team2')
('team1', 'team3')
('team1', 'team4')
('team2', 'team3')
('team2', 'team4')
('team3', 'team4')



From there, you only have to worry about properly putting them into sets.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1