1 # https://stackoverflow.com/questions/14860967/
2 # https://stackoverflow.com/questions/7537125/
4 def collapse(patterns
):
5 """Reduce patterns into compact dash notation"""
6 newPatterns
= [] # reduced patterns
7 matched
= [] # indexes with a string that was already matched
8 for x
, p1
in enumerate(patterns
): # pattern1
9 if x
in matched
: continue # skip if this pattern already matched
10 for y
, p2
in enumerate(patterns
[x
+1:], 1):
11 if x
+y
in matched
: continue # skip if this pattern already matched
12 diffs
= 0 # number of differences found
13 for idx
, bit
in enumerate(zip(p1
, p2
)):
14 if bit
[0] != bit
[1]: # count of bits that are different
18 # if exactly 1 bit different between the two,
19 # they can be compressed together
21 newPatterns
.append('-'.join([p1
[:dbit
], p1
[dbit
+1:]]))
24 # if the pattern wasn't matched, just append it as is.
25 if x
not in matched
: newPatterns
.append(p1
)
27 # if reductions occurred on this run, then call again
28 # to check if more are possible.
30 newPatterns
= collapse(newPatterns
)
34 if __name__
== '__main__':
70 isel1
= collapse(isel
)
90 svshape1
= collapse(svshape
)
91 print ("svshape", svshape1
)