aboutsummaryrefslogtreecommitdiff
path: root/ass1/binary_boyermoore.py
diff options
context:
space:
mode:
authorakiyamn2021-03-23 17:07:50 +1100
committerakiyamn2021-03-23 17:07:50 +1100
commitf0cdffeacc7e1996ac1fa97647105701f9300ef8 (patch)
tree2f77eacf87a5768c3d330c09a53f90b19dffb304 /ass1/binary_boyermoore.py
parentde34b185172d1ce1b72400e9806600e3cd795f1b (diff)
downloadfit3155-f0cdffeacc7e1996ac1fa97647105701f9300ef8.tar.gz
fit3155-f0cdffeacc7e1996ac1fa97647105701f9300ef8.zip
i thought it was working but it wasn't, might be now
Diffstat (limited to 'ass1/binary_boyermoore.py')
-rw-r--r--ass1/binary_boyermoore.py30
1 files changed, 21 insertions, 9 deletions
diff --git a/ass1/binary_boyermoore.py b/ass1/binary_boyermoore.py
index d9b813d..a7193ad 100644
--- a/ass1/binary_boyermoore.py
+++ b/ass1/binary_boyermoore.py
@@ -1,6 +1,6 @@
-txt = "zzzzzzabcdzzzzzz"
-# pat = "tbapxab"
-pat = "abcd"
+txt = "zzzzaaabcdabcdazzzz"
+pat = "abcda"
+# pat = "abcda"
m = len(pat)
n = len(txt)
R = [m for _ in range(0, 26)]
@@ -49,17 +49,19 @@ def gusfield(string):
for i in range(m):
+ print(pat[i], i)
R[alpha_number(pat[i])] = i
Z = reverse(gusfield(reverse(pat)))+[0]
print(list(pat))
+print(R)
print(Z)
for i in range(m):
j = m - Z[i]
- good_suffix[j] = i
+ good_suffix[j] = i+1
-print(good_suffix)
+print("g", good_suffix)
matched_prefix = gusfield(pat)+[0]
for i in range(m-1, -1, -1):
@@ -67,16 +69,26 @@ for i in range(m-1, -1, -1):
print(matched_prefix)
+print("="*15)
+print(5*" " + txt)
+
j = 0
while j < n-m:
matches = 0
for i in range(m-1, -1, -1):
- print(j)
+ print(f"{j=} {' ' * j}", end="")
+ for x in range(len(pat)):
+ if x == i:
+ print(pat[x].upper(), end="")
+ else:
+ print(pat[x], end="")
+ print()
+ # print(j)
match = pat[i] == txt[j+i]
if match:
matches += 1
if matches == m:
- print(f"Match at {j}")
+ # print(f" Match at {j}", end="")
good_suffix_shift = m - matched_prefix[1]
j += good_suffix_shift
i = m-1
@@ -87,13 +99,13 @@ while j < n-m:
bad_char_shift = i-R[alpha_number(mismatched)]
good_suffix_shift = 1
if good_suffix[i+1] > 0:
- good_suffix_shift = m - good_suffix[i+1]-1
+ good_suffix_shift = m - good_suffix[i+1]
elif good_suffix[i+1] == 0:
good_suffix_shift = m - matched_prefix[i+1]
elif matches == m:
print(f"Match at {j}")
good_suffix_shift = m - matched_prefix[1]
j += max(good_suffix_shift, bad_char_shift)
+ break
if j > n-m:
break
-print(R)