Skip to content

Commit

Permalink
Merge branch 'master' into Almu
Browse files Browse the repository at this point in the history
  • Loading branch information
MuthanaMustafa authored Jun 26, 2024
2 parents 39e7967 + 0416cab commit 6f2fbd0
Show file tree
Hide file tree
Showing 24 changed files with 389 additions and 29 deletions.
77 changes: 69 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,75 @@ https://github.com/CatalinAnt/algorithms-SEP-95/commit/c16f26e952322b2c1729778a4



#### Ayman Errahmouni

##### Function 1: simplify_path_v2

[Link the commit](https://github.com/CatalinAnt/algorithms-SEP-95/pull/2/commits/22ee6fa1df4785596c603af61a725c558973eb0b)

Screenshot of branch measurement (66%):<br>
![image](image-7.png)

##### Function 2: insertion_sort

[Link to commit](https://github.com/CatalinAnt/algorithms-SEP-95/pull/2/commits/5dae7f28036f89b7f6ff673639a922dd714aff3e)

Screenshot of branch measurement (0%, was untested):<br>
![alt text](image-8.png)

#### Catalin Antonescu

##### Function 1: strong_password

Link to commit:
[https://github.com/CatalinAnt/algorithms-SEP-95/commit/eaad6d32ecd73bb8fde876a4d4852cb522aea6f8](https://github.com/CatalinAnt/algorithms-SEP-95/commit/2b0b9187c1c040e4476b1ca14f2c2249273566b7)

Screenshot of branch measurement:
![image](https://github.com/CatalinAnt/algorithms-SEP-95/assets/113595149/e718a47f-5ea0-412c-b250-25a193412164)

##### Function 2: rotate_image

Link to commit:(same as for the first one)
[https://github.com/CatalinAnt/algorithms-SEP-95/commit/eaad6d32ecd73bb8fde876a4d4852cb522aea6f8](https://github.com/CatalinAnt/algorithms-SEP-95/commit/2b0b9187c1c040e4476b1ca14f2c2249273566b7)

Screenshot of branch measurement:
![image](https://github.com/CatalinAnt/algorithms-SEP-95/assets/113595149/94eec9b6-3dd6-46e3-b087-40892eccc10e)

#### Abdullah Abdelkhalik


pythagoras

https://github.com/CatalinAnt/algorithms-SEP-95/commit/5651abafebe8ae3a5ea63e74883bb991acf19303

![pythagoras_hits](https://github.com/CatalinAnt/algorithms-SEP-95/assets/114078193/0df1fa2b-2185-4b9f-ae65-5d969edb009b)


first_unique_char

https://github.com/CatalinAnt/algorithms-SEP-95/commit/c16f26e952322b2c1729778a4141a57103ba7658

![first_unique_char_hits](https://github.com/CatalinAnt/algorithms-SEP-95/assets/114078193/10d7c45c-398e-4408-8f11-6771f51fa95c)


#### Abdullah Abdelkhalik


pythagoras

https://github.com/CatalinAnt/algorithms-SEP-95/commit/5651abafebe8ae3a5ea63e74883bb991acf19303

![pythagoras_hits](https://github.com/CatalinAnt/algorithms-SEP-95/assets/114078193/0df1fa2b-2185-4b9f-ae65-5d969edb009b)


first_unique_char

https://github.com/CatalinAnt/algorithms-SEP-95/commit/c16f26e952322b2c1729778a4141a57103ba7658

![first_unique_char_hits](https://github.com/CatalinAnt/algorithms-SEP-95/assets/114078193/10d7c45c-398e-4408-8f11-6771f51fa95c)



## Coverage improvement

### Individual tests
Expand Down Expand Up @@ -174,7 +243,6 @@ New coverage:

For strong_password there was a 26% coverage improvement with the existing tool and 40% with manual measurement tool.


Test 2:

In test_matrix:
Expand Down Expand Up @@ -216,7 +284,6 @@ https://github.com/CatalinAnt/algorithms-SEP-95/commit/5651abafebe8ae3a5ea63e748

The coverage is improved by 13%, the code only hit three out of five branches and only set up two examples. I added a case where there is no unique letter.


## Almuthana Almustafa

stoogsort in stoog_sort.py
Expand Down Expand Up @@ -291,9 +358,3 @@ Due to the large size of the project, the percentage only went up by one percent
Abdullah -> increased the coverage for two functions.
Almuthana Almustafa -> Instrumentation was added to two functions, and test cases were created for them to improve 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()

17 changes: 17 additions & 0 deletions algorithms/sort/insertion_sort.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
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():
print("branch coverage for `insertion_sort`:")
for branch, hit in branch_coverage.items():
print(f"{branch} was {'hit' if hit else 'not hit'}")

Loading

0 comments on commit 6f2fbd0

Please sign in to comment.