Skip to content

Instantly share code, notes, and snippets.

@insipid
Created June 15, 2010 11:08
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 insipid/438980 to your computer and use it in GitHub Desktop.
Save insipid/438980 to your computer and use it in GitHub Desktop.
class Colouring(object):
def __init__(self, colouring):
self.colouring = colouring
def __eq__(self, other):
# Two colourings are "equal" (read: "equivalent") if one is a
# rotational-shift of the other. (I'm sure there are smarter
# ways to do this (this may not even be *right*), but I was in
# a hurry, and it seems to work.)
current = self.colouring
for i in range(0, len(current)):
# There's *definitely* cleverer ways of rotating an array
# in-place, I'm sure. But I was lazy.
temp = current[1:]
temp.append(current[0])
current = temp
if (current == other.colouring): return True
return False
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
return "<%s>" % (self.colouring.__repr__()[1:-1])
class NoDupeList(object):
# Can't use a set, because objects that are "equal" have to hash to
# the same value; and I couldn't think of clever way to do that, quickly.
def __init__(self, iterable):
self.list = []
for x in iterable:
add = True
for y in self.list:
if (x == y): add = False
if add: self.list.append(x)
def __repr__(self):
return self.list.__repr__()
def __len__(self):
return self.list.__len__()
def __iter__(self):
return self.list.__iter__()
# Some previous lame attempts at "testing"
#print Colouring(["a", "b", "a", "b"]) == Colouring(["a", "b", "a", "b"])
#print Colouring(["a", "b", "a", "b"]) == Colouring(["a", "b", "a", "c"])
#print Colouring(["a", "b", "a", "b"]) == Colouring(["b", "a", "b", "a"])
#print Colouring(["a", "b"]) == Colouring(["b", "a"])
#print NoDupeList([Colouring(["a", "b"]), Colouring(["a", "b"])])
#print NoDupeList([Colouring(["a", "b"]), Colouring(["b", "a"])])
colours = ["a", "b"]
distinct_colourings = NoDupeList([Colouring([w, x, y, z]) for w in colours for x in colours for y in colours for z in colours])
print "\nUsing %s elements: %s" % (len(colours), colours)
print "Creates %s distinct colourings:" % (len(distinct_colourings))
for x in distinct_colourings: print x
colours = ["a", "b", "c"]
distinct_colourings = NoDupeList([Colouring([w, x, y, z]) for w in colours for x in colours for y in colours for z in colours])
print "\nUsing %s elements: %s" % (len(colours), colours)
print "Creates %s distinct colourings:" % (len(distinct_colourings))
for x in distinct_colourings: print x
colours = ["a", "b", "c", "d"]
distinct_colourings = NoDupeList([Colouring([w, x, y, z]) for w in colours for x in colours for y in colours for z in colours])
print "\nUsing %s elements: %s" % (len(colours), colours)
print "Creates %s distinct colourings:" % (len(distinct_colourings))
for x in distinct_colourings: print x
Using 2 elements: ['a', 'b']
Creates 6 distinct colourings:
<'a', 'a', 'a', 'a'>
<'a', 'a', 'a', 'b'>
<'a', 'a', 'b', 'b'>
<'a', 'b', 'a', 'b'>
<'a', 'b', 'b', 'b'>
<'b', 'b', 'b', 'b'>
Using 3 elements: ['a', 'b', 'c']
Creates 24 distinct colourings:
<'a', 'a', 'a', 'a'>
<'a', 'a', 'a', 'b'>
<'a', 'a', 'a', 'c'>
<'a', 'a', 'b', 'b'>
<'a', 'a', 'b', 'c'>
<'a', 'a', 'c', 'b'>
<'a', 'a', 'c', 'c'>
<'a', 'b', 'a', 'b'>
<'a', 'b', 'a', 'c'>
<'a', 'b', 'b', 'b'>
<'a', 'b', 'b', 'c'>
<'a', 'b', 'c', 'b'>
<'a', 'b', 'c', 'c'>
<'a', 'c', 'a', 'c'>
<'a', 'c', 'b', 'b'>
<'a', 'c', 'b', 'c'>
<'a', 'c', 'c', 'b'>
<'a', 'c', 'c', 'c'>
<'b', 'b', 'b', 'b'>
<'b', 'b', 'b', 'c'>
<'b', 'b', 'c', 'c'>
<'b', 'c', 'b', 'c'>
<'b', 'c', 'c', 'c'>
<'c', 'c', 'c', 'c'>
Using 4 elements: ['a', 'b', 'c', 'd']
Creates 70 distinct colourings:
<'a', 'a', 'a', 'a'>
<'a', 'a', 'a', 'b'>
<'a', 'a', 'a', 'c'>
<'a', 'a', 'a', 'd'>
<'a', 'a', 'b', 'b'>
<'a', 'a', 'b', 'c'>
<'a', 'a', 'b', 'd'>
<'a', 'a', 'c', 'b'>
<'a', 'a', 'c', 'c'>
<'a', 'a', 'c', 'd'>
<'a', 'a', 'd', 'b'>
<'a', 'a', 'd', 'c'>
<'a', 'a', 'd', 'd'>
<'a', 'b', 'a', 'b'>
<'a', 'b', 'a', 'c'>
<'a', 'b', 'a', 'd'>
<'a', 'b', 'b', 'b'>
<'a', 'b', 'b', 'c'>
<'a', 'b', 'b', 'd'>
<'a', 'b', 'c', 'b'>
<'a', 'b', 'c', 'c'>
<'a', 'b', 'c', 'd'>
<'a', 'b', 'd', 'b'>
<'a', 'b', 'd', 'c'>
<'a', 'b', 'd', 'd'>
<'a', 'c', 'a', 'c'>
<'a', 'c', 'a', 'd'>
<'a', 'c', 'b', 'b'>
<'a', 'c', 'b', 'c'>
<'a', 'c', 'b', 'd'>
<'a', 'c', 'c', 'b'>
<'a', 'c', 'c', 'c'>
<'a', 'c', 'c', 'd'>
<'a', 'c', 'd', 'b'>
<'a', 'c', 'd', 'c'>
<'a', 'c', 'd', 'd'>
<'a', 'd', 'a', 'd'>
<'a', 'd', 'b', 'b'>
<'a', 'd', 'b', 'c'>
<'a', 'd', 'b', 'd'>
<'a', 'd', 'c', 'b'>
<'a', 'd', 'c', 'c'>
<'a', 'd', 'c', 'd'>
<'a', 'd', 'd', 'b'>
<'a', 'd', 'd', 'c'>
<'a', 'd', 'd', 'd'>
<'b', 'b', 'b', 'b'>
<'b', 'b', 'b', 'c'>
<'b', 'b', 'b', 'd'>
<'b', 'b', 'c', 'c'>
<'b', 'b', 'c', 'd'>
<'b', 'b', 'd', 'c'>
<'b', 'b', 'd', 'd'>
<'b', 'c', 'b', 'c'>
<'b', 'c', 'b', 'd'>
<'b', 'c', 'c', 'c'>
<'b', 'c', 'c', 'd'>
<'b', 'c', 'd', 'c'>
<'b', 'c', 'd', 'd'>
<'b', 'd', 'b', 'd'>
<'b', 'd', 'c', 'c'>
<'b', 'd', 'c', 'd'>
<'b', 'd', 'd', 'c'>
<'b', 'd', 'd', 'd'>
<'c', 'c', 'c', 'c'>
<'c', 'c', 'c', 'd'>
<'c', 'c', 'd', 'd'>
<'c', 'd', 'c', 'd'>
<'c', 'd', 'd', 'd'>
<'d', 'd', 'd', 'd'>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment