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
4 changes: 1 addition & 3 deletions data_structures/disjoint_set/alternate_disjoint_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@


class DisjointSet:
def __init__(self, set_counts: list) -> None:
def __init__(self, set_counts: list[int]) -> None:
"""
Initialize with a list of the number of items in each set
and with rank = 1 for each set
"""
self.set_counts = set_counts
self.max_set = max(set_counts)
num_sets = len(set_counts)
self.ranks = [1] * num_sets
self.parents = list(range(num_sets))
Expand Down Expand Up @@ -46,9 +45,8 @@
self.set_counts[src_parent] += self.set_counts[dst_parent]
self.set_counts[dst_parent] = 0
self.parents[dst_parent] = src_parent
joined_set_size = self.set_counts[src_parent]

Check failure on line 48 in data_structures/disjoint_set/alternate_disjoint_set.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (F841)

data_structures/disjoint_set/alternate_disjoint_set.py:48:13: F841 Local variable `joined_set_size` is assigned to but never used help: Remove assignment to unused variable `joined_set_size`

self.max_set = max(self.max_set, joined_set_size)
return True

def get_parent(self, disj_set: int) -> int:
Expand Down
4 changes: 4 additions & 0 deletions searches/double_linear_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ def double_linear_search(array: list[int], search_item: int) -> int:
-1
>>> double_linear_search([1, 5, 5, 10], 10)
3
>>> double_linear_search([], 1)
-1
"""
if not array:
return -1
# define the start and end index of the given array
start_ind, end_ind = 0, len(array) - 1
while start_ind <= end_ind:
Expand Down
7 changes: 4 additions & 3 deletions searches/double_linear_search_recursion.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
def search(list_data: list, key: int, left: int = 0, right: int = 0) -> int:
def search(list_data: list, key: int, left: int = 0, right: int | None = None) -> int:
"""
Iterate through the array to find the index of key using recursion.
:param list_data: the list to be searched
:param key: the key to be searched
:param left: the index of first element
:param right: the index of last element
:param right: the index of last element (defaults to len(list_data) - 1)
:return: the index of key value if found, -1 otherwise.

>>> search(list(range(0, 11)), 5)
Expand All @@ -18,7 +18,8 @@ def search(list_data: list, key: int, left: int = 0, right: int = 0) -> int:
>>> search([], 1)
-1
"""
right = right or len(list_data) - 1
if right is None:
right = len(list_data) - 1
if left > right:
return -1
elif list_data[left] == key:
Expand Down
4 changes: 3 additions & 1 deletion searches/sentinel_linear_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"""


def sentinel_linear_search(sequence, target):
def sentinel_linear_search(sequence: list, target) -> int | None:
"""Pure implementation of sentinel linear search algorithm in Python
:param sequence: some sequence with comparable items
Expand All @@ -31,6 +31,8 @@ def sentinel_linear_search(sequence, target):
>>> sentinel_linear_search([0, 5, 7, 10, 15], 6)
"""
# Create a copy to avoid mutating the input sequence
sequence = sequence.copy()
sequence.append(target)

index = 0
Expand Down
5 changes: 4 additions & 1 deletion strings/remove_duplicate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
def remove_duplicates(sentence: str) -> str:
"""
Remove duplicates from sentence
Remove duplicate words from sentence and return sorted unique words.

Note: The output words are sorted alphabetically, not in original order.

>>> remove_duplicates("Python is great and Java is also great")
'Java Python also and great is'
>>> remove_duplicates("Python is great and Java is also great")
Expand Down
Loading