Created
May 15, 2018 19:16
-
-
Save robertmaxwilliams/cd99bb82d247bcfdf173ebd9e6cb0803 to your computer and use it in GitHub Desktop.
https://en.wikipedia.org/wiki/Karnaugh_map I forgot how this code works, sorry.
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
from itertools import combinations | |
from copy import copy | |
import pdb | |
def grabpoints(size, mod, pointnums): | |
""" returns nums under size | |
that are divisible by mod, | |
duplicated on pointnums | |
""" | |
box = {n for n in range(size) if n%mod == 0} | |
for num in pointnums: | |
temp = copy(box) | |
for n in temp: | |
box.add(n+num) | |
return box | |
def makegroups(points, spacenums): | |
""" yields a set created by | |
duplicating a point in points | |
over spacenums | |
""" | |
for point in points: | |
box = {point} | |
for x in spacenums: | |
temp = copy(box) | |
for n in temp: | |
box.add(n + x) | |
yield box | |
def gengroups(size, mod, pointnums, spacenums): | |
points = grabpoints(size, mod, pointnums) | |
for group in makegroups(points, spacenums): | |
yield group | |
def setsplit(sett, bottomsize): | |
""" split set into 2 sets, | |
using bottomsize. | |
yields all possible results | |
as (set, set) tuples | |
Also, largest element is always in | |
the second set. | |
Small detail. | |
""" | |
maxx = max(sett) | |
sett.remove(maxx) | |
for bottom in combinations(sett, bottomsize-1): | |
yield (sett - set(bottom), set(bottom) | {maxx,}) | |
def nface_mcube(n, m): | |
""" nface: 1face is edge, 2face is face, | |
3face is cube | |
mcube: hypercube of m dimensions | |
""" | |
size = 2**m | |
for top in range(n, m+1): | |
for pointnums,spacenums in setsplit({2**x for x in range(top)}, n): | |
for face in gengroups(size, 2**top, pointnums, spacenums): | |
yield face |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment