Created
October 28, 2013 07:40
-
-
Save Makistos/7192777 to your computer and use it in GitHub Desktop.
A round-robin algorithm implementation written in Python. #round-robin #scheduling #algorithm #python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
div1 = ["Lions", "Tigers", "Jaguars", "Cougars"] | |
div2 = ["Whales", "Sharks", "Piranhas", "Alligators"] | |
div3 = ["Cubs", "Kittens", "Puppies", "Calfs"] | |
def create_schedule(list): | |
""" Create a schedule for the teams in the list and return it""" | |
s = [] | |
if len(list) % 2 == 1: list = list + ["BYE"] | |
for i in range(len(list)-1): | |
mid = len(list) / 2 | |
l1 = list[:mid] | |
l2 = list[mid:] | |
l2.reverse() | |
# Switch sides after each round | |
if(i % 2 == 1): | |
s = s + [ zip(l1, l2) ] | |
else: | |
s = s + [ zip(l2, l1) ] | |
list.insert(1, list.pop()) | |
return s | |
def main(): | |
for round in create_schedule(div1): | |
for match in round: | |
print match[0] + " - " + match[1] | |
for round in create_schedule(div2): | |
for match in round: | |
print match[0] + " - " + match[1] | |
for round in create_schedule(div3): | |
for match in round: | |
print match[0] + " - " + match[1] | |
for round in create_schedule(div1+div2+div3): | |
for match in round: | |
print match[0] + " - " + match[1] | |
if __name__ == "__main__": | |
main() |
Un mes entero buscando la forma de desarrollar un algoritmo que permita realizar emparejamientos y no encontraba, hasta que me dí cuenta que ya existían.
Gracias por el método
from itertools import cycle
A = [[1,2,3],[4,5,6],[7]]
B = [[8],[9,10,11],[12,13]]
i = len(A)
j = 0
C = [ ] #for results
list_num = cycle(k for k in range ( i ))
for x in list_num:
j += 1
if j == i*3:
break
if A[x]:
C.append(A[x].pop(0))
if B[x]:
C.append(B[x].pop(0))
Output=========>[1, 8, 4, 9, 7, 12, 2, 5, 10, 13, 3, 6, 11]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey there,
I love this code, but noticed one small piece that could improve it. I know you posted this 5+ years ago, so it's cool if you don't care about it anymore.
When working with larger datasets and using the "list.insert(1, list.pop())" line, you'll run into repeating combinations.
With set:
A
B
C
D
and splitting it into two groups to zip of
A - C
B - D
and then popping D, moving it between A and B, you get
A
D
B
C
and sets
A - B
D - C
which is unique to the first time through. BUT, that only works for sets of 4 (I think). And granted, your problem only had 4 items, so your code is totally correct. I was using a set of 12 (1-12 for instance), and wanted to do a round robin of 11 games each. Below is what I was seeing with "list.insert(1, list.pop())". Notice player 1 has a new partner every time, but everyone else repeats their partner until he's up to play # 1. I'll highlight set (2,8)
[(1, 7), (2, 8), (3, 9), (4, 10), (5, 11), (6, 12)]
[(6, 1), (7, 12), (8, 2), (9, 3), (10, 4), (11, 5)]
[(1, 5), (11, 6), (12, 7), (2, 8), (3, 9), (4, 10)]
[(4, 1), (5, 10), (6, 11), (7, 12), (8, 2), (9, 3)]
[(1, 3), (9, 4), (10, 5), (11, 6), (12, 7), (2, 8)]
[(2, 1), (3, 8), (4, 9), (5, 10), (6, 11), (7, 12)]
[(1, 12), (7, 2), (8, 3), (9, 4), (10, 5), (11, 6)]
[(11, 1), (12, 6), (2, 7), (3, 8), (4, 9), (5, 10)]
[(1, 10), (5, 11), (6, 12), (7, 2), (8, 3), (9, 4)]
[(9, 1), (10, 4), (11, 5), (12, 6), (2, 7), (3, 8)]
[(1, 8), (3, 9), (4, 10), (5, 11), (6, 12), (7, 2)]
All other players do this, just look for a pair and then the diagonal-right pair will be the same.
I had a great suggestion to use "list.insert(mid, list.pop())" instead, but that doesn't work either (makes it unique until you exhuast your 'half'). Sorry I couldn't contribute a ton.