Skip to content

Instantly share code, notes, and snippets.

@cammckinnon
Created January 15, 2013 04:20
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 cammckinnon/4536013 to your computer and use it in GitHub Desktop.
Save cammckinnon/4536013 to your computer and use it in GitHub Desktop.
from itertools import combinations
# in is the number of dimensions of the cube (3 for a 3d cube)
def generate_vertices(n):
for number_of_ones in xrange(0, n + 1):
for location_of_ones in combinations(xrange(0,n), number_of_ones):
location_of_ones = set(location_of_ones)
yield [1 if i in location_of_ones else 0 for i in xrange(0, n) ]
for vertex in generate_vertices(3):
print vertex
# result:
# [0, 0, 0]
# [1, 0, 0]
# [0, 1, 0]
# [0, 0, 1]
# [1, 1, 0]
# [1, 0, 1]
# [0, 1, 1]
# [1, 1, 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment