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

updated #928

Open
wants to merge 81 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
c6f9dd0
Added README.md template (see section 3.6 in Assignemnt 1),
aymanpopje Jun 13, 2024
bbce1bd
Added missing comma in "test_array.py"
aymanpopje Jun 13, 2024
a2036f5
Created script for measuring doing tests and measuring branch coverag…
aymanpopje Jun 14, 2024
a3a6a28
Merge pull request #1 from CatalinAnt/AymanBranch
aymanpopje Jun 14, 2024
f455324
Improved test_unix.py by adding new test cases for "test_simplify_path"
aymanpopje Jun 14, 2024
fcec603
add
Jun 14, 2024
22ee6fa
Instrumented "simplify_path_v2"
aymanpopje Jun 15, 2024
5dae7f2
Instrumented "insertion_sort"
aymanpopje Jun 15, 2024
3a34ead
Added Diff of new and old code to README.md
aymanpopje Jun 15, 2024
c6a5436
Merge pull request #2 from CatalinAnt/ayman-branch-cov
aymanpopje Jun 15, 2024
2b0b918
Added instrumentation for branch coverage + improvements
CatalinAnt Jun 18, 2024
5a09e40
Merge pull request #3 from CatalinAnt/CatalinBranch1
CatalinAnt Jun 18, 2024
eaad6d3
Improved upon previous instrumentation and tests
CatalinAnt Jun 18, 2024
d36e32f
Merge pull request #4 from CatalinAnt/CatalinBranch1
CatalinAnt Jun 18, 2024
6c12e56
Merge pull request #5 from CatalinAnt/Almu
MuthanaMustafa Jun 18, 2024
869dd97
Update README.md (not finished)
CatalinAnt Jun 19, 2024
60832d9
pythagoras test_maths
bodda93 Jun 20, 2024
5651aba
first_unique_function
bodda93 Jun 20, 2024
3ccee3b
fix
bodda93 Jun 22, 2024
857cf17
Merge branch 'master' of https://github.com/CatalinAnt/algorithms-SEP-95
bodda93 Jun 22, 2024
4baef66
new branch
bodda93 Jun 24, 2024
c16f26e
branch numbers
bodda93 Jun 24, 2024
ab40f93
Update README.md
CatalinAnt Jun 24, 2024
08ad526
test case
bodda93 Jun 24, 2024
59424da
Merge branch 'master' of https://github.com/CatalinAnt/algorithms-SEP-95
bodda93 Jun 24, 2024
ffbac22
Update README.md
CatalinAnt Jun 24, 2024
db18006
Update README.md
CatalinAnt Jun 24, 2024
dd7c72a
Update README.md
CatalinAnt Jun 24, 2024
56f72d6
first_unique_char_printCoverage
bodda93 Jun 24, 2024
e594d90
Merge branch 'master' of https://github.com/CatalinAnt/algorithms-SEP-95
bodda93 Jun 24, 2024
bb4e552
Update README.md
CatalinAnt Jun 24, 2024
b774260
Update README.md
bodda93 Jun 24, 2024
44a27cb
Merge branch 'master' into bodda93-patch-1
bodda93 Jun 24, 2024
07b0bd5
Merge pull request #6 from CatalinAnt/bodda93-patch-1
bodda93 Jun 24, 2024
c45cfea
Update README.md
bodda93 Jun 24, 2024
0a643f8
Update README.md
CatalinAnt Jun 24, 2024
bd18bf9
Update README.md
CatalinAnt Jun 24, 2024
0a33262
Update README.md
CatalinAnt Jun 24, 2024
76cdd11
Update README.md
CatalinAnt Jun 24, 2024
f8ed727
Update README.md
CatalinAnt Jun 24, 2024
175e67a
Update README.md
CatalinAnt Jun 24, 2024
94547f7
Merge branch 'master' into CatalinAnt-patch-1
CatalinAnt Jun 24, 2024
2be0207
Merge pull request #7 from CatalinAnt/CatalinAnt-patch-1
CatalinAnt Jun 24, 2024
64797c2
Update README.md
CatalinAnt Jun 26, 2024
9653ea6
Update README.md
CatalinAnt Jun 26, 2024
d4de0f6
Added pre-improvement instrument measurements
aymanpopje Jun 26, 2024
57b6687
add
Jun 26, 2024
ca93612
Merge pull request #8 from CatalinAnt/Almu
MuthanaMustafa Jun 26, 2024
157de36
add
Jun 26, 2024
386ed52
add
Jun 26, 2024
0416cab
Merge pull request #9 from CatalinAnt/Almu
MuthanaMustafa Jun 26, 2024
0f4d2d9
add
Jun 26, 2024
b0de551
add
Jun 26, 2024
4b6a7b9
add
Jun 26, 2024
d823b9c
add
Jun 26, 2024
fab5c24
add
Jun 26, 2024
dbb131c
add
Jun 26, 2024
c8c7575
added
Jun 26, 2024
6bab0cd
added
Jun 26, 2024
d7d2780
add
Jun 26, 2024
50cfaff
add
Jun 26, 2024
39e7967
ad
Jun 26, 2024
6f2fbd0
Merge branch 'master' into Almu
MuthanaMustafa Jun 26, 2024
ec2ea92
Merge pull request #10 from CatalinAnt/Almu
MuthanaMustafa Jun 26, 2024
2063648
images added
Jun 26, 2024
60118df
image added
MuthanaMustafa Jun 26, 2024
7c2276a
ad
MuthanaMustafa Jun 26, 2024
162f9a8
Merge pull request #11 from CatalinAnt/Almu
MuthanaMustafa Jun 26, 2024
cf6dfe6
Fixed instrumentation (reported in README.md)
aymanpopje Jun 26, 2024
c7cb85e
Merge branch 'master' of https://github.com/CatalinAnt/algorithms-SEP-95
aymanpopje Jun 26, 2024
2fed7e9
Update README.md
bodda93 Jun 26, 2024
44cd4e7
Merge pull request #12 from CatalinAnt/bodda93-patch-2
bodda93 Jun 26, 2024
190081b
Update README.md
bodda93 Jun 26, 2024
bc42bcc
Update README.md
bodda93 Jun 26, 2024
2017fe3
updated
MuthanaMustafa Jun 27, 2024
d19bc03
Merge branch 'master' into Almu
MuthanaMustafa Jun 27, 2024
fee9d5a
Merge pull request #13 from CatalinAnt/Almu
MuthanaMustafa Jun 27, 2024
afd12cb
Fixed wrong image usage
aymanpopje Jun 27, 2024
8c9caa8
Merge branch 'master' of https://github.com/CatalinAnt/algorithms-SEP-95
aymanpopje Jun 27, 2024
253296e
Fixed typos
aymanpopje Jun 27, 2024
1add5ec
Removed duplicate text in README.md
aymanpopje Jun 27, 2024
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
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[report]
omit =
*/tests/*
*/python?.?/*
*/site-packages/nose/*
*__init__*
727 changes: 303 additions & 424 deletions README.md

Large diffs are not rendered by default.

424 changes: 424 additions & 0 deletions README_original.md

Large diffs are not rendered by default.

30 changes: 26 additions & 4 deletions algorithms/dp/word_break.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
"""


# TC: O(N^2) SC: O(N)
branch_coverage = {
"check_5": False,
"check_6": False,
"check_7": False,
"check_8": False,

}
# TC: O(N^2) SC: O(N)
def word_break(word, word_dict):
"""
Expand All @@ -27,15 +35,29 @@ def word_break(word, word_dict):
dp_array = [False] * (len(word)+1)
dp_array[0] = True
for i in range(1, len(word)+1):
branch_coverage["check_5"] = True
for j in range(0, i):
branch_coverage["check_6"] = True
if dp_array[j] and word[j:i] in word_dict:
branch_coverage["check_7"] = True
dp_array[i] = True
break
branch_coverage["check_8"] = True
return dp_array[-1]


if __name__ == "__main__":
STR = "keonkim"
dic = ["keon", "kim"]
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 = word_break("keonkim", {"keon", "kim"})
print_coverage()




print(word_break(str, dic))
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()
25 changes: 25 additions & 0 deletions algorithms/maths/find_order_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,42 @@

import math

branch_coverage = {
"branch_6": False,
"branch_7": False,
"branch_8": False,
"branch_9": False,
"branch_10": False
}

def find_order(a, n):
"""
Find order for positive integer n and given integer a that satisfies gcd(a, n) = 1.
"""

if (a == 1) & (n == 1):
# Exception Handeling : 1 is the order of of 1
branch_coverage["branch_6"] = True
print("branch_6")
return 1
if math.gcd(a, n) != 1:
branch_coverage["branch_7"] = True
print("branch_7")
print ("a and n should be relative prime!")
return -1
for i in range(1, n):
branch_coverage["branch_8"] = True
print("branch_8")
if pow(a, i) % n == 1:
branch_coverage["branch_9"] = True
print("branch_9")
return i
branch_coverage["branch_10"] = True
print("branch_10")
return -1

def print_coverage():
for branch, hit in branch_coverage.items():
print(f"{branch} was {'hit' if hit else 'not hit'}")

print_coverage()
25 changes: 25 additions & 0 deletions algorithms/maths/prime_check.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
branch_coverage = {
"branch_1": False, # n <= 1
"branch_2": False, # n == 2 or n == 3
"branch_3": False, # n % 2 == 0 or n % 3 == 0
"branch_4": False, # while j * j <= n
"branch_5": False # n % j == 0 or n % (j + 2) == 0
}
def prime_check(n):
"""Return True if n is a prime number
Else return False.
"""
print(f"Checking {n}") # Debugging statement

if n <= 1:
branch_coverage["branch_1"] = True
print("branch_1")
return False

if n == 2 or n == 3:
branch_coverage["branch_2"] = True
print("branch_2")
return True
if n % 2 == 0 or n % 3 == 0:
branch_coverage["branch_3"] = True
print("branch_3")
return False
j = 5
while j * j <= n:
branch_coverage["branch_4"] = True
print("branch_4")
if n % j == 0 or n % (j + 2) == 0:
branch_coverage["branch_5"] = True
print("branch_5")
return False
j += 6
return True

def print_coverage():
for branch, hit in branch_coverage.items():
print(f"{branch} was {'hit' if hit else 'not hit'}")

print_coverage()
21 changes: 21 additions & 0 deletions algorithms/maths/pythagoras.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
Given the lengths of two of the three sides of a right angled triangle, this function returns the
length of the third side.
"""
branch_coverage = {
"branch_31": False,
"branch_32": False,
"branch_33": False,
"branch_34": False,
"branch_35": False
}

def pythagoras(opposite, adjacent, hypotenuse):
"""
Expand All @@ -10,11 +17,25 @@ def pythagoras(opposite, adjacent, hypotenuse):
"""
try:
if opposite == str("?"):
branch_coverage["branch_31"] = True
return ("Opposite = " + str(((hypotenuse**2) - (adjacent**2))**0.5))
if adjacent == str("?"):
branch_coverage["branch_32"] = True
return ("Adjacent = " + str(((hypotenuse**2) - (opposite**2))**0.5))
if hypotenuse == str("?"):
branch_coverage["branch_33"] = True
return ("Hypotenuse = " + str(((opposite**2) + (adjacent**2))**0.5))
branch_coverage["branch_34"] = True
return "You already know the answer!"
except:
branch_coverage["branch_35"] = True
raise ValueError("invalid argument(s) were given.")

def print_coverage():
for branch, hit in branch_coverage.items():
print(f"{branch} was {'hit' if hit else 'not hit'}")

pythagoras(3, 2, "?")

print_coverage()

42 changes: 35 additions & 7 deletions algorithms/matrix/rotate_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
Could you do this in-place?
"""

branch_coverage = {
"rotate_matrix_1": False, # if not branch for mat
"rotate_matrix_2": False, # invisible else branch
"rotate_matrix_3": False, # for branch
"rotate_matrix_4": False, # for branch

}


# clockwise rotate
# first reverse up to down, then swap the symmetry
Expand All @@ -16,18 +24,38 @@

def rotate(mat):
if not mat:
branch_coverage["rotate_matrix_1"] = True
return mat

branch_coverage["rotate_matrix_2"] = True

mat.reverse()
for i in range(len(mat)):
branch_coverage["rotate_matrix_3"] = True

for j in range(i):
branch_coverage["rotate_matrix_4"] = True

mat[i][j], mat[j][i] = mat[j][i], mat[i][j]
return mat


if __name__ == "__main__":
mat = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
print(mat)
rotate(mat)
print(mat)
def print_coverage():
for branch, hit in branch_coverage.items():
print(f"{branch} was {'hit' if hit else 'not hit'}")
total = len(branch_coverage)
hit = sum(branch_coverage.values())
result = hit / total * 100

print("The total branch coverage is:", result, "%")


rotate([])
print_coverage()
print("\n")
print_coverage()
rotate([[1, 2], [3, 4]])
print("\n")
print_coverage()
rotate([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("\n")
23 changes: 22 additions & 1 deletion algorithms/search/interpolation_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@

from typing import List

branch_coverage = {
"branch_60": False,
"branch_61": False,
"branch_62": False,
"branch_63": False,
"branch_64": False,

}


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

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

# search_key is found
if array[pos] == search_key:
branch_coverage["branch_61"] = True
return pos

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

# if search_key is smaller, search_key is in lower part
else:
branch_coverage["branch_63"] = True
high = pos - 1


branch_coverage["branch_64"] = True
return -1

def print_coverage():
for branch, hit in branch_coverage.items():
print(f"{branch} was {'hit' if hit else 'not hit'}")



if __name__ == "__main__":
import doctest
doctest.testmod()
print_coverage()

28 changes: 28 additions & 0 deletions algorithms/sort/insertion_sort.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,53 @@
branch_coverage = {
"simulation": False,
"for": False,
"while": False,
"simulation-nested": False,
}

def insertion_sort(arr, simulation=False):
""" Insertion Sort
Complexity: O(n^2)
"""

iteration = 0
if simulation:
branch_coverage["simulation"] = True
print("iteration",iteration,":",*arr)

for i in range(len(arr)):
branch_coverage["for"] = True
cursor = arr[i]
pos = i

while pos > 0 and arr[pos - 1] > cursor:
branch_coverage["while"] = True
# Swap the number down the list
arr[pos] = arr[pos - 1]
pos = pos - 1
# Break and do the final swap
arr[pos] = cursor

if simulation:
branch_coverage["simulation-nested"] = True
iteration = iteration + 1
print("iteration",iteration,":",*arr)

return arr

def print_coverage():
covered = 0
print("branch coverage for `insertion_sort`:")
for branch, hit in branch_coverage.items():
print(f"{branch} was {'hit' if hit else 'not hit'}")
if hit: covered += 1;
print(f"Branch coverage: {covered / len(branch_coverage) * 100}")

insertion_sort([])
insertion_sort([1])
insertion_sort([69, 420])
insertion_sort([420, 69])
insertion_sort([0, 1, 2, 3, 4, 3, 2, 1, 0])
insertion_sort([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], True)
insertion_sort([0, 1, 2, 3, 4, 3, 2, 1, 0], True)
print_coverage()
Loading