From abe29f6b5b4a3c6ba11cafa75dbdbdefaa2b892d Mon Sep 17 00:00:00 2001 From: Sangram Das Date: Sun, 5 Oct 2025 18:26:59 +0530 Subject: [PATCH 1/8] add a new reverse-linked-list --- .../linked_list/reverse_linked_list.py | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 data_structures/linked_list/reverse_linked_list.py diff --git a/data_structures/linked_list/reverse_linked_list.py b/data_structures/linked_list/reverse_linked_list.py new file mode 100644 index 000000000000..c6c9e4562de5 --- /dev/null +++ b/data_structures/linked_list/reverse_linked_list.py @@ -0,0 +1,53 @@ +""" +https://www.enjoyalgorithms.com/blog/reverse-linked-list +""" + +class ListNode: + """Definition for singly-linked list.""" + def __init__(self, val=0, next=None): + self.val = val + self.next = next + + +def reverse_linked_list(head: ListNode) -> ListNode: + """ + Reverse a singly linked list. + + Args: + head: The head node of the linked list. + + Returns: + The new head node of the reversed linked list. + + Examples: + >>> a = ListNode(1) + >>> b = ListNode(2) + >>> c = ListNode(3) + >>> a.next, b.next = b, c + >>> head = reverse_linked_list(a) + >>> [head.val, head.next.val, head.next.next.val] + [3, 2, 1] + """ + prev = None + current = head + while current: + nxt = current.next + current.next = prev + prev = current + current = nxt + return prev + + +if __name__ == "__main__": + import doctest + doctest.testmod() + + # Example execution + a = ListNode(1) + b = ListNode(2) + c = ListNode(3) + a.next, b.next = b, c + + print("Original Linked List: 1 -> 2 -> 3") + new_head = reverse_linked_list(a) + print(f"Reversed Linked List: {new_head.val} -> {new_head.next.val} -> {new_head.next.next.val}") From a3138a84a27876c7e0db969eddb14de0b1559ed1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 5 Oct 2025 13:05:00 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/linked_list/reverse_linked_list.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/data_structures/linked_list/reverse_linked_list.py b/data_structures/linked_list/reverse_linked_list.py index c6c9e4562de5..4b78f9969b7e 100644 --- a/data_structures/linked_list/reverse_linked_list.py +++ b/data_structures/linked_list/reverse_linked_list.py @@ -2,8 +2,10 @@ https://www.enjoyalgorithms.com/blog/reverse-linked-list """ + class ListNode: """Definition for singly-linked list.""" + def __init__(self, val=0, next=None): self.val = val self.next = next @@ -40,6 +42,7 @@ def reverse_linked_list(head: ListNode) -> ListNode: if __name__ == "__main__": import doctest + doctest.testmod() # Example execution @@ -50,4 +53,6 @@ def reverse_linked_list(head: ListNode) -> ListNode: print("Original Linked List: 1 -> 2 -> 3") new_head = reverse_linked_list(a) - print(f"Reversed Linked List: {new_head.val} -> {new_head.next.val} -> {new_head.next.next.val}") + print( + f"Reversed Linked List: {new_head.val} -> {new_head.next.val} -> {new_head.next.next.val}" + ) From 27f956c8a7a502abaf70349931e73a9b08d6e5a7 Mon Sep 17 00:00:00 2001 From: Sangram Das Date: Mon, 6 Oct 2025 00:18:57 +0530 Subject: [PATCH 3/8] new median of two sorted arrays --- .../arrays/median_of_two_sorted_arrays.py | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 data_structures/arrays/median_of_two_sorted_arrays.py diff --git a/data_structures/arrays/median_of_two_sorted_arrays.py b/data_structures/arrays/median_of_two_sorted_arrays.py new file mode 100644 index 000000000000..45a45a65b114 --- /dev/null +++ b/data_structures/arrays/median_of_two_sorted_arrays.py @@ -0,0 +1,59 @@ +""" +https://www.enjoyalgorithms.com/blog/median-of-two-sorted-arrays +""" + +def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: + """ + Find the median of two arrays. + + Args: + nums1: The first array. + nums2: The second array. + + Returns: + The median of the two arrays. + + Examples: + >>> find_median_sorted_arrays([1, 3], [2]) + 2.0 + + >>> find_median_sorted_arrays([1, 2], [3, 4]) + 2.5 + + >>> find_median_sorted_arrays([0, 0], [0, 0]) + 0.0 + + >>> find_median_sorted_arrays([], []) + Traceback (most recent call last): + ... + ValueError: Both input arrays are empty. + + >>> find_median_sorted_arrays([], [1]) + 1.0 + + >>> find_median_sorted_arrays([-1000], [1000]) + 0.0 + + >>> find_median_sorted_arrays([-1.1, -2.2], [-3.3, -4.4]) + -2.75 + """ + if not nums1 and not nums2: + raise ValueError("Both input arrays are empty.") + + # Merge the arrays into a single sorted array. + merged = sorted(nums1 + nums2) + total = len(merged) + + if total % 2 == 1: # If the total number of elements is odd + return float(merged[total // 2]) # then return the middle element + + # If the total number of elements is even, calculate + # the average of the two middle elements as the median. + middle1 = merged[total // 2 - 1] + middle2 = merged[total // 2] + return (float(middle1) + float(middle2)) / 2.0 + + +if __name__ == "__main__": + import doctest + doctest.testmod() From 3320f4cb11982190ef9e7a82c002c6e978dbf8c4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 5 Oct 2025 18:50:15 +0000 Subject: [PATCH 4/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/median_of_two_sorted_arrays.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data_structures/arrays/median_of_two_sorted_arrays.py b/data_structures/arrays/median_of_two_sorted_arrays.py index 45a45a65b114..972b0ee44201 100644 --- a/data_structures/arrays/median_of_two_sorted_arrays.py +++ b/data_structures/arrays/median_of_two_sorted_arrays.py @@ -2,6 +2,7 @@ https://www.enjoyalgorithms.com/blog/median-of-two-sorted-arrays """ + def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: """ Find the median of two arrays. @@ -56,4 +57,5 @@ def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: if __name__ == "__main__": import doctest + doctest.testmod() From e25d0e9b3014023844b514387cc90fc77f00c5fe Mon Sep 17 00:00:00 2001 From: Sangram Das Date: Mon, 6 Oct 2025 00:26:01 +0530 Subject: [PATCH 5/8] add a new algo in the number_theory --- .../median_of_two_sorted_arrays.py | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 data_structures/hashing/number_theory/median_of_two_sorted_arrays.py diff --git a/data_structures/hashing/number_theory/median_of_two_sorted_arrays.py b/data_structures/hashing/number_theory/median_of_two_sorted_arrays.py new file mode 100644 index 000000000000..45a45a65b114 --- /dev/null +++ b/data_structures/hashing/number_theory/median_of_two_sorted_arrays.py @@ -0,0 +1,59 @@ +""" +https://www.enjoyalgorithms.com/blog/median-of-two-sorted-arrays +""" + +def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: + """ + Find the median of two arrays. + + Args: + nums1: The first array. + nums2: The second array. + + Returns: + The median of the two arrays. + + Examples: + >>> find_median_sorted_arrays([1, 3], [2]) + 2.0 + + >>> find_median_sorted_arrays([1, 2], [3, 4]) + 2.5 + + >>> find_median_sorted_arrays([0, 0], [0, 0]) + 0.0 + + >>> find_median_sorted_arrays([], []) + Traceback (most recent call last): + ... + ValueError: Both input arrays are empty. + + >>> find_median_sorted_arrays([], [1]) + 1.0 + + >>> find_median_sorted_arrays([-1000], [1000]) + 0.0 + + >>> find_median_sorted_arrays([-1.1, -2.2], [-3.3, -4.4]) + -2.75 + """ + if not nums1 and not nums2: + raise ValueError("Both input arrays are empty.") + + # Merge the arrays into a single sorted array. + merged = sorted(nums1 + nums2) + total = len(merged) + + if total % 2 == 1: # If the total number of elements is odd + return float(merged[total // 2]) # then return the middle element + + # If the total number of elements is even, calculate + # the average of the two middle elements as the median. + middle1 = merged[total // 2 - 1] + middle2 = merged[total // 2] + return (float(middle1) + float(middle2)) / 2.0 + + +if __name__ == "__main__": + import doctest + doctest.testmod() From fb195d28fea01b6a724eaec31eb237d6fd82616d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 5 Oct 2025 18:56:29 +0000 Subject: [PATCH 6/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../hashing/number_theory/median_of_two_sorted_arrays.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data_structures/hashing/number_theory/median_of_two_sorted_arrays.py b/data_structures/hashing/number_theory/median_of_two_sorted_arrays.py index 45a45a65b114..972b0ee44201 100644 --- a/data_structures/hashing/number_theory/median_of_two_sorted_arrays.py +++ b/data_structures/hashing/number_theory/median_of_two_sorted_arrays.py @@ -2,6 +2,7 @@ https://www.enjoyalgorithms.com/blog/median-of-two-sorted-arrays """ + def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: """ Find the median of two arrays. @@ -56,4 +57,5 @@ def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: if __name__ == "__main__": import doctest + doctest.testmod() From 5650e7da35021b4bf037d4a0b553be0a310a5a4e Mon Sep 17 00:00:00 2001 From: Sangram Das Date: Mon, 6 Oct 2025 00:36:14 +0530 Subject: [PATCH 7/8] add three algo in the array section --- data_structures/arrays/majority_element.py | 35 ++++++++++++++++++++++ data_structures/arrays/maximum_subarray.py | 34 +++++++++++++++++++++ data_structures/arrays/rotate_array.py | 31 +++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 data_structures/arrays/majority_element.py create mode 100644 data_structures/arrays/maximum_subarray.py create mode 100644 data_structures/arrays/rotate_array.py diff --git a/data_structures/arrays/majority_element.py b/data_structures/arrays/majority_element.py new file mode 100644 index 000000000000..6d41f12fc675 --- /dev/null +++ b/data_structures/arrays/majority_element.py @@ -0,0 +1,35 @@ +""" +Find the majority element in an array. +""" + +def majority_element(nums: list[int]) -> int: + """ + Find the element that appears more than n/2 times using + Boyer-Moore Voting Algorithm. + + Args: + nums: List of integers. + + Returns: + The majority element. + + Examples: + >>> majority_element([3, 2, 3]) + 3 + >>> majority_element([2, 2, 1, 1, 1, 2, 2]) + 2 + """ + count = 0 + candidate = None + + for num in nums: + if count == 0: + candidate = num + count += (1 if num == candidate else -1) + + return candidate + + +if __name__ == "__main__": + import doctest + doctest.testmod() diff --git a/data_structures/arrays/maximum_subarray.py b/data_structures/arrays/maximum_subarray.py new file mode 100644 index 000000000000..9fff56cca2b8 --- /dev/null +++ b/data_structures/arrays/maximum_subarray.py @@ -0,0 +1,34 @@ +""" +Find the maximum subarray sum (Kadane's Algorithm). +""" + +def max_subarray(nums: list[int]) -> int: + """ + Find the contiguous subarray with the largest sum. + + Args: + nums: List of integers. + + Returns: + Maximum sum of subarray. + + Examples: + >>> max_subarray([-2,1,-3,4,-1,2,1,-5,4]) + 6 + >>> max_subarray([1]) + 1 + >>> max_subarray([5,4,-1,7,8]) + 23 + """ + max_sum = current_sum = nums[0] + + for num in nums[1:]: + current_sum = max(num, current_sum + num) + max_sum = max(max_sum, current_sum) + + return max_sum + + +if __name__ == "__main__": + import doctest + doctest.testmod() diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py new file mode 100644 index 000000000000..01333c1c526b --- /dev/null +++ b/data_structures/arrays/rotate_array.py @@ -0,0 +1,31 @@ +""" +Rotate an array to the right by k steps. +""" + +def rotate(nums: list[int], k: int) -> list[int]: + """ + Rotate the array to the right by k steps. + + Args: + nums: List of integers. + k: Number of steps to rotate. + + Returns: + The rotated list. + + Examples: + >>> rotate([1,2,3,4,5,6,7], 3) + [5, 6, 7, 1, 2, 3, 4] + + >>> rotate([-1,-100,3,99], 2) + [3, 99, -1, -100] + """ + n = len(nums) + k = k % n # In case k > n + nums[:] = nums[-k:] + nums[:-k] + return nums + + +if __name__ == "__main__": + import doctest + doctest.testmod() From 43530a101dd4e63a5e83be41ab91d097c47a6822 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 5 Oct 2025 19:06:39 +0000 Subject: [PATCH 8/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/majority_element.py | 4 +++- data_structures/arrays/maximum_subarray.py | 2 ++ data_structures/arrays/rotate_array.py | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/majority_element.py b/data_structures/arrays/majority_element.py index 6d41f12fc675..a03edfb198b2 100644 --- a/data_structures/arrays/majority_element.py +++ b/data_structures/arrays/majority_element.py @@ -2,6 +2,7 @@ Find the majority element in an array. """ + def majority_element(nums: list[int]) -> int: """ Find the element that appears more than n/2 times using @@ -25,11 +26,12 @@ def majority_element(nums: list[int]) -> int: for num in nums: if count == 0: candidate = num - count += (1 if num == candidate else -1) + count += 1 if num == candidate else -1 return candidate if __name__ == "__main__": import doctest + doctest.testmod() diff --git a/data_structures/arrays/maximum_subarray.py b/data_structures/arrays/maximum_subarray.py index 9fff56cca2b8..a45a62e7e688 100644 --- a/data_structures/arrays/maximum_subarray.py +++ b/data_structures/arrays/maximum_subarray.py @@ -2,6 +2,7 @@ Find the maximum subarray sum (Kadane's Algorithm). """ + def max_subarray(nums: list[int]) -> int: """ Find the contiguous subarray with the largest sum. @@ -31,4 +32,5 @@ def max_subarray(nums: list[int]) -> int: if __name__ == "__main__": import doctest + doctest.testmod() diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py index 01333c1c526b..a2951f8823e8 100644 --- a/data_structures/arrays/rotate_array.py +++ b/data_structures/arrays/rotate_array.py @@ -2,6 +2,7 @@ Rotate an array to the right by k steps. """ + def rotate(nums: list[int], k: int) -> list[int]: """ Rotate the array to the right by k steps. @@ -28,4 +29,5 @@ def rotate(nums: list[int], k: int) -> list[int]: if __name__ == "__main__": import doctest + doctest.testmod()