Skip to content

Instantly share code, notes, and snippets.

Created May 18, 2016 09:46
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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):
# 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:
# print results
for card in card_set:
message = ''.join(['number of cards = ', str(len(card_set))])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment