Skip to content

Commit 5651aba

Browse files
committed
first_unique_function
1 parent 60832d9 commit 5651aba

File tree

6 files changed

+117
-2
lines changed

6 files changed

+117
-2
lines changed

algorithms/maths/find_order_simple.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,42 @@
1111

1212
import math
1313

14+
branch_coverage = {
15+
"branch_6": False,
16+
"branch_7": False,
17+
"branch_8": False,
18+
"branch_9": False,
19+
"branch_10": False
20+
}
21+
1422
def find_order(a, n):
1523
"""
1624
Find order for positive integer n and given integer a that satisfies gcd(a, n) = 1.
1725
"""
26+
1827
if (a == 1) & (n == 1):
1928
# Exception Handeling : 1 is the order of of 1
29+
branch_coverage["branch_6"] = True
30+
print("branch_6")
2031
return 1
2132
if math.gcd(a, n) != 1:
33+
branch_coverage["branch_7"] = True
34+
print("branch_7")
2235
print ("a and n should be relative prime!")
2336
return -1
2437
for i in range(1, n):
38+
branch_coverage["branch_8"] = True
39+
print("branch_8")
2540
if pow(a, i) % n == 1:
41+
branch_coverage["branch_9"] = True
42+
print("branch_9")
2643
return i
44+
branch_coverage["branch_10"] = True
45+
print("branch_10")
2746
return -1
47+
48+
def print_coverage():
49+
for branch, hit in branch_coverage.items():
50+
print(f"{branch} was {'hit' if hit else 'not hit'}")
51+
52+
print_coverage()

algorithms/maths/prime_check.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,42 @@
1+
branch_coverage = {
2+
"branch_1": False, # n <= 1
3+
"branch_2": False, # n == 2 or n == 3
4+
"branch_3": False, # n % 2 == 0 or n % 3 == 0
5+
"branch_4": False, # while j * j <= n
6+
"branch_5": False # n % j == 0 or n % (j + 2) == 0
7+
}
18
def prime_check(n):
29
"""Return True if n is a prime number
310
Else return False.
411
"""
12+
print(f"Checking {n}") # Debugging statement
513

614
if n <= 1:
15+
branch_coverage["branch_1"] = True
16+
print("branch_1")
717
return False
18+
819
if n == 2 or n == 3:
20+
branch_coverage["branch_2"] = True
21+
print("branch_2")
922
return True
1023
if n % 2 == 0 or n % 3 == 0:
24+
branch_coverage["branch_3"] = True
25+
print("branch_3")
1126
return False
1227
j = 5
1328
while j * j <= n:
29+
branch_coverage["branch_4"] = True
30+
print("branch_4")
1431
if n % j == 0 or n % (j + 2) == 0:
32+
branch_coverage["branch_5"] = True
33+
print("branch_5")
1534
return False
1635
j += 6
1736
return True
37+
38+
def print_coverage():
39+
for branch, hit in branch_coverage.items():
40+
print(f"{branch} was {'hit' if hit else 'not hit'}")
41+
42+
print_coverage()

algorithms/maths/pythagoras.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
Given the lengths of two of the three sides of a right angled triangle, this function returns the
33
length of the third side.
44
"""
5+
branch_coverage = {
6+
"branch_1": False,
7+
"branch_2": False,
8+
"branch_3": False,
9+
"branch_4": False,
10+
#"branch_5": False
11+
}
512

613
def pythagoras(opposite, adjacent, hypotenuse):
714
"""
@@ -10,11 +17,32 @@ def pythagoras(opposite, adjacent, hypotenuse):
1017
"""
1118
try:
1219
if opposite == str("?"):
20+
branch_coverage["branch_1"] = True
21+
print("branch_1")
1322
return ("Opposite = " + str(((hypotenuse**2) - (adjacent**2))**0.5))
1423
if adjacent == str("?"):
24+
branch_coverage["branch_2"] = True
25+
print("branch_2")
1526
return ("Adjacent = " + str(((hypotenuse**2) - (opposite**2))**0.5))
1627
if hypotenuse == str("?"):
28+
branch_coverage["branch_3"] = True
29+
print("branch_3")
1730
return ("Hypotenuse = " + str(((opposite**2) + (adjacent**2))**0.5))
31+
branch_coverage["branch_4"] = True
32+
print("branch_4")
1833
return "You already know the answer!"
1934
except:
35+
# branch_coverage["branch_5"] = True
36+
# print("branch_5")
2037
raise ValueError("invalid argument(s) were given.")
38+
39+
def print_coverage():
40+
for branch, hit in branch_coverage.items():
41+
print(f"{branch} was {'hit' if hit else 'not hit'}")
42+
43+
print_coverage()
44+
45+
# total = len(branch_coverage)
46+
# reached_branches = sum(branch_coverage.values())
47+
# percentage = (reached_branches/total)*100
48+
# print_coverage()

algorithms/search/interpolation_search.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313

1414
from typing import List
1515

16+
branch_coverage = {
17+
"branch_60": False,
18+
"branch_61": False,
19+
"branch_62": False,
20+
"branch_63": False,
21+
"branch_64": False,
22+
23+
}
24+
1625

1726
def interpolation_search(array: List[int], search_key: int) -> int:
1827
"""
@@ -38,24 +47,36 @@ def interpolation_search(array: List[int], search_key: int) -> int:
3847

3948
while (low <= high) and (array[low] <= search_key <= array[high]):
4049
# calculate the search position
50+
branch_coverage["branch_60"] = True
4151
pos = low + int(((search_key - array[low]) *
4252
(high - low) / (array[high] - array[low])))
4353

4454
# search_key is found
4555
if array[pos] == search_key:
56+
branch_coverage["branch_61"] = True
4657
return pos
4758

4859
# if search_key is larger, search_key is in upper part
4960
if array[pos] < search_key:
61+
branch_coverage["branch_62"] = True
5062
low = pos + 1
5163

5264
# if search_key is smaller, search_key is in lower part
5365
else:
66+
branch_coverage["branch_63"] = True
5467
high = pos - 1
55-
68+
69+
branch_coverage["branch_64"] = True
5670
return -1
5771

72+
def print_coverage():
73+
for branch, hit in branch_coverage.items():
74+
print(f"{branch} was {'hit' if hit else 'not hit'}")
75+
76+
5877

5978
if __name__ == "__main__":
6079
import doctest
6180
doctest.testmod()
81+
print_coverage()
82+

tests/test_search.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,28 @@ def test_next_greatest_letter(self):
139139
self.assertEqual("c", next_greatest_letter_v2(letters, target))
140140

141141
def test_interpolation_search(self):
142-
array = [0, 3, 5, 5, 9, 12, 12, 15, 16, 19, 20]
142+
array = [0, 3, 5, 5, 9, 12, 12, 15, 16, 19, 20, 25]
143+
#, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]
144+
145+
# array = [0, 3, 5, 5, 9, 12, 12, 15, 16, 19, 20, 25]
143146
self.assertEqual(1, interpolation_search(array, 3))
144147
self.assertEqual(2, interpolation_search(array, 5))
145148
self.assertEqual(6, interpolation_search(array, 12))
146149
self.assertEqual(-1, interpolation_search(array, 22))
147150
self.assertEqual(-1, interpolation_search(array, -10))
148151
self.assertEqual(10, interpolation_search(array, 20))
152+
153+
# self.assertEqual(-1, interpolation_search(array, 1))
154+
# self.assertEqual(0, interpolation_search(array, 0)) # Found at the start
155+
# self.assertEqual(interpolation_search(array, 12), 5) # Found in the middle, first occurrence
156+
# self.assertEqual(-1, interpolation_search(array, 110)) # Not found, above maximum
157+
# self.assertEqual(interpolation_search(array, 55), 17) # Found in the middle
158+
# self.assertEqual(interpolation_search(array, 70), 20) # Found, single value in range
159+
# self.assertEqual(interpolation_search(array, 22), -1) # Not found, above the range
160+
# self.assertEqual(interpolation_search(array, 50), 16) # Found, single value in range
161+
# self.assertEqual(interpolation_search(array, 45), 15) # Found, single value in range
162+
163+
149164

150165

151166
if __name__ == '__main__':

tests/test_strings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ class TestFirstUniqueChar(unittest.TestCase):
568568
def test_first_unique_char(self):
569569
self.assertEqual(0, first_unique_char("leetcode"))
570570
self.assertEqual(2, first_unique_char("loveleetcode"))
571+
self.assertEqual(-1, first_unique_char("aabb"))
571572

572573

573574
class TestRepeatSubstring(unittest.TestCase):

0 commit comments

Comments
 (0)