From 37f5771699501d954277bd337c5ed6e2c6860105 Mon Sep 17 00:00:00 2001 From: ShivaDahal99 <130563462+ShivaDahal99@users.noreply.github.com> Date: Mon, 22 May 2023 19:19:07 +0200 Subject: [PATCH 1/9] Create TestShiva --- TestShiva | 1 + 1 file changed, 1 insertion(+) create mode 100644 TestShiva diff --git a/TestShiva b/TestShiva new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/TestShiva @@ -0,0 +1 @@ + From baf8d49b81cb576cad1821e05ad11240110958e0 Mon Sep 17 00:00:00 2001 From: ShivaDahal99 <130563462+ShivaDahal99@users.noreply.github.com> Date: Mon, 22 May 2023 19:19:40 +0200 Subject: [PATCH 2/9] Delete TestShiva --- TestShiva | 1 - 1 file changed, 1 deletion(-) delete mode 100644 TestShiva diff --git a/TestShiva b/TestShiva deleted file mode 100644 index 8b137891791f..000000000000 --- a/TestShiva +++ /dev/null @@ -1 +0,0 @@ - From e8c960a1af72223328b46ae986137c3982399c68 Mon Sep 17 00:00:00 2001 From: "e.abouelkomsan" Date: Wed, 7 Jun 2023 21:20:52 +0200 Subject: [PATCH 3/9] Add the maximum subarray sum algorithm --- dynamic_programming/max_sub_array_sum.py | 44 ++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 dynamic_programming/max_sub_array_sum.py diff --git a/dynamic_programming/max_sub_array_sum.py b/dynamic_programming/max_sub_array_sum.py new file mode 100644 index 000000000000..20f935297661 --- /dev/null +++ b/dynamic_programming/max_sub_array_sum.py @@ -0,0 +1,44 @@ +from typing import List + +def max_sub_array_sum(arr: List[int], size: int) -> int: + """ + Finds the maximum sum of a subarray within the given array using Kadane's algorithm. + + Args: + arr (list): The input array of numbers. + size (int): The size of the array. + + Returns: + int: The maximum sum of a subarray within the array. + + Example: + >>> arr = [-2, -3, 4, -1, -2, 5, -3] + >>> max_sub_array_sum(arr, len(arr)) + 6 + In this example, the input array is [-2, -3, 4, -1, -2, 5, -3]. The maximum sum of a subarray + within this array is 6, which corresponds to the subarray [4, -1, -2, 5]. + + >>> arr = [-3, -4, 5, -1, 2, -4, 6, -1] + >>> max_sub_array_sum(arr, len(arr)) + 8 + + References: + https://en.wikipedia.org/wiki/Maximum_subarray_problem + """ + max_till_now = arr[0] + max_ending = 0 + + for i in range(size): + max_ending = max_ending + arr[i] + if max_ending < 0: + max_ending = 0 + elif max_till_now < max_ending: + max_till_now = max_ending + + return max_till_now + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 0681a2c4a0177db6fde6d2d111646a66b2bed21c Mon Sep 17 00:00:00 2001 From: "e.abouelkomsan" Date: Wed, 7 Jun 2023 21:23:13 +0200 Subject: [PATCH 4/9] Add the sliding window algorithm --- web_programming/sliding_window.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 web_programming/sliding_window.py diff --git a/web_programming/sliding_window.py b/web_programming/sliding_window.py new file mode 100644 index 000000000000..e69de29bb2d1 From bb8d9926284900e8f78e07159a6a2c3b6fd4afc3 Mon Sep 17 00:00:00 2001 From: "e.abouelkomsan" Date: Wed, 7 Jun 2023 21:25:18 +0200 Subject: [PATCH 5/9] Add the sliding window algorithm using generators --- .../sliding_window_using_generators.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 web_programming/sliding_window_using_generators.py diff --git a/web_programming/sliding_window_using_generators.py b/web_programming/sliding_window_using_generators.py new file mode 100644 index 000000000000..bb6af3e8cb9b --- /dev/null +++ b/web_programming/sliding_window_using_generators.py @@ -0,0 +1,32 @@ +from typing import List, Generator + +def sliding_window(elements: List[int], window_size: int) -> Generator[List[int], None, None]: + """ + Generate sliding windows of size window_size from the given elements. + + Args: + elements (list): The input list of elements. + window_size (int): The size of the sliding window. + + Returns: + generator: A generator that yields sublists of size window_size. + + Example: + >>> lst = [1, 2, 3, 4, 5, 6, 7, 8] + >>> sw_gen = sliding_window(lst, 3) + >>> print(next(sw_gen)) + [1, 2, 3] + >>> print(next(sw_gen)) + [2, 3, 4] + + References: + https://stackoverflow.com/questions/8269916/what-is-sliding-window-algorithm-examples + """ + if len(elements) <= window_size: + return elements + for i in range(len(elements) - window_size + 1): + yield elements[i:i + window_size] + +if __name__ == "__main__": + import doctest + doctest.testmod() From 7010d7876162eaaf29086a8c9bf6b3c7c83e6cfb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 7 Jun 2023 19:30:07 +0000 Subject: [PATCH 6/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/max_sub_array_sum.py | 1 + web_programming/sliding_window_using_generators.py | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/dynamic_programming/max_sub_array_sum.py b/dynamic_programming/max_sub_array_sum.py index 20f935297661..10134b94f29c 100644 --- a/dynamic_programming/max_sub_array_sum.py +++ b/dynamic_programming/max_sub_array_sum.py @@ -1,5 +1,6 @@ from typing import List + def max_sub_array_sum(arr: List[int], size: int) -> int: """ Finds the maximum sum of a subarray within the given array using Kadane's algorithm. diff --git a/web_programming/sliding_window_using_generators.py b/web_programming/sliding_window_using_generators.py index bb6af3e8cb9b..71b8657af5a5 100644 --- a/web_programming/sliding_window_using_generators.py +++ b/web_programming/sliding_window_using_generators.py @@ -1,6 +1,9 @@ from typing import List, Generator -def sliding_window(elements: List[int], window_size: int) -> Generator[List[int], None, None]: + +def sliding_window( + elements: List[int], window_size: int +) -> Generator[List[int], None, None]: """ Generate sliding windows of size window_size from the given elements. @@ -18,15 +21,17 @@ def sliding_window(elements: List[int], window_size: int) -> Generator[List[int] [1, 2, 3] >>> print(next(sw_gen)) [2, 3, 4] - + References: - https://stackoverflow.com/questions/8269916/what-is-sliding-window-algorithm-examples + https://stackoverflow.com/questions/8269916/what-is-sliding-window-algorithm-examples """ if len(elements) <= window_size: return elements for i in range(len(elements) - window_size + 1): - yield elements[i:i + window_size] + yield elements[i : i + window_size] + if __name__ == "__main__": import doctest + doctest.testmod() From e5e1eb8c7cd1ba0e7f62e87bd39f803ea8e6300f Mon Sep 17 00:00:00 2001 From: "e.abouelkomsan" Date: Thu, 8 Jun 2023 00:20:35 +0200 Subject: [PATCH 7/9] Pre-commit Changes --- _black_version.py | 1 + dynamic_programming/max_sub_array_sum.py | 44 ------------------------ 2 files changed, 1 insertion(+), 44 deletions(-) create mode 100644 _black_version.py delete mode 100644 dynamic_programming/max_sub_array_sum.py diff --git a/_black_version.py b/_black_version.py new file mode 100644 index 000000000000..86b89cb8833d --- /dev/null +++ b/_black_version.py @@ -0,0 +1 @@ +version = "23.3.0" diff --git a/dynamic_programming/max_sub_array_sum.py b/dynamic_programming/max_sub_array_sum.py deleted file mode 100644 index 20f935297661..000000000000 --- a/dynamic_programming/max_sub_array_sum.py +++ /dev/null @@ -1,44 +0,0 @@ -from typing import List - -def max_sub_array_sum(arr: List[int], size: int) -> int: - """ - Finds the maximum sum of a subarray within the given array using Kadane's algorithm. - - Args: - arr (list): The input array of numbers. - size (int): The size of the array. - - Returns: - int: The maximum sum of a subarray within the array. - - Example: - >>> arr = [-2, -3, 4, -1, -2, 5, -3] - >>> max_sub_array_sum(arr, len(arr)) - 6 - In this example, the input array is [-2, -3, 4, -1, -2, 5, -3]. The maximum sum of a subarray - within this array is 6, which corresponds to the subarray [4, -1, -2, 5]. - - >>> arr = [-3, -4, 5, -1, 2, -4, 6, -1] - >>> max_sub_array_sum(arr, len(arr)) - 8 - - References: - https://en.wikipedia.org/wiki/Maximum_subarray_problem - """ - max_till_now = arr[0] - max_ending = 0 - - for i in range(size): - max_ending = max_ending + arr[i] - if max_ending < 0: - max_ending = 0 - elif max_till_now < max_ending: - max_till_now = max_ending - - return max_till_now - - -if __name__ == "__main__": - import doctest - - doctest.testmod() From 2bdff9ba23d2605e5bd58f93be3659e53780e625 Mon Sep 17 00:00:00 2001 From: "e.abouelkomsan" Date: Thu, 8 Jun 2023 00:31:24 +0200 Subject: [PATCH 8/9] Format Fixing --- dynamic_programming/max_subarray_sum.py | 43 +++++++++++++++++++ .../sliding_window_using_generators.py | 17 +++++--- 2 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 dynamic_programming/max_subarray_sum.py diff --git a/dynamic_programming/max_subarray_sum.py b/dynamic_programming/max_subarray_sum.py new file mode 100644 index 000000000000..be1a42152684 --- /dev/null +++ b/dynamic_programming/max_subarray_sum.py @@ -0,0 +1,43 @@ +def max_sub_array_sum(arr: list[int], size: int) -> int: + """ + Finds the maximum sum of a subarray within the given array using Kadane's algorithm. + + Args: + arr (list): The input array of numbers. + size (int): The size of the array. + + Returns: + int: The maximum sum of a subarray within the array. + + Example: + >>> arr = [-2, -3, 4, -1, -2, 5, -3] + >>> max_sub_array_sum(arr, len(arr)) + 6 + In this example, the input array is [-2, -3, 4, -1, -2, 5, -3]. + The maximum sum of a subarray within this array is 6, + which corresponds to the subarray [4, -1, -2, 5]. + + >>> arr = [-3, -4, 5, -1, 2, -4, 6, -1] + >>> max_sub_array_sum(arr, len(arr)) + 8 + + References: + https://en.wikipedia.org/wiki/Maximum_subarray_problem + """ + max_till_now = arr[0] + max_ending = 0 + + for i in range(size): + max_ending = max_ending + arr[i] + if max_ending < 0: + max_ending = 0 + elif max_till_now < max_ending: + max_till_now = max_ending + + return max_till_now + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/web_programming/sliding_window_using_generators.py b/web_programming/sliding_window_using_generators.py index bb6af3e8cb9b..217396236db5 100644 --- a/web_programming/sliding_window_using_generators.py +++ b/web_programming/sliding_window_using_generators.py @@ -1,6 +1,9 @@ -from typing import List, Generator +from collections.abc import Generator -def sliding_window(elements: List[int], window_size: int) -> Generator[List[int], None, None]: + +def sliding_window( + elements: list[int], window_size: int +) -> Generator[list[int], None, None]: """ Generate sliding windows of size window_size from the given elements. @@ -18,15 +21,17 @@ def sliding_window(elements: List[int], window_size: int) -> Generator[List[int] [1, 2, 3] >>> print(next(sw_gen)) [2, 3, 4] - + References: - https://stackoverflow.com/questions/8269916/what-is-sliding-window-algorithm-examples + https://stackoverflow.com/questions/8269916/what-is-sliding-window-algorithm-examples """ if len(elements) <= window_size: - return elements + yield elements for i in range(len(elements) - window_size + 1): - yield elements[i:i + window_size] + yield elements[i : i + window_size] + if __name__ == "__main__": import doctest + doctest.testmod() From 124a0e20ce01e9236d055cf471804ad53421ae84 Mon Sep 17 00:00:00 2001 From: "e.abouelkomsan" Date: Thu, 8 Jun 2023 00:54:39 +0200 Subject: [PATCH 9/9] rename files --- dynamic_programming/{max_subarray_sum.py => max_sub_array_sum.py} | 0 ...ng_window_using_generators.py => sliding_window_generators.py} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename dynamic_programming/{max_subarray_sum.py => max_sub_array_sum.py} (100%) rename web_programming/{sliding_window_using_generators.py => sliding_window_generators.py} (100%) diff --git a/dynamic_programming/max_subarray_sum.py b/dynamic_programming/max_sub_array_sum.py similarity index 100% rename from dynamic_programming/max_subarray_sum.py rename to dynamic_programming/max_sub_array_sum.py diff --git a/web_programming/sliding_window_using_generators.py b/web_programming/sliding_window_generators.py similarity index 100% rename from web_programming/sliding_window_using_generators.py rename to web_programming/sliding_window_generators.py