Skip to content

Commit ca93612

Browse files
Merge pull request #8 from CatalinAnt/Almu
2 parents d4de0f6 + 57b6687 commit ca93612

File tree

2 files changed

+57
-11
lines changed

2 files changed

+57
-11
lines changed

algorithms/dp/word_break.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
"""
1818

1919

20+
# TC: O(N^2) SC: O(N)
21+
branch_coverage = {
22+
"check_1": False,
23+
"check_2": False,
24+
"check_3": False,
25+
"check_4": False,
26+
27+
}
2028
# TC: O(N^2) SC: O(N)
2129
def word_break(word, word_dict):
2230
"""
@@ -27,15 +35,29 @@ def word_break(word, word_dict):
2735
dp_array = [False] * (len(word)+1)
2836
dp_array[0] = True
2937
for i in range(1, len(word)+1):
38+
branch_coverage["check_1"] = True
3039
for j in range(0, i):
40+
branch_coverage["check_2"] = True
3141
if dp_array[j] and word[j:i] in word_dict:
42+
branch_coverage["check_3"] = True
3243
dp_array[i] = True
3344
break
45+
branch_coverage["check_4"] = True
3446
return dp_array[-1]
3547

3648

37-
if __name__ == "__main__":
38-
STR = "keonkim"
39-
dic = ["keon", "kim"]
49+
def print_coverage():
50+
total = len(branch_coverage)
51+
reached = sum(branch_coverage.values())
52+
coverage_percentage = (reached / total) * 100
53+
for branch, hit in branch_coverage.items():
54+
print(f"{branch} was {'hit' if hit else 'not hit'}")
55+
print(f"coverage_percentage: {coverage_percentage}%")
56+
57+
58+
result = word_break("keonkim", {"keon", "kim"})
59+
print_coverage()
60+
61+
62+
4063

41-
print(word_break(str, dic))

algorithms/sort/stooge_sort.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,30 @@
66
77
'''
88

9-
9+
branch_coverage = {
10+
"check_1": False, # if branch for x > 0
11+
"check_2": False, # else branch
12+
"check_3": False,
13+
"check_4": False,
14+
}
1015

1116
def stoogesort(arr, l, h):
1217
if l >= h:
18+
branch_coverage["check_1"] = True
1319
return
1420

1521
# If first element is smaller
1622
# than last, swap them
1723
if arr[l]>arr[h]:
24+
branch_coverage["check_2"] = True
1825
t = arr[l]
1926
arr[l] = arr[h]
2027
arr[h] = t
2128

2229
# If there are more than 2 elements in
2330
# the array
2431
if h-l + 1 > 2:
32+
branch_coverage["check_3"] = True
2533
t = (int)((h-l + 1)/3)
2634

2735
# Recursively sort first 2 / 3 elements
@@ -33,11 +41,27 @@ def stoogesort(arr, l, h):
3341
# Recursively sort first 2 / 3 elements
3442
# again to confirm
3543
stoogesort(arr, l, (h-t))
44+
45+
branch_coverage["check_4"] = True
46+
47+
def print_coverage():
48+
total = len(branch_coverage)
49+
reached = sum(branch_coverage.values())
50+
coverage_percentage = (reached / total) * 100
51+
for branch, hit in branch_coverage.items():
52+
print(f"{branch} was {'hit' if hit else 'not hit'}")
53+
print(f"coverage_percentage: {coverage_percentage}%")
54+
55+
arr1 = [10, -1, 2, 3, 0]
56+
arr2 = []
57+
result = stoogesort(arr1, 0, len(arr1) - 1)
58+
result = stoogesort(arr1, 0, len(arr2) - 1)
59+
print_coverage()
3660

3761

38-
if __name__ == "__main__":
39-
array = [1,3,64,5,7,8]
40-
n = len(array)
41-
stoogesort(array, 0, n-1)
42-
for i in range(0, n):
43-
print(array[i], end = ' ')
62+
# if __name__ == "__main__":
63+
# array = [1,3,64,5,7,8]
64+
# n = len(array)
65+
# stoogesort(array, 0, n-1)
66+
# for i in range(0, n):
67+
# print(array[i], end = ' ')

0 commit comments

Comments
 (0)