Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add #5

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions algorithms/maths/euler_totient.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,37 @@
which are coprime to n.
(Two numbers are coprime if their greatest common divisor (GCD) equals 1).
"""
branch_coverage = {
"check_1": False, # if branch for x > 0
"check_2": False, # else branch
"check_2": False,
"check_2": False,
}
def euler_totient(n):
"""Euler's totient function or Phi function.
Time Complexity: O(sqrt(n))."""
result = n
for i in range(2, int(n ** 0.5) + 1):
branch_coverage["check_1"] = True
if n % i == 0:
branch_coverage["check_2"] = True
while n % i == 0:
branch_coverage["check_3"] = True
n //= i
result -= result // i
if n > 1:
branch_coverage["check_4"] = True
result -= result // n
return result

def print_coverage():
total = len(branch_coverage)
reached = sum(branch_coverage.values())
coverage_percentage = (reached / total) * 100
for branch, hit in branch_coverage.items():
print(f"{branch} was {'hit' if hit else 'not hit'}")
print(f"coverage_percentage: {coverage_percentage}%")


result = euler_totient(21)
print_coverage()
23 changes: 23 additions & 0 deletions algorithms/strings/count_binary_substring.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,39 @@
Explanation: There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1's and 0's.
Reference: https://leetcode.com/problems/count-binary-substrings/description/
"""
branch_coverage = {
"check_5": False,
"check_6": False,
"check_7": False,
}

def count_binary_substring(s):
global branch_coverage
cur = 1
pre = 0
count = 0
for i in range(1, len(s)):
branch_coverage["check_5"] = True
if s[i] != s[i - 1]:
branch_coverage["check_6"] = True
count = count + min(pre, cur)
pre = cur
cur = 1
else:
branch_coverage["check_7"] = True
cur = cur + 1
count = count + min(pre, cur)
return count


def print_coverage():
total = len(branch_coverage)
reached_branches = sum(branch_coverage.values())
percentage = (reached_branches / total) * 100
for branch, hit in branch_coverage.items():
print(f"{branch} was {'hit' if hit else 'not hit'}")
print(f"total Coverage: {percentage}%")

count = count_binary_substring("01100110")

print_coverage()