Skip to content

Commit fcec603

Browse files
author
almuthana
committed
add
1 parent a3a6a28 commit fcec603

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

algorithms/maths/euler_totient.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,37 @@
44
which are coprime to n.
55
(Two numbers are coprime if their greatest common divisor (GCD) equals 1).
66
"""
7+
branch_coverage = {
8+
"check_1": False, # if branch for x > 0
9+
"check_2": False, # else branch
10+
"check_2": False,
11+
"check_2": False,
12+
}
713
def euler_totient(n):
814
"""Euler's totient function or Phi function.
915
Time Complexity: O(sqrt(n))."""
1016
result = n
1117
for i in range(2, int(n ** 0.5) + 1):
18+
branch_coverage["check_1"] = True
1219
if n % i == 0:
20+
branch_coverage["check_2"] = True
1321
while n % i == 0:
22+
branch_coverage["check_3"] = True
1423
n //= i
1524
result -= result // i
1625
if n > 1:
26+
branch_coverage["check_4"] = True
1727
result -= result // n
1828
return result
29+
30+
def print_coverage():
31+
total = len(branch_coverage)
32+
reached = sum(branch_coverage.values())
33+
coverage_percentage = (reached / total) * 100
34+
for branch, hit in branch_coverage.items():
35+
print(f"{branch} was {'hit' if hit else 'not hit'}")
36+
print(f"coverage_percentage: {coverage_percentage}%")
37+
38+
39+
result = euler_totient(21)
40+
print_coverage()

algorithms/strings/count_binary_substring.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,39 @@
1818
Explanation: There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1's and 0's.
1919
Reference: https://leetcode.com/problems/count-binary-substrings/description/
2020
"""
21+
branch_coverage = {
22+
"check_5": False,
23+
"check_6": False,
24+
"check_7": False,
25+
}
26+
2127
def count_binary_substring(s):
28+
global branch_coverage
2229
cur = 1
2330
pre = 0
2431
count = 0
2532
for i in range(1, len(s)):
33+
branch_coverage["check_5"] = True
2634
if s[i] != s[i - 1]:
35+
branch_coverage["check_6"] = True
2736
count = count + min(pre, cur)
2837
pre = cur
2938
cur = 1
3039
else:
40+
branch_coverage["check_7"] = True
3141
cur = cur + 1
3242
count = count + min(pre, cur)
3343
return count
44+
45+
46+
def print_coverage():
47+
total = len(branch_coverage)
48+
reached_branches = sum(branch_coverage.values())
49+
percentage = (reached_branches / total) * 100
50+
for branch, hit in branch_coverage.items():
51+
print(f"{branch} was {'hit' if hit else 'not hit'}")
52+
print(f"total Coverage: {percentage}%")
53+
54+
count = count_binary_substring("01100110")
55+
56+
print_coverage()

0 commit comments

Comments
 (0)