Created
January 14, 2019 17:27
-
-
Save pberkes/ab45d5dd49c4914601f4e5a1e49c7ce1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_sets2(cards): | |
ndims, ncards = cards.shape | |
all_features = set([0, 1, 2]) | |
# solutions contain the indices of the cards forming sets | |
solutions = [] | |
# iterate over all pairs | |
for idx1, idx2 in itertools.combinations(range(ncards - 1), 2): | |
c1, c2 = cards[:, idx1], cards[:, idx2] | |
# compute card that would complete the set | |
missing = numpy.empty((ndims,), dtype='i') | |
for d in range(ndims): | |
if c1[d] == c2[d]: | |
# same feature on this dimension ->; missing card also has same | |
missing[d] = c1[d] | |
else: | |
# different features -> find third missing feature | |
missing[d] = list(all_features - set([c1[d], c2[d]]))[0] | |
# look for missing card in the cards array | |
where_idx = numpy.flatnonzero(numpy.all(cards[:, idx2 + 1:].T == missing, | |
axis=1)) | |
# append to solutions if found | |
if len(where_idx) > 0: | |
solutions.append((idx1, idx2, where_idx[0] + idx2 + 1)) | |
return solutions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment