public
Created

  • Download Gist
gistfile1.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.