-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmp_module.py
More file actions
101 lines (79 loc) · 2.1 KB
/
kmp_module.py
File metadata and controls
101 lines (79 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import itertools
# Initialise array based on the failure function:
def init_arr(w):
m = len(w)
i = 0
j = 1
# No proper prefix for string of length 1:
global arr
arr[0] = 0
while j < m:
if w[i] == w[j]:
i += 1
arr[j] = i
j += 1
# The first character didn't match:
elif i == 0:
arr[j] = 0
j += 1
# Mismatch after at least one matching character:
else:
i = arr[i - 1]
def kmp_search(w, s):
# Initialise array:
init_arr(w)
# Initialising variables:
i = 0
j = 0
m = len(w)
n = len(s)
# Start search:
while i < m and j < n:
# Last character matches -> Substring found:
if w[i] == s[j] and i == m - 1:
return True
# Character matches:
elif w[i] == s[j]:
i += 1
j += 1
# Character didn't match -> Backtrack:
else:
i = arr[i - 1]
if i == 0:
j += 1
# Substring not found:
return False
def kmp_search(file1,file2):
i=0
for line in file1 & file2:
if line:
print(line)
i=1
if(i==1): return True
elif(i == 0): return False
def listToString(s):
str1 = ""
for ele in s:
str1 += ele
return str1
def kmp_compare(f1,f2):
print()
print("----- KMP Analysis of "+f1+" & "+f2+" -----")
print()
print("Common Lines Identified:")
file1 = set(line.strip() for line in open(f1))
file2 = set(line.strip() for line in open(f2))
if(not (kmp_search(file1,file2))): print("None Found")
line1=[line.rstrip('\n') for line in open(f1)]
linelist2 = [line.rstrip('\n') for line in open(f2)]
line1=listToString(line1)
for a in line1:
if (a==''): line1.remove(a)
linelist2 = [line.rstrip('\n') for line in open(f1)]
# create array:
arr = [None] * len(line1)
# create array:
arr = [None] * len(line1)
print()
print()
print()