Created
December 8, 2017 16:25
-
-
Save mousetail/eb07e30ef725ace91049457f839b7634 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
# python3.5 | |
import itertools | |
def isIn(group, elem): | |
elem=''.join(sorted(set(elem[0]))),''.join(sorted(set(elem[1]))) | |
for s in group: | |
if s[0]==elem[0]: | |
for g in elem[1]: | |
if g not in s[1]: | |
return False | |
return True | |
return False | |
def add(group, elem): | |
elem = ''.join(sorted(set(elem[0]))), ''.join(sorted(elem[1])) | |
for symbol in elem[1]: | |
found = False | |
for s in group: | |
if s[0]==elem[0]: | |
found = True | |
if symbol not in s[1]: | |
group.remove(s) | |
group.add((elem[0], ''.join(sorted(symbol+s[1])))) | |
break | |
if not found: | |
group.add((elem[0],symbol)) | |
def getlength(group): | |
l = 0 | |
for elem in group: | |
l+=len(elem[1]) | |
return l | |
def prettyprint(group, alphabet, numcolumns = 3): | |
d = dict(group) | |
x = -1 | |
for key, value in d.items(): | |
print ("{:>12}".format("".join(key)+"⟶"+"".join(value)), end="") | |
x+=1 | |
if (x == numcolumns): | |
x=-1 | |
print() | |
def armstrong(group, symbols): | |
for set1, set2 in itertools.product(itertools.product(symbols, repeat=3), | |
itertools.product(symbols, repeat=3)): | |
set1 = set(set1) | |
set2 = set(set2) | |
if (set2.issubset(set1)): | |
add(group, (set1, set2)) | |
lastlength = 0 | |
while lastlength != getlength(group): | |
lastlength = getlength(group) | |
for symbol1, symbol2, symbol3 in itertools.product( | |
itertools.product(symbols, repeat=3), repeat=3): | |
symbol1, symbol2, symbol3 = set(symbol1), set(symbol2), set(symbol3) | |
if isIn(group, (symbol1,symbol2)): | |
add(group, (symbol1.union(symbol3), symbol2.union(symbol3))) | |
if isIn(group, (symbol1, symbol2)) and isIn(group, (symbol2, symbol3)): | |
add(group, (symbol1, symbol3)) | |
alphabet = 'abcd' | |
objs = set() | |
add(objs, ("a", "b")) | |
add(objs, ("b", "c")) | |
armstrong(objs, alphabet) | |
prettyprint(objs, alphabet, 4) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment