Skip to content

Instantly share code, notes, and snippets.

@jtushman
Last active December 25, 2015 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtushman/6927959 to your computer and use it in GitHub Desktop.
Save jtushman/6927959 to your computer and use it in GitHub Desktop.
Randomize your team seating. Ensuring that you are sitting next to two new people you haven't sat next to before. Only works with teams greater than 4
import random
def all_perms(elements):
if len(elements) <=1:
yield elements
else:
for perm in all_perms(elements[1:]):
for i in range(len(elements)):
#nb elements[0:1] works in both string and list contexts
yield perm[:i] + elements[0:1] + perm[i:]
def find_position(key,lizt):
return [i for i,x in enumerate(lizt) if x == key][0]
def new_neighbors(some_list):
new_neighbor_list = some_list[:]
list_size = len(some_list)
for new_neighbor_list in all_perms(some_list):
print new_neighbor_list
too_many_neighbors = False
for i,team_member in enumerate(new_neighbor_list):
#find position in inital list
position_in_original_list = find_position(team_member,some_list)
original_neighbors = []
original_neighbors.append(some_list[(position_in_original_list+1) % list_size])
original_neighbors.append(some_list[(position_in_original_list-1) % list_size])
new_neighbors = []
new_neighbors.append(new_neighbor_list[(i+1) % list_size])
new_neighbors.append(new_neighbor_list[(i-1) % list_size])
delta = len(set(new_neighbors) - set(original_neighbors))
#print "for {} comparing: {} with {} = {}".format(team_member,original_neighbors,new_neighbors,delta)
if not delta == 2:
too_many_neighbors = True
break
if too_many_neighbors == False:
return new_neighbor_list
else:
print "No Matches"
return []
team = ['JT','FS','MC','MA','FD']
new_seating = new_neighbors(team)
print new_seating
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment