Skip to content

Instantly share code, notes, and snippets.

@aambrioso1
Last active July 16, 2019 18:16
Show Gist options
  • Save aambrioso1/7f92ab44911c4e9eb7b6fd0dc7cfa73e to your computer and use it in GitHub Desktop.
Save aambrioso1/7f92ab44911c4e9eb7b6fd0dc7cfa73e to your computer and use it in GitHub Desktop.
GeneralizedColorings.py
"""
Vince list rewritten to use itertools product function. This calculates the cartesian product of input iterables. It operates like nested for loops.
See:
https://docs.python.org/3/library/itertools.html#itertools.product
for detailed documentation.
My goal is too generalize the count to n colors.
"""
import itertools as it
monolist = []
duolist = []
triolist = []
quadlist = []
tuples = it.product('abcd', repeat=4)
all = []
for i in tuples:
newlist = list(i)
all.append(newlist)
if len(set(newlist)) == 1:
monolist.append(newlist)
if len(set(newlist)) == 2:
duolist.append(newlist)
if len(set(newlist)) == 3:
triolist.append(newlist)
if len(set(newlist)) == 4:
quadlist.append(newlist)
"""
if len(set(i)) == 5:
quadlist.append(i)
if len(set(i)) == 6:
quadlist.append(i)
"""
# Compute the number of colorings by finding the length of each list.
mono = len(monolist)
duo = len(duolist)
trio = len(triolist)
quad = len(quadlist)
print('\n\n************************************')
print(f"Mono - use only one color: {mono}")
print(f"Duo: - use only two colors: {duo}")
print(f"Trio: - use only three colors: {trio}")
print(f"Quad - use all four colors: {quad}")
print('************************************')
print(f"Total number of colorings: {len(all)}")
print(f"Sum of mono, duo, trio, quad is {mono + duo + trio + quad}")
print('************************************')
print('\n**********')
print('Monos')
print('**********')
print(monolist)
print('\n**********')
print('Duos')
print('**********')
print(duolist)
print('\n**********')
print('Trios')
print('**********')
print(triolist)
print('\n**********')
print('Quads')
print('**********')
print(quadlist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment