Reducing binary patterns in Python -
Reducing binary patterns in Python -
i've got think interesting problem, programming exercise point of view.
i have long list of binary patterns want cut down more compact form nowadays users. notation followed '-' can represent either '1' or '0', ['1011','1010']
represented ['101-']
and
['1100', '1000', '0100', '0000', '1111', '1011', '0111', '0011']
could represented ['--00', '--11']
. note patterns same length (though quite perchance longer 4 bits).
expanding patterns trivial, reducing them bit trickier.
i've come code accomplishes this, long, slow, , kind of hard read.
def reducepatterns(patterns): '''reduce patterns compact dash notation''' newpatterns = [] #reduced patterns matched = [] #indexes string matched x,p1 in enumerate(patterns): #pattern1 if x in matched: go on #skip if pattern has been matched y,p2 in enumerate(patterns[x+1:],1): if x+y in matched: go on #skip if pattern has been matched diffs=0 # number of differences found idx,bit in enumerate(zip(p1,p2)): if bit[0] != bit [1]: #count number of bits different diffs += 1 dbit = idx if diffs >1:break if diffs ==1: #if 1 bit different between two, can compressed newpatterns.append(p1[:dbit]+'-'+p1[dbit+1:]) matched+=[x,x+y] break if x not in matched: newpatterns.append(p1) #if pattern wasn't matched, append is. if matched: #if reductions occured on run, phone call 1 time again check if more possible. newpatterns = reducepatterns(newpatterns) homecoming newpatterns
does out there have suggestions better/more efficient way this? more effective looping/use of iterators? regex magic? bitwise manipulation bundle i've been missing? little bit more readable @ least?
what looking quine–mccluskey algorithm implementation in python.
a quick google took me page quine-mccluskey algorithm in python
python
Comments
Post a Comment