Skip to content

Instantly share code, notes, and snippets.

@MShrimp4
Created November 10, 2021 09:16
Show Gist options
  • Save MShrimp4/0db5c604c3bd21df88d6ca607f806b03 to your computer and use it in GitHub Desktop.
Save MShrimp4/0db5c604c3bd21df88d6ca607f806b03 to your computer and use it in GitHub Desktop.
Crappy impementation of Quine-McCluskey Algorithm
## Written by Yongha Hwang
import copy
import itertools
debug = False
name_list = list()
class bitvector:
def __init__(self, bit, essential):
self.bit = bit
self.essential = essential
self.prime = True
def is_prime (self):
return self.prime
def is_essential (self):
return self.essential
def __eq__ (self, other):
return self.bit == other.bit
def reprstr(self):
converted_list = list()
for i,j in zip(self.bit, name_list):
if i == "1":
converted_list.append("(%s)"%j)
elif i == "0":
converted_list.append("(%s')"%j)
if debug:
return "'{0}{1}{2}'".format(" " if self.essential else "d",
"p" if self.prime else " ",
"".join(converted_list))
else:
return "".join(converted_list)
def __repr__(self):
return self.reprstr()
def __str__(self):
return self.reprstr()
def __add__ (self, other):
lst = []
err = False
for i,j in zip(self.bit, other.bit):
if (i == "-") ^ (j == "-"):
return False
if i == j:
lst.append(i)
elif err:
return False
else:
err = True
lst.append("-")
self.prime = False
other.prime = False
return bitvector(lst, self.essential or other.essential)
def contains (self, other):
for i,j in zip(self.bit, other.bit):
if i != "-" and i != j:
return False
return True
def size_index (self):
return self.bit.count("-")
def count_index (self):
return self.bit.count("1")
def prime_contains_all (primes, essentials):
for e in essentials:
contains = False
for p in combination:
if p.contains(e):
contains = True
break
if not contains:
return False
return True
size = 0
essentials = []
primes = []
iter_data = []
name_list = input("Name for each bit:").split()
print (name_list)
while (vb := [c for c in input("bitvector (d= don't care, e to end):")])[0] != 'e':
essential = True
if (vb[0] == 'd'):
essential = False
vb = vb[1:]
if size == 0:
size = len(vb)
iter_data = [[list() for j in range(size-i+1)] for i in range (size+1)]
elif len(vb) != size:
print ("Wrong argument")
continue
bitvec = bitvector(vb, essential)
if essential:
essentials.append(copy.deepcopy(bitvec))
iter_data[0][bitvec.count_index()].append(bitvec)
for tries in range(size+1):
nextiter = []
for size_group in iter_data:
for i_count in range(len(size_group)-1):
for m in size_group[i_count]:
for n in size_group[i_count+1]:
sum = m + n
if sum and not (sum in nextiter) and not (sum in primes):
nextiter.append(sum)
for size_group in iter_data:
for count_group in size_group:
for item in count_group:
if item.is_prime() and not (item in primes):
primes.append(item)
count_group.remove(item)
for item in nextiter:
iter_data[item.size_index()][item.count_index()].append(item)
print ("\n\nPrimes:\n{0} \n".format(primes))
found = []
for i in range(1,len(primes)+1):
for combination in itertools.combinations(primes,i):
if prime_contains_all (list(combination), essentials):
found.append(combination)
if len(found) != 0:
break
minimum = []
max_dc = 0
for combination in found:
dc = 0
for pi in list(combination):
dc = dc + pi.size_index()
if dc >= max_dc:
minimum = combination
max_dc = dc
print ("\nMinimum:\n{0}\n".format(minimum))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment