def collapse(patterns):
"""Reduce patterns into compact dash notation"""
- newPatterns = [] #reduced patterns
- matched = [] #indexes with a string that was already matched
- for x,p1 in enumerate(patterns): #pattern1
- if x in matched: continue #skip if this pattern already matched
- for y,p2 in enumerate(patterns[x+1:],1):
- if x+y in matched: continue #skip if this pattern already matched
- diffs=0 # number of differences found
- for idx,bit in enumerate(zip(p1,p2)):
- if bit[0] != bit [1]: # count of bits that are different
+ newPatterns = [] # reduced patterns
+ matched = [] # indexes with a string that was already matched
+ for x, p1 in enumerate(patterns): # pattern1
+ if x in matched: continue # skip if this pattern already matched
+ for y, p2 in enumerate(patterns[x+1:], 1):
+ if x+y in matched: continue # skip if this pattern already matched
+ diffs = 0 # number of differences found
+ for idx, bit in enumerate(zip(p1, p2)):
+ if bit[0] != bit[1]: # count of bits that are different
diffs += 1
- dbit = idx
- if diffs >1:break
+ dbit = idx
+ if diffs > 1: break
# if exactly 1 bit different between the two,
# they can be compressed together
if diffs == 1:
- newPatterns.append(p1[:dbit]+'-'+p1[dbit+1:])
+ newPatterns.append('-'.join([p1[:dbit], p1[dbit+1:]]))
matched+=[x,x+y]
break
# if the pattern wasn't matched, just append it as is.
if x not in matched: newPatterns.append(p1)
- # if reductions occured on this run, then call again
+ # if reductions occurred on this run, then call again
# to check if more are possible.
if matched:
newPatterns = collapse(newPatterns)