Skip to content
Open
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
39 changes: 39 additions & 0 deletions data_structures/arrays/dutch_national_flag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
def dutch_national_flag(arr: list[int]) -> list[int]:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/dutch_national_flag.py, please provide doctest for the function dutch_national_flag

"""
Sorts an array containing only 0s, 1s and 2s

Args:
arr(list[int]): The input array (containing only 0s, 1s and 2s)

Returns:
list[int]: Sorted array

Examples:
>>> dutch_national_flag([2, 0, 2, 1, 2, 0, 1])
[0, 0, 1, 1, 2, 2, 2]
>>> dutch_national_flag([0, 1, 2, 0, 1, 2])
[0, 0, 1, 1, 2, 2]
>>> dutch_national_flag([1, 1, 1])
[1, 1, 1]
>>> dutch_national_flag([])
[]
"""

low, mid, high = 0, 0, len(arr) - 1

while mid <= high:
if arr[mid] == 0:
arr[low], arr[mid] = arr[mid], arr[low]
low += 1
mid += 1
elif arr[mid] == 1:
mid += 1
else:
arr[mid], arr[high] = arr[high], arr[mid]
high -= 1
return arr


if __name__ == "__main__":
arr = [2, 0, 2, 1, 2, 0, 1]
print("Sorted array: ", dutch_national_flag(arr))
8 changes: 5 additions & 3 deletions greedy_methods/fractional_knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ def frac_knapsack(vl, wt, w, n):
return (
0
if k == 0
else sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
if k != n
else sum(vl[:k])
else (
sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
if k != n
else sum(vl[:k])
)
)


Expand Down
8 changes: 5 additions & 3 deletions matrix/matrix_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,11 @@ def cofactors(self) -> Matrix:
return Matrix(
[
[
self.minors().rows[row][column]
if (row + column) % 2 == 0
else self.minors().rows[row][column] * -1
(
self.minors().rows[row][column]
if (row + column) % 2 == 0
else self.minors().rows[row][column] * -1
)
for column in range(self.minors().num_columns)
]
for row in range(self.minors().num_rows)
Expand Down