> set(, perm ) for perm in permutations( range( 9 ) ) ] ) Pass the list to set to get make sure each tuple only exists once. To allow in the uneven case for groups of three you just need to change round_robin_odd(d, n): def round_robin_odd(d, n): Update to account for the updated question: Here's my first implementation, which is less readable IMO: def round_robin(n): What haunts me about this version is the amount of code duplication, but I couldn't find a way to improve while keeping it as readable. This special step is that I swap the two leftmost elements (which are the first and last elements of the deque) before each rotation - this means the 0 stays all the time upper left.Ĭase even elements: round 1 round 2 # pairs are those numbers that sit which shows me how important it is to check edge cases when implementing an algorithm.) (I missed the first time cause I only checked the uneven case. In case of even elements an additional step is required. To make it clear:Ĭase uneven elements: round 1 round 2 # pairs are those numbers that sit One can imagine this as folding the deque in the middle back on itself. Then it rotates and builds consecutive pairs taking from both ends towards the middle. It puts the objects(ints here) in the deque. I'd like to share a different implementation of round-robin scheduling that makes use of the deque-data structure from the Standard Library: from collections import deque Update 2: Theodros' solution produces exactly the right result without the inelegant futzing about I describe above. However, I wonder if there isn't already an algorithm or adjustment to round-robin that does this in a better way. Then, after the pairing, find the person paired with the invisible person and randomly place them into an existing group to form a team of three. My current thinking is to (1) make an even number of people by adding in an invisible person. Thus, if I have an odd number of people, I need to create a group of three to include an odd person for each exercise. I have an additional constraint which is that what I'm doing is grouping people for pair-programming exercises. Update: Gareth Rees is correct - I was completely unaware that I was trying to solve the round-robin problem. I'll bet there's an additional itertool that would solve this but I'm not expert enough to know what it is. How do I further filter it so that I only ever see (8,4) once (throughout all of the filtered permutations), and (8,5) only once, and (0,1) only once, and (4,7) only once, etc.?īasically I want the permutations such that each two-element pairing happens only once. Generates all possible two-paired permutations here's a small subset of the output. So for example: import itertoolsįor perm in itertools.permutations(range(9)): I need to generate all possible pairings, but with the constraint that a particular pairing only occurs once in the results.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |