Skip to content

Commit 699971b

Browse files
committed
logging in searches
1 parent e52c94e commit 699971b

File tree

3 files changed

+52
-35
lines changed

3 files changed

+52
-35
lines changed

competitivepython/searches/KMP_pattern_search.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import logging
2+
13
def kmp_search(pat, txt):
24
M = len(pat)
35
N = len(txt)
@@ -7,30 +9,34 @@ def kmp_search(pat, txt):
79
lps = [0] * M
810
j = 0 # index for pat[]
911

10-
# Preprocess the pattern (calculate lps[] array)
11-
computeLPSArray(pat, M, lps)
12-
13-
i = 0 # index for txt[]
14-
indices = []
15-
while i < N:
16-
if pat[j] == txt[i]:
17-
i += 1
18-
j += 1
12+
try:
13+
# Preprocess the pattern (calculate lps[] array)
14+
computeLPSArray(pat, M, lps)
1915

20-
if j == M:
21-
indices.append(i - j)
22-
j = lps[j - 1]
16+
i = 0 # index for txt[]
17+
indices = []
18+
while i < N:
19+
if pat[j] == txt[i]:
20+
i += 1
21+
j += 1
2322

24-
# mismatch after j matches
25-
elif i < N and pat[j] != txt[i]:
26-
# Do not match lps[0..lps[j-1]] characters,
27-
# they will match anyway
28-
if j != 0:
23+
if j == M:
24+
indices.append(i - j)
2925
j = lps[j - 1]
30-
else:
31-
i += 1
3226

33-
return indices
27+
# mismatch after j matches
28+
elif i < N and pat[j] != txt[i]:
29+
# Do not match lps[0..lps[j-1]] characters,
30+
# they will match anyway
31+
if j != 0:
32+
j = lps[j - 1]
33+
else:
34+
i += 1
35+
36+
return indices
37+
except Exception as e:
38+
logging.exception("An error occurred during KMP search: %s", e)
39+
return []
3440

3541
def computeLPSArray(pat, M, lps):
3642
len = 0 # length of the previous longest prefix suffix
@@ -55,5 +61,3 @@ def computeLPSArray(pat, M, lps):
5561
else:
5662
lps[i] = 0
5763
i += 1
58-
59-
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
import logging
2+
13
def binary_search(arr, target):
24
left, right = 0, len(arr) - 1
3-
while left <= right:
4-
mid = left + (right - left) // 2
5-
if arr[mid] == target:
6-
return mid
7-
elif arr[mid] < target:
8-
left = mid + 1
9-
else:
10-
right = mid - 1
11-
return -1
5+
try:
6+
while left <= right:
7+
mid = left + (right - left) // 2
8+
if arr[mid] == target:
9+
return mid
10+
elif arr[mid] < target:
11+
left = mid + 1
12+
else:
13+
right = mid - 1
14+
return -1
15+
except Exception as e:
16+
logging.exception("An error occurred during binary search: %s", e)
17+
return -1
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
import logging
2+
13
def linear_search(arr, target):
2-
for search in range(len(arr)):
3-
if arr[search] == target:
4-
return search
5-
return -1
4+
try:
5+
for search in range(len(arr)):
6+
if arr[search] == target:
7+
return search
8+
return -1
9+
except Exception as e:
10+
logging.exception("An error occurred during binary search: %s", e)
11+
return -1
12+

0 commit comments

Comments
 (0)