Skip to content

Commit b78b639

Browse files
authored
Merge pull request #101 from BrianLusina/feat/algorithms-graphs-course-schedule
feat(algorithms graphs): course schedule
2 parents 1005257 + 5a5d7a1 commit b78b639

92 files changed

Lines changed: 690 additions & 313 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/pre-commit.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ jobs:
66
pre-commit:
77
runs-on: ubuntu-latest
88
steps:
9-
- uses: actions/checkout@v2
10-
- uses: actions/cache@v2
9+
- uses: actions/checkout@v5
10+
- uses: actions/cache@v4
1111
with:
1212
path: |
1313
~/.cache/pre-commit

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
* [Test Find Duplicate](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/fast_and_slow/find_duplicate/test_find_duplicate.py)
7676
* Happy Number
7777
* [Test Happy Number](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/fast_and_slow/happy_number/test_happy_number.py)
78+
* Graphs
79+
* Course Schedule
80+
* [Test Course Schedule](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/graphs/course_schedule/test_course_schedule.py)
7881
* Greedy
7982
* Min Arrows
8083
* [Test Find Min Arrows](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/greedy/min_arrows/test_find_min_arrows.py)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Python Snippets
22

33
[![Build Status](https://travis-ci.org/BrianLusina/Python_Snippets.svg?branch=master)](https://travis-ci.org/BrianLusina/Python_Snippets)
4+
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/11cfc8e125c54bdb833fe19ed9ddad72)](https://app.codacy.com/gh/BrianLusina/Python_Snippets/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
45

56
Repository for some of my simple [Python](https://www.python.org/ "Python") functions and snippets. Each directory
67
and/or python package has a readme for more information about the Python program

algorithms/arrays/intersection/intersection_two.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
T = TypeVar("T")
44

55

6-
def intersect(list_one: List[T], list_two: List[T], include_duplicates: bool = True) -> List[T]:
6+
def intersect(
7+
list_one: List[T], list_two: List[T], include_duplicates: bool = True
8+
) -> List[T]:
79
"""
810
Given two arrays, find their intersection. This assumes that the lists are not sorted. First sorting takes place
911
on both lists which will incur a cost of O(nlog(n)) + O(mlog(m)) depending on the algorithm used. Time will also
@@ -35,7 +37,10 @@ def intersect(list_one: List[T], list_two: List[T], include_duplicates: bool = T
3537
if include_duplicates:
3638
result.append(first_element)
3739
else:
38-
if pointer_one == 0 or first_element != sorted_list_one[pointer_one - 1]:
40+
if (
41+
pointer_one == 0
42+
or first_element != sorted_list_one[pointer_one - 1]
43+
):
3944
result.append(first_element)
4045
pointer_one += 1
4146
pointer_two += 1

algorithms/arrays/non_constructible_change/test_non_constructible_change.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,12 @@ def test_13(self):
9595
self.assertEqual(actual, expected)
9696

9797
def test_input_not_mutated(self):
98-
"""should not mutate the input coins list"""
99-
coins = [5, 7, 1, 1, 2, 3, 22]
100-
snapshot = coins[:]
101-
_ = non_constructible_change(coins)
102-
self.assertEqual(coins, snapshot)
98+
"""should not mutate the input coins list"""
99+
coins = [5, 7, 1, 1, 2, 3, 22]
100+
snapshot = coins[:]
101+
_ = non_constructible_change(coins)
102+
self.assertEqual(coins, snapshot)
103+
103104

104-
if __name__ == '__main__':
105+
if __name__ == "__main__":
105106
unittest.main()

algorithms/arrays/optimal_task_assignment/test_optimal_task_assignment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ def test_1(self):
1111
self.assertEqual(expected, actual)
1212

1313

14-
if __name__ == '__main__':
14+
if __name__ == "__main__":
1515
unittest.main()

algorithms/arrays/sorted_squared_array/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def sorted_squared_array(array: List[int]) -> List[int]:
2020
left, right = 0, n - 1
2121

2222
# file result array from right to left (largest to smallest squares
23-
for i in range(n-1, -1, -1):
23+
for i in range(n - 1, -1, -1):
2424
left_abs = abs(array[left])
2525
right_abs = abs(array[right])
2626

@@ -56,7 +56,7 @@ def sorted_squared_array_2(array: List[int]) -> List[int]:
5656
left, right = 0, n - 1
5757

5858
# file result array from right to left (largest to smallest squares
59-
for i in range(n-1, -1, -1):
59+
for i in range(n - 1, -1, -1):
6060
left_square = array[left] ** 2
6161
right_square = array[right] ** 2
6262

algorithms/arrays/sorted_squared_array/test_sorted_squared_array.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def test_2(self):
1919

2020
def test_3(self):
2121
"""for an input of [-7,-3,2,3,11] it should return [4,9,9,49,121]"""
22-
input_array = [-7,-3,2,3,11]
23-
expected = [4,9,9,49,121]
22+
input_array = [-7, -3, 2, 3, 11]
23+
expected = [4, 9, 9, 49, 121]
2424
actual = sorted_squared_array(input_array)
2525
self.assertEqual(expected, actual)
2626

@@ -42,11 +42,11 @@ def test_2(self):
4242

4343
def test_3(self):
4444
"""for an input of [-7,-3,2,3,11] it should return [4,9,9,49,121]"""
45-
input_array = [-7,-3,2,3,11]
46-
expected = [4,9,9,49,121]
45+
input_array = [-7, -3, 2, 3, 11]
46+
expected = [4, 9, 9, 49, 121]
4747
actual = sorted_squared_array_2(input_array)
4848
self.assertEqual(expected, actual)
4949

5050

51-
if __name__ == '__main__':
51+
if __name__ == "__main__":
5252
unittest.main()

algorithms/arrays/subsequence/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def is_valid_subsequence(array: List[int], sequence: List[int]) -> bool:
3737

3838
return False
3939

40+
4041
def is_valid_subsequence_v2(array: List[int], sequence: List[int]) -> bool:
4142
"""
4243
Returns true if a sequence is a subsequence of the provided array

algorithms/arrays/subsequence/test_is_valid_subsequence.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,33 @@ def test_1(self):
1212

1313
def test_2(self):
1414
"""should return True for array = [5, 1, 22, 25, 6, -1, 8, 10], sequence = [5, 1, 22, 6, -1, 8, 10]"""
15-
array = [5, 1, 22, 25, 6, -1, 8, 10]
15+
array = [5, 1, 22, 25, 6, -1, 8, 10]
1616
sequence = [5, 1, 22, 6, -1, 8, 10]
1717
actual = is_valid_subsequence(array, sequence)
1818
self.assertTrue(actual)
1919

2020
def test_3(self):
2121
"""should return False for array = [5, 1, 22, 25, 6, -1, 8, 10], sequence = [5, 6, 1, 10, 22, 8, -1, 25]"""
22-
array = [5, 1, 22, 25, 6, -1, 8, 10]
22+
array = [5, 1, 22, 25, 6, -1, 8, 10]
2323
sequence = [5, 6, 1, 10, 22, 8, -1, 25]
2424
actual = is_valid_subsequence(array, sequence)
2525
self.assertFalse(actual)
2626

2727
def test_4(self):
2828
"""should return True for array = [1,2,3,4], sequence = [1,3,4]"""
29-
array = [1,2,3,4]
30-
sequence = [1,3,4]
29+
array = [1, 2, 3, 4]
30+
sequence = [1, 3, 4]
3131
actual = is_valid_subsequence(array, sequence)
3232
self.assertTrue(actual)
3333

3434
def test_5(self):
3535
"""should return True for array = [1,2,3,4], sequence = [2,4]"""
36-
array = [1,2,3,4]
37-
sequence = [2,4]
36+
array = [1, 2, 3, 4]
37+
sequence = [2, 4]
3838
actual = is_valid_subsequence(array, sequence)
3939
self.assertTrue(actual)
4040

41+
4142
class IsValidSubsequenceV2TestCase(unittest.TestCase):
4243
def test_1(self):
4344
"""array = [5, 1, 22, 25, 6, -1, 8, 10], sequence = [1, 6, -1, 10]"""
@@ -48,32 +49,32 @@ def test_1(self):
4849

4950
def test_2(self):
5051
"""should return True for array = [5, 1, 22, 25, 6, -1, 8, 10], sequence = [5, 1, 22, 6, -1, 8, 10]"""
51-
array = [5, 1, 22, 25, 6, -1, 8, 10]
52+
array = [5, 1, 22, 25, 6, -1, 8, 10]
5253
sequence = [5, 1, 22, 6, -1, 8, 10]
5354
actual = is_valid_subsequence_v2(array, sequence)
5455
self.assertTrue(actual)
5556

5657
def test_3(self):
5758
"""should return False for array = [5, 1, 22, 25, 6, -1, 8, 10], sequence = [5, 6, 1, 10, 22, 8, -1, 25]"""
58-
array = [5, 1, 22, 25, 6, -1, 8, 10]
59+
array = [5, 1, 22, 25, 6, -1, 8, 10]
5960
sequence = [5, 6, 1, 10, 22, 8, -1, 25]
6061
actual = is_valid_subsequence_v2(array, sequence)
6162
self.assertFalse(actual)
6263

6364
def test_4(self):
6465
"""should return True for array = [1,2,3,4], sequence = [1,3,4]"""
65-
array = [1,2,3,4]
66-
sequence = [1,3,4]
66+
array = [1, 2, 3, 4]
67+
sequence = [1, 3, 4]
6768
actual = is_valid_subsequence_v2(array, sequence)
6869
self.assertTrue(actual)
6970

7071
def test_5(self):
7172
"""should return True for array = [1,2,3,4], sequence = [2,4]"""
72-
array = [1,2,3,4]
73-
sequence = [2,4]
73+
array = [1, 2, 3, 4]
74+
sequence = [2, 4]
7475
actual = is_valid_subsequence_v2(array, sequence)
7576
self.assertTrue(actual)
7677

7778

78-
if __name__ == '__main__':
79+
if __name__ == "__main__":
7980
unittest.main()

0 commit comments

Comments
 (0)