Skip to content
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
4 changes: 2 additions & 2 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
- uses: actions/checkout@v5
- uses: actions/cache@v4
with:
path: |
~/.cache/pre-commit
Expand Down
3 changes: 3 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
* [Test Find Duplicate](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/fast_and_slow/find_duplicate/test_find_duplicate.py)
* Happy Number
* [Test Happy Number](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/fast_and_slow/happy_number/test_happy_number.py)
* Graphs
* Course Schedule
* [Test Course Schedule](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/graphs/course_schedule/test_course_schedule.py)
* Greedy
* Min Arrows
* [Test Find Min Arrows](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/greedy/min_arrows/test_find_min_arrows.py)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Python Snippets

[![Build Status](https://travis-ci.org/BrianLusina/Python_Snippets.svg?branch=master)](https://travis-ci.org/BrianLusina/Python_Snippets)
[![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)

Repository for some of my simple [Python](https://www.python.org/ "Python") functions and snippets. Each directory
and/or python package has a readme for more information about the Python program
Expand Down
9 changes: 7 additions & 2 deletions algorithms/arrays/intersection/intersection_two.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
T = TypeVar("T")


def intersect(list_one: List[T], list_two: List[T], include_duplicates: bool = True) -> List[T]:
def intersect(
list_one: List[T], list_two: List[T], include_duplicates: bool = True
) -> List[T]:
"""
Given two arrays, find their intersection. This assumes that the lists are not sorted. First sorting takes place
on both lists which will incur a cost of O(nlog(n)) + O(mlog(m)) depending on the algorithm used. Time will also
Expand Down Expand Up @@ -35,7 +37,10 @@ def intersect(list_one: List[T], list_two: List[T], include_duplicates: bool = T
if include_duplicates:
result.append(first_element)
else:
if pointer_one == 0 or first_element != sorted_list_one[pointer_one - 1]:
if (
pointer_one == 0
or first_element != sorted_list_one[pointer_one - 1]
):
result.append(first_element)
pointer_one += 1
pointer_two += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ def test_13(self):
self.assertEqual(actual, expected)

def test_input_not_mutated(self):
"""should not mutate the input coins list"""
coins = [5, 7, 1, 1, 2, 3, 22]
snapshot = coins[:]
_ = non_constructible_change(coins)
self.assertEqual(coins, snapshot)
"""should not mutate the input coins list"""
coins = [5, 7, 1, 1, 2, 3, 22]
snapshot = coins[:]
_ = non_constructible_change(coins)
self.assertEqual(coins, snapshot)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ def test_1(self):
self.assertEqual(expected, actual)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
4 changes: 2 additions & 2 deletions algorithms/arrays/sorted_squared_array/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def sorted_squared_array(array: List[int]) -> List[int]:
left, right = 0, n - 1

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def test_2(self):

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

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

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


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
1 change: 1 addition & 0 deletions algorithms/arrays/subsequence/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def is_valid_subsequence(array: List[int], sequence: List[int]) -> bool:

return False


def is_valid_subsequence_v2(array: List[int], sequence: List[int]) -> bool:
"""
Returns true if a sequence is a subsequence of the provided array
Expand Down
27 changes: 14 additions & 13 deletions algorithms/arrays/subsequence/test_is_valid_subsequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,33 @@ def test_1(self):

def test_2(self):
"""should return True for array = [5, 1, 22, 25, 6, -1, 8, 10], sequence = [5, 1, 22, 6, -1, 8, 10]"""
array = [5, 1, 22, 25, 6, -1, 8, 10]
array = [5, 1, 22, 25, 6, -1, 8, 10]
sequence = [5, 1, 22, 6, -1, 8, 10]
actual = is_valid_subsequence(array, sequence)
self.assertTrue(actual)

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

def test_4(self):
"""should return True for array = [1,2,3,4], sequence = [1,3,4]"""
array = [1,2,3,4]
sequence = [1,3,4]
array = [1, 2, 3, 4]
sequence = [1, 3, 4]
actual = is_valid_subsequence(array, sequence)
self.assertTrue(actual)

def test_5(self):
"""should return True for array = [1,2,3,4], sequence = [2,4]"""
array = [1,2,3,4]
sequence = [2,4]
array = [1, 2, 3, 4]
sequence = [2, 4]
actual = is_valid_subsequence(array, sequence)
self.assertTrue(actual)


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

def test_2(self):
"""should return True for array = [5, 1, 22, 25, 6, -1, 8, 10], sequence = [5, 1, 22, 6, -1, 8, 10]"""
array = [5, 1, 22, 25, 6, -1, 8, 10]
array = [5, 1, 22, 25, 6, -1, 8, 10]
sequence = [5, 1, 22, 6, -1, 8, 10]
actual = is_valid_subsequence_v2(array, sequence)
self.assertTrue(actual)

def test_3(self):
"""should return False for array = [5, 1, 22, 25, 6, -1, 8, 10], sequence = [5, 6, 1, 10, 22, 8, -1, 25]"""
array = [5, 1, 22, 25, 6, -1, 8, 10]
array = [5, 1, 22, 25, 6, -1, 8, 10]
sequence = [5, 6, 1, 10, 22, 8, -1, 25]
actual = is_valid_subsequence_v2(array, sequence)
self.assertFalse(actual)

def test_4(self):
"""should return True for array = [1,2,3,4], sequence = [1,3,4]"""
array = [1,2,3,4]
sequence = [1,3,4]
array = [1, 2, 3, 4]
sequence = [1, 3, 4]
actual = is_valid_subsequence_v2(array, sequence)
self.assertTrue(actual)

def test_5(self):
"""should return True for array = [1,2,3,4], sequence = [2,4]"""
array = [1,2,3,4]
sequence = [2,4]
array = [1, 2, 3, 4]
sequence = [2, 4]
actual = is_valid_subsequence_v2(array, sequence)
self.assertTrue(actual)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class CircularArrayLoopTestCase(unittest.TestCase):
def test_1(self):
"""should return True for [3,1,2]"""
nums = [3,1,2]
nums = [3, 1, 2]
actual = circular_array_loop(nums)
self.assertTrue(actual)

Expand All @@ -17,46 +17,46 @@ def test_2(self):

def test_3(self):
"""should return False for [2,1,-1,-2]"""
nums = [2,1,-1,-2]
nums = [2, 1, -1, -2]
actual = circular_array_loop(nums)
self.assertFalse(actual)

def test_4(self):
"""should return True for [3,-3,1,1]"""
nums = [3,-3,1,1]
nums = [3, -3, 1, 1]
actual = circular_array_loop(nums)
self.assertTrue(actual)

def test_5(self):
"""should return True for [1,3,-2,-4,1]"""
nums = [1,3,-2,-4,1]
nums = [1, 3, -2, -4, 1]
actual = circular_array_loop(nums)
self.assertTrue(actual)

def test_6(self):
"""should return True for [2,1,-1,-2]"""
nums = [2,1,-1,-2]
nums = [2, 1, -1, -2]
actual = circular_array_loop(nums)
self.assertFalse(actual)

def test_7(self):
"""should return True for [5,4,-2,-1,3]"""
nums = [5,4,-2,-1,3]
nums = [5, 4, -2, -1, 3]
actual = circular_array_loop(nums)
self.assertFalse(actual)

def test_8(self):
"""should return True for [1,2,-3,3,4,7,1]"""
nums = [1,2,-3,3,4,7,1]
nums = [1, 2, -3, 3, 4, 7, 1]
actual = circular_array_loop(nums)
self.assertTrue(actual)

def test_9(self):
"""should return True for [3,3,1,-1,2]"""
nums = [3,3,1,-1,2]
nums = [3, 3, 1, -1, 2]
actual = circular_array_loop(nums)
self.assertTrue(actual)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_10(self):

def test_11(self):
"""nums = [1,3,4,2,2] should return 2"""
nums = [1,3,4,2,2]
nums = [1, 3, 4, 2, 2]
expected = 2

actual = find_duplicate_floyd_algo(nums)
Expand All @@ -133,7 +133,7 @@ def test_11(self):

def test_12(self):
"""nums = [1,3,6,2,7,3,5,4] should return 3"""
nums = [1,3,6,2,7,3,5,4]
nums = [1, 3, 6, 2, 7, 3, 5, 4]
expected = 3

actual = find_duplicate_floyd_algo(nums)
Expand All @@ -142,7 +142,7 @@ def test_12(self):

def test_13(self):
"""nums = [1,2,2] should return 2"""
nums = [1,2,2]
nums = [1, 2, 2]
expected = 2

actual = find_duplicate_floyd_algo(nums)
Expand Down
1 change: 1 addition & 0 deletions algorithms/fast_and_slow/happy_number/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def get_next(number: int) -> int:

return n == 1


def is_happy_number_2(n: int) -> bool:
"""
Checks if an unsigned integer is a happy number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def test_9(self):
actual = is_happy_number(number)
self.assertFalse(actual)


class HappyNumberTestCase2(unittest.TestCase):
def test_1(self):
"""should return true for 23"""
Expand Down Expand Up @@ -113,5 +114,5 @@ def test_9(self):
self.assertFalse(actual)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
Loading
Loading