Skip to content

Almu #927

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

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open

Almu #927

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
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__*
726 changes: 302 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.

Binary file added SS_befor1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added WB_befor1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 26 additions & 4 deletions algorithms/dp/word_break.py
Original file line number Diff line number Diff line change
@@ -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):
"""
@@ -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
@@ -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()
38 changes: 31 additions & 7 deletions algorithms/sort/stooge_sort.py
Original file line number Diff line number Diff line change
@@ -6,22 +6,30 @@

'''


branch_coverage = {
"check_1": False, # if branch for x > 0
"check_2": False, # else branch
"check_3": False,
"check_4": False,
}

def stoogesort(arr, l, h):
if l >= h:
branch_coverage["check_1"] = True
return

# If first element is smaller
# than last, swap them
if arr[l]>arr[h]:
branch_coverage["check_2"] = True
t = arr[l]
arr[l] = arr[h]
arr[h] = t

# If there are more than 2 elements in
# the array
if h-l + 1 > 2:
branch_coverage["check_3"] = True
t = (int)((h-l + 1)/3)

# Recursively sort first 2 / 3 elements
@@ -33,11 +41,27 @@ def stoogesort(arr, l, h):
# Recursively sort first 2 / 3 elements
# again to confirm
stoogesort(arr, l, (h-t))

branch_coverage["check_4"] = True

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}%")

arr1 = [10, -1, 2, 3, 0]
arr2 = []
result = stoogesort(arr1, 0, len(arr1) - 1)
result = stoogesort(arr1, 0, len(arr2) - 1)
print_coverage()


if __name__ == "__main__":
array = [1,3,64,5,7,8]
n = len(array)
stoogesort(array, 0, n-1)
for i in range(0, n):
print(array[i], end = ' ')
# if __name__ == "__main__":
# array = [1,3,64,5,7,8]
# n = len(array)
# stoogesort(array, 0, n-1)
# for i in range(0, n):
# print(array[i], end = ' ')
23 changes: 23 additions & 0 deletions algorithms/strings/count_binary_substring.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 4 additions & 0 deletions branch-coverage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import subprocess

subprocess.run(["coverage", "run", "--branch", "-m", "pytest", "tests"])
subprocess.run(["coverage", "report"])
Binary file added result_image-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added result_image_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added stoog_sort_image1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added stoog_sort_image2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/test_array.py
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
missing_ranges,
move_zeros,
plus_one_v1, plus_one_v2, plus_one_v3,
remove_duplicates
remove_duplicates,
rotate_v1, rotate_v2, rotate_v3,
summarize_ranges,
three_sum,
13 changes: 12 additions & 1 deletion tests/test_dp.py
Original file line number Diff line number Diff line change
@@ -14,7 +14,8 @@
longest_increasing_subsequence_optimized,
longest_increasing_subsequence_optimized2,
int_divide,find_k_factor,
planting_trees, regex_matching
planting_trees, regex_matching,
word_break,
)


@@ -258,6 +259,16 @@ def test_symbol_2(self):
p = "ab*"
self.assertTrue(regex_matching.is_match(s, p))

class TestWordBreak(unittest.TestCase):
"""Test for interpolation_search and word_break"""

def test_word_break(self):
self.assertTrue(word_break("keonkim", {"keon", "kim"}))
self.assertTrue(word_break("leetcode", {"leet", "code"}))
self.assertFalse(word_break("catsandog", {"cats", "dog", "sand", "and", "cat"}))
self.assertTrue(word_break("applepenapple", {"apple", "pen"}))
self.assertFalse(word_break("pineapplepenapple", {"apple", "pen"}))
self.assertTrue(word_break("aaaaaaa", {"aaaa", "aaa"}))

if __name__ == '__main__':
unittest.main()
25 changes: 23 additions & 2 deletions tests/test_sort.py
Original file line number Diff line number Diff line change
@@ -17,7 +17,8 @@
radix_sort,
gnome_sort,
cocktail_shaker_sort,
top_sort, top_sort_recursive
top_sort, top_sort_recursive,
stoogesort
)

import unittest
@@ -124,7 +125,27 @@ def test_topsort(self):
self.assertTrue(res.index('g') < res.index('e'))
res = top_sort(self.depGraph)
self.assertTrue(res.index('g') < res.index('e'))

class TestStoog(unittest.TestCase):
def test_stoogesort(self):
arr1 = [1, 3, 64, 5, 7, 8]
stoogesort(arr1, 0, len(arr1) - 1)
self.assertEqual(arr1, [1, 3, 5, 7, 8, 64])

arr2 = [5, 4, 3, 2, 1]
stoogesort(arr2, 0, len(arr2) - 1)
self.assertEqual(arr2, [1, 2, 3, 4, 5])

arr3 = [1, 2, 3, 4, 5]
stoogesort(arr3, 0, len(arr3) - 1)
self.assertEqual(arr3, [1, 2, 3, 4, 5])

arr4 = [10, -1, 2, 3, 0]
stoogesort(arr4, 0, len(arr4) - 1)
self.assertEqual(arr4, [-1, 0, 2, 3, 10])

arr5 = []
stoogesort(arr5, 0, len(arr5) - 1)
self.assertEqual(arr5, [])

if __name__ == "__main__":
unittest.main()
Binary file added word_break_image1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added word_break_image2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.