Skip to content

Instantly share code, notes, and snippets.

@GrantTrebbin
Created May 18, 2016 09:46
Show Gist options
  • Save GrantTrebbin/6c7e68d178bcaa7c8ff694e6c250278b to your computer and use it in GitHub Desktop.
Save GrantTrebbin/6c7e68d178bcaa7c8ff694e6c250278b to your computer and use it in GitHub Desktop.
import itertools as itt
from collections import deque
# Each element of a sequence can have these values
possible_elements = [0, 1, 2, 3, 4, 5, 6, 7]
element_count = len(possible_elements)
# A sequence contains this many elements
sequence_length = 4
# An iterator for every possible tuple of length "sequence_length"
# with elements selected from "possible_elements"
card_iterator = itt.product(possible_elements, repeat=sequence_length)
# valid cards
card_set = set()
# test each card
for card in card_iterator:
# create a set containing all the orientations of a card
# Basically, rotate the sequence and add it to the set. Do this
# until each element has a turn appearing at the start of the sequence
orientation = deque(card)
orientations = set()
for i in range(0, sequence_length):
orientation.rotate(1)
orientations.add(tuple(orientation))
# If the number of orientation in the set at this point is not
# equal to "sequence_length" there are duplicate orientations that can
# be confused. These cards are not processed.
if len(orientations) == sequence_length:
# Test if any of the orientations are in the valid card set. This
# can be done by finding the intersection of valid cards and all
# the orientations of a single card. If the intersection is a null
# the original card can be added to the valid card set.
setint = orientations.intersection(card_set)
if len(setint) == 0:
card_set.add(tuple(orientation))
# print results
for card in card_set:
print(card)
message = ''.join(['number of cards = ', str(len(card_set))])
print(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment