From 0b75e10dd9ea6835f7f27ca922a7d401fb152841 Mon Sep 17 00:00:00 2001 From: Himanshu Tomar Date: Sat, 29 Apr 2023 22:51:01 +0530 Subject: [PATCH 01/16] Added minimum waiting time problem solution using greedy algorithm --- DIRECTORY.md | 3 +- greedy_methods/minimum_waiting_time.py | 45 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 greedy_methods/minimum_waiting_time.py diff --git a/DIRECTORY.md b/DIRECTORY.md index 8e67c85c6fa8..dd14f1a58649 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -449,7 +449,8 @@ * [Fractional Knapsack](greedy_methods/fractional_knapsack.py) * [Fractional Knapsack 2](greedy_methods/fractional_knapsack_2.py) * [Optimal Merge Pattern](greedy_methods/optimal_merge_pattern.py) - + * [Minimum Waiting Time ](greedy_methods/minimum_waiting_time.py) + ## Hashes * [Adler32](hashes/adler32.py) * [Chaos Machine](hashes/chaos_machine.py) diff --git a/greedy_methods/minimum_waiting_time.py b/greedy_methods/minimum_waiting_time.py new file mode 100644 index 000000000000..a62edfb34510 --- /dev/null +++ b/greedy_methods/minimum_waiting_time.py @@ -0,0 +1,45 @@ +""" +This is a pure Python implementation for minimum waiting time problem using greedy algorithm +reference: https://www.youtube.com/watch?v=Sf3eiO12eJs + +For doctests run following command: +python -m doctest -v .\greedy_methods\minimum_waiting_time.py + +The minimum_waiting_time function uses a greedy algorithm to calculate the minimum time for queries to complete. +It sorts the list in non-decreasing order, calculates the waiting time for each query by multiplying its position +in the list with the sum of all remaining query times, and returns the total waiting time. A doctest ensures that +the function produces the correct output. +""" + + +def minimum_waiting_time(queries): + """ + This function takes a list of query times and returns the minimum waiting time + for all queries to be completed. + + >>> minimum_waiting_time([3, 2, 1, 2, 6]) + 17 + >>> minimum_waiting_time([3, 2, 1]) + 4 + >>> minimum_waiting_time([1, 2, 3, 4]) + 10 + """ + # Base case + n = len(queries) + if n == 0 or n == 1: + return 0 + + # Sort the list in non-decreasing order + queries.sort() + + # Calculate the total waiting time + total_waiting_time = 0 + for i in range(n - 1): + total_waiting_time += queries[i] * (n - i - 1) + + return total_waiting_time + + +if __name__ == '__main__': + import doctest + doctest.testmod() From 6520b7b1c0ab9cd1079360c270273c9da47bb84b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 29 Apr 2023 17:27:58 +0000 Subject: [PATCH 02/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- DIRECTORY.md | 2 +- greedy_methods/minimum_waiting_time.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index dd14f1a58649..1a4ad8dcff85 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -450,7 +450,7 @@ * [Fractional Knapsack 2](greedy_methods/fractional_knapsack_2.py) * [Optimal Merge Pattern](greedy_methods/optimal_merge_pattern.py) * [Minimum Waiting Time ](greedy_methods/minimum_waiting_time.py) - + ## Hashes * [Adler32](hashes/adler32.py) * [Chaos Machine](hashes/chaos_machine.py) diff --git a/greedy_methods/minimum_waiting_time.py b/greedy_methods/minimum_waiting_time.py index a62edfb34510..e511a9ea685e 100644 --- a/greedy_methods/minimum_waiting_time.py +++ b/greedy_methods/minimum_waiting_time.py @@ -40,6 +40,7 @@ def minimum_waiting_time(queries): return total_waiting_time -if __name__ == '__main__': +if __name__ == "__main__": import doctest + doctest.testmod() From bd51b5a8da603c3b35e17595a0b6dbcf722e56bc Mon Sep 17 00:00:00 2001 From: Himanshu Tomar Date: Sat, 29 Apr 2023 23:09:30 +0530 Subject: [PATCH 03/16] ruff --fix --- greedy_methods/minimum_waiting_time.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/greedy_methods/minimum_waiting_time.py b/greedy_methods/minimum_waiting_time.py index a62edfb34510..4174e3792e4c 100644 --- a/greedy_methods/minimum_waiting_time.py +++ b/greedy_methods/minimum_waiting_time.py @@ -1,14 +1,16 @@ """ -This is a pure Python implementation for minimum waiting time problem using greedy algorithm +This is a pure Python implementation for minimum waiting time problem using greedy +algorithm. reference: https://www.youtube.com/watch?v=Sf3eiO12eJs For doctests run following command: -python -m doctest -v .\greedy_methods\minimum_waiting_time.py +python -m doctest -v minimum_waiting_time.py -The minimum_waiting_time function uses a greedy algorithm to calculate the minimum time for queries to complete. -It sorts the list in non-decreasing order, calculates the waiting time for each query by multiplying its position -in the list with the sum of all remaining query times, and returns the total waiting time. A doctest ensures that -the function produces the correct output. +The minimum_waiting_time function uses a greedy algorithm to calculate the minimum +time for queries to complete. It sorts the list in non-decreasing order, calculates +the waiting time for each query by multiplying its position in the list with the +sum of all remaining query times, and returns the total waiting time. A doctest +ensures that the function produces the correct output. """ From 0bd440e214b35800b5adec752bf77b5de2f74c2b Mon Sep 17 00:00:00 2001 From: Himanshu Tomar Date: Sat, 29 Apr 2023 23:16:50 +0530 Subject: [PATCH 04/16] Add type hints --- greedy_methods/minimum_waiting_time.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/greedy_methods/minimum_waiting_time.py b/greedy_methods/minimum_waiting_time.py index 1ad475052f7c..c20c12cb9011 100644 --- a/greedy_methods/minimum_waiting_time.py +++ b/greedy_methods/minimum_waiting_time.py @@ -14,11 +14,18 @@ """ -def minimum_waiting_time(queries): +def minimum_waiting_time(queries: list) -> int: """ This function takes a list of query times and returns the minimum waiting time for all queries to be completed. + Args: + queries [list]: A list of queries + + Returns: + total_waiting_time [int]: Minimum waiting time + + Examples: >>> minimum_waiting_time([3, 2, 1, 2, 6]) 17 >>> minimum_waiting_time([3, 2, 1]) From ac29bff0ceabea014012544cd0ee8d439eba4ab0 Mon Sep 17 00:00:00 2001 From: Himanshu Tomar Date: Sun, 30 Apr 2023 13:05:52 +0530 Subject: [PATCH 05/16] Added two more doc test --- greedy_methods/minimum_waiting_time.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/greedy_methods/minimum_waiting_time.py b/greedy_methods/minimum_waiting_time.py index c20c12cb9011..0cff23394337 100644 --- a/greedy_methods/minimum_waiting_time.py +++ b/greedy_methods/minimum_waiting_time.py @@ -32,6 +32,10 @@ def minimum_waiting_time(queries: list) -> int: 4 >>> minimum_waiting_time([1, 2, 3, 4]) 10 + >>> minimum_waiting_time([5, 5, 5, 5]) + 30 + >>> minimum_waiting_time([]) + 0 """ # Base case n = len(queries) From 8062a3ceec41cca41b7acca24f762bd38d9c9483 Mon Sep 17 00:00:00 2001 From: Himanshu Tomar Date: Mon, 1 May 2023 11:12:10 +0530 Subject: [PATCH 06/16] Removed unnecessary comments --- greedy_methods/minimum_waiting_time.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/greedy_methods/minimum_waiting_time.py b/greedy_methods/minimum_waiting_time.py index 0cff23394337..8d7bbc14fdbd 100644 --- a/greedy_methods/minimum_waiting_time.py +++ b/greedy_methods/minimum_waiting_time.py @@ -37,15 +37,13 @@ def minimum_waiting_time(queries: list) -> int: >>> minimum_waiting_time([]) 0 """ - # Base case + n = len(queries) if n == 0 or n == 1: return 0 - # Sort the list in non-decreasing order queries.sort() - # Calculate the total waiting time total_waiting_time = 0 for i in range(n - 1): total_waiting_time += queries[i] * (n - i - 1) From 27a0d4a0f5a488f284001ccea8bb81e28c2264a9 Mon Sep 17 00:00:00 2001 From: Himanshu Tomar Date: Mon, 1 May 2023 11:17:28 +0530 Subject: [PATCH 07/16] updated type hints --- greedy_methods/minimum_waiting_time.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/greedy_methods/minimum_waiting_time.py b/greedy_methods/minimum_waiting_time.py index 8d7bbc14fdbd..1125c6e30aa3 100644 --- a/greedy_methods/minimum_waiting_time.py +++ b/greedy_methods/minimum_waiting_time.py @@ -14,13 +14,13 @@ """ -def minimum_waiting_time(queries: list) -> int: +def minimum_waiting_time(queries: list[int]) -> int: """ This function takes a list of query times and returns the minimum waiting time for all queries to be completed. Args: - queries [list]: A list of queries + queries [list[int]]: A list of queries Returns: total_waiting_time [int]: Minimum waiting time From b2bf9886f7335cb22c08508d6244d6292bdad8be Mon Sep 17 00:00:00 2001 From: Himanshu Tomar Date: Mon, 1 May 2023 15:35:11 +0530 Subject: [PATCH 08/16] Updated the code as per the code review --- greedy_methods/minimum_waiting_time.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/greedy_methods/minimum_waiting_time.py b/greedy_methods/minimum_waiting_time.py index 1125c6e30aa3..aaae8cf8f720 100644 --- a/greedy_methods/minimum_waiting_time.py +++ b/greedy_methods/minimum_waiting_time.py @@ -1,6 +1,5 @@ """ -This is a pure Python implementation for minimum waiting time problem using greedy -algorithm. +Calculate the minimum waiting time using a greedy algorithm. reference: https://www.youtube.com/watch?v=Sf3eiO12eJs For doctests run following command: @@ -20,10 +19,10 @@ def minimum_waiting_time(queries: list[int]) -> int: for all queries to be completed. Args: - queries [list[int]]: A list of queries + queries: A list of queries measured in picoseconds Returns: - total_waiting_time [int]: Minimum waiting time + total_waiting_time: Minimum waiting time measured in picoseconds Examples: >>> minimum_waiting_time([3, 2, 1, 2, 6]) @@ -37,18 +36,10 @@ def minimum_waiting_time(queries: list[int]) -> int: >>> minimum_waiting_time([]) 0 """ - n = len(queries) - if n == 0 or n == 1: + if n in (0, 1): return 0 - - queries.sort() - - total_waiting_time = 0 - for i in range(n - 1): - total_waiting_time += queries[i] * (n - i - 1) - - return total_waiting_time + return sum(query * (n - i - 1) for i, query in enumerate(sorted(queries))) if __name__ == "__main__": From 7ce741d93168d23bca994eb948ca0dae524b3ff2 Mon Sep 17 00:00:00 2001 From: Himanshu Tomar Date: Wed, 24 May 2023 13:13:27 +0530 Subject: [PATCH 09/16] Added recursive algo to calculate product sum from an array --- data_structures/arrays/product_sum.py | 70 +++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 data_structures/arrays/product_sum.py diff --git a/data_structures/arrays/product_sum.py b/data_structures/arrays/product_sum.py new file mode 100644 index 000000000000..ddb7107223c4 --- /dev/null +++ b/data_structures/arrays/product_sum.py @@ -0,0 +1,70 @@ +""" +Calculate Product Sum from an Array. +reference: https://dev.to/sfrasica/algorithms-product-sum-from-an-array-dc6 + +For doctests run following command: +python -m doctest -v product_sum.py + +We need to create a function that calculates the product sum of a "special" array. +This array can contain integers or nested arrays. The product sum is obtained by +adding all the elements together and multiplying by their respective depth. + +For example, in the array [x, y], the product sum is (x + y). In the array [x, [y, z]], +the product sum is x + 2 * (y + z). In the array [x, [y, [z]]], +the product sum is x + 2 * (y + 3z). + +Example Input: + +[5, 2, [-7, 1], 3, [6, [-13, 8], 4]] +""" + + +def product_sum(arr: list[int | list], depth: int) -> int: + """ + Recursively calculates the product sum of an array. + + The product sum of an array is defined as the sum of its elements multiplied by + their respective depths.If an element is a list, its product sum is calculated + recursively by multiplying the sum of its elements with its depth plus one. + + Args: + arr: The array of integers and nested lists. + depth: The current depth level. + + Returns: + int: The product sum of the array. + """ + total_sum = 0 + for ele in arr: + if isinstance(ele, list): + total_sum += product_sum(ele, depth + 1) + else: + total_sum += ele + return total_sum * depth + + +def product_sum_array(array: list[int | list]) -> int: + """ + Calculates the product sum of an array. + + Args: + array (List[Union[int, List]]): The array of integers and nested lists. + + Returns: + int: The product sum of the array. + + Examples: + >>> product_sum_array([1, 2, 3]) + 6 + >>> product_sum_array([1, [2, 3]]) + 11 + >>> product_sum_array([1, [2, [3, 4]]]) + 47 + """ + return product_sum(array, 1) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From e56303eb168923be5e9584b1524e0676cbedf061 Mon Sep 17 00:00:00 2001 From: Himanshu Tomar Date: Wed, 24 May 2023 13:25:21 +0530 Subject: [PATCH 10/16] Added recursive algo to calculate product sum from an array --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 5c4b5716629b..cfcbc701c6d5 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -166,6 +166,7 @@ * Arrays * [Permutations](data_structures/arrays/permutations.py) * [Prefix Sum](data_structures/arrays/prefix_sum.py) + * [Product Sum Array](data_structures/arrays/product_sum.py) * Binary Tree * [Avl Tree](data_structures/binary_tree/avl_tree.py) * [Basic Binary Tree](data_structures/binary_tree/basic_binary_tree.py) @@ -450,7 +451,6 @@ * [Fractional Knapsack 2](greedy_methods/fractional_knapsack_2.py) * [Minimum Waiting Time](greedy_methods/minimum_waiting_time.py) * [Optimal Merge Pattern](greedy_methods/optimal_merge_pattern.py) - * [Minimum Waiting Time ](greedy_methods/minimum_waiting_time.py) ## Hashes * [Adler32](hashes/adler32.py) From 1ca9b66fc90d44c00d225b1a1b817a33323b83ee Mon Sep 17 00:00:00 2001 From: Himanshu Tomar Date: Sun, 28 May 2023 13:58:00 +0530 Subject: [PATCH 11/16] Update doc string --- data_structures/arrays/product_sum.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/product_sum.py b/data_structures/arrays/product_sum.py index ddb7107223c4..7342fad4d5c8 100644 --- a/data_structures/arrays/product_sum.py +++ b/data_structures/arrays/product_sum.py @@ -14,8 +14,9 @@ the product sum is x + 2 * (y + 3z). Example Input: - [5, 2, [-7, 1], 3, [6, [-13, 8], 4]] +Output: 12 + """ From f1f8f67d31c471f5d09792f6a19465368dd288bf Mon Sep 17 00:00:00 2001 From: Himanshu Tomar Date: Mon, 29 May 2023 13:31:06 +0530 Subject: [PATCH 12/16] Added doctest for product_sum function --- data_structures/arrays/product_sum.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/data_structures/arrays/product_sum.py b/data_structures/arrays/product_sum.py index ddb7107223c4..c8f1be4e2cd3 100644 --- a/data_structures/arrays/product_sum.py +++ b/data_structures/arrays/product_sum.py @@ -33,6 +33,12 @@ def product_sum(arr: list[int | list], depth: int) -> int: Returns: int: The product sum of the array. + + Examples: + >>> product_sum([1, 2, 3], 1) + 6 + >>> product_sum([-1, 2, [-3, 4]], 2) + 8 """ total_sum = 0 for ele in arr: From 0f46c07eb0122e5945125e87fdd7711009f4613f Mon Sep 17 00:00:00 2001 From: Himanshu Tomar Date: Mon, 29 May 2023 17:31:15 +0530 Subject: [PATCH 13/16] Updated the code and added more doctests --- data_structures/arrays/product_sum.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/data_structures/arrays/product_sum.py b/data_structures/arrays/product_sum.py index ee90c32fbf1a..9ed38c908d7e 100644 --- a/data_structures/arrays/product_sum.py +++ b/data_structures/arrays/product_sum.py @@ -40,13 +40,28 @@ def product_sum(arr: list[int | list], depth: int) -> int: 6 >>> product_sum([-1, 2, [-3, 4]], 2) 8 + >>> product_sum([1, 2, 3], -1) + -6 + >>> product_sum([1, 2, 3], 0) + 0 + >>> product_sum([1, 2, 3], 7) + 42 + >>> product_sum((1, 2, 3), 7) + 42 + >>> product_sum({1, 2, 3}, 7) + 42 + >>> product_sum([1, -1], 1) + 0 + >>> product_sum([1, -2], 1) + -1 + >>> product_sum([-3.5, [1, [0.5]]], 1) + 1.5 + """ total_sum = 0 for ele in arr: - if isinstance(ele, list): - total_sum += product_sum(ele, depth + 1) - else: - total_sum += ele + total_sum += product_sum(ele, depth + + 1) if isinstance(ele, list) else ele return total_sum * depth From 340e3f81646b111bcc82e6d23ce08033035d722d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 29 May 2023 12:02:59 +0000 Subject: [PATCH 14/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/product_sum.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/data_structures/arrays/product_sum.py b/data_structures/arrays/product_sum.py index 9ed38c908d7e..951fdad653fb 100644 --- a/data_structures/arrays/product_sum.py +++ b/data_structures/arrays/product_sum.py @@ -60,8 +60,7 @@ def product_sum(arr: list[int | list], depth: int) -> int: """ total_sum = 0 for ele in arr: - total_sum += product_sum(ele, depth + - 1) if isinstance(ele, list) else ele + total_sum += product_sum(ele, depth + 1) if isinstance(ele, list) else ele return total_sum * depth From 6b9f7122ada72d674ce37c2933fa14763dbe8477 Mon Sep 17 00:00:00 2001 From: Himanshu Tomar Date: Fri, 9 Jun 2023 22:10:00 +0530 Subject: [PATCH 15/16] Added more test coverage for product_sum method --- data_structures/arrays/product_sum.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/data_structures/arrays/product_sum.py b/data_structures/arrays/product_sum.py index 9ed38c908d7e..ebc7fb548dbf 100644 --- a/data_structures/arrays/product_sum.py +++ b/data_structures/arrays/product_sum.py @@ -82,6 +82,13 @@ def product_sum_array(array: list[int | list]) -> int: 11 >>> product_sum_array([1, [2, [3, 4]]]) 47 + >>> product_sum_array([0]) + 0 + >>> product_sum_array([-3.5, [1, [0.5]]]) + 1.5 + >>> product_sum_array([1, -2]) + -1 + """ return product_sum(array, 1) From 4fae370cc09553d03d9402bcd5137c4bbb5ed661 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 23 Jun 2023 10:12:46 +0200 Subject: [PATCH 16/16] Update product_sum.py --- data_structures/arrays/product_sum.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/data_structures/arrays/product_sum.py b/data_structures/arrays/product_sum.py index b214b598817e..4fb906f369ab 100644 --- a/data_structures/arrays/product_sum.py +++ b/data_structures/arrays/product_sum.py @@ -1,13 +1,13 @@ """ -Calculate Product Sum from an Array. +Calculate the Product Sum from a Special Array. reference: https://dev.to/sfrasica/algorithms-product-sum-from-an-array-dc6 -For doctests run following command: +Python doctests can be run with the following command: python -m doctest -v product_sum.py -We need to create a function that calculates the product sum of a "special" array. -This array can contain integers or nested arrays. The product sum is obtained by -adding all the elements together and multiplying by their respective depth. +Calculate the product sum of a "special" array which can contain integers or nested +arrays. The product sum is obtained by adding all elements and multiplying by their +respective depths. For example, in the array [x, y], the product sum is (x + y). In the array [x, [y, z]], the product sum is x + 2 * (y + z). In the array [x, [y, [z]]], @@ -25,7 +25,7 @@ def product_sum(arr: list[int | list], depth: int) -> int: Recursively calculates the product sum of an array. The product sum of an array is defined as the sum of its elements multiplied by - their respective depths.If an element is a list, its product sum is calculated + their respective depths. If an element is a list, its product sum is calculated recursively by multiplying the sum of its elements with its depth plus one. Args: