From 567112a462ca9314e824a4504485aaf94d8493b0 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Mon, 28 Sep 2020 19:22:20 +0200 Subject: [PATCH 1/4] heap's algorithm iterative --- .../heaps_algorithm_iterative.py | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 divide_and_conquer/heaps_algorithm_iterative.py diff --git a/divide_and_conquer/heaps_algorithm_iterative.py b/divide_and_conquer/heaps_algorithm_iterative.py new file mode 100644 index 000000000000..e0ae59df43f8 --- /dev/null +++ b/divide_and_conquer/heaps_algorithm_iterative.py @@ -0,0 +1,57 @@ +""" +Heap's algorithm (iterative) returns the list of all permutations possible from a list. +It minimizes movement by generating each permutation from the previous one +by swapping only two elements. +More information: +https://en.wikipedia.org/wiki/Heap%27s_algorithm. +""" + + +def heaps(arr: list) -> list: + """ + Pure python implementation of the iterative Heap's algorithm, + returning all permutations of a list. + >>> heaps([]) + [()] + >>> heaps([0]) + [(0,)] + >>> heaps([-1, 1]) + [(-1, 1), (1, -1)] + >>> heaps([1, 2, 3]) + [(1, 2, 3), (2, 1, 3), (3, 1, 2), (1, 3, 2), (2, 3, 1), (3, 2, 1)] + >>> from itertools import permutations + >>> sorted(heaps([1,2,3])) == sorted(permutations([1,2,3])) + True + """ + + if len(arr) <= 1: + return [tuple(arr)] + + res = [] + + def generate(n: int, arr: list): + c = [0] * n + res.append(tuple(arr)) + + i = 0 + while i < n: + if c[i] < i: + if i % 2 == 0: + arr[0], arr[i] = arr[i], arr[0] + else: + arr[c[i]], arr[i] = arr[i], arr[c[i]] + res.append(tuple(arr)) + c[i] += 1 + i = 0 + else: + c[i] = 0 + i += 1 + + generate(len(arr), arr) + return res + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + arr = [int(item) for item in user_input.split(",")] + print(heaps(arr)) From 32b396ec5703f69e597b1a380defae51e2fc8c94 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Mon, 28 Sep 2020 20:08:33 +0200 Subject: [PATCH 2/4] doctest --- divide_and_conquer/heaps_algorithm_iterative.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/divide_and_conquer/heaps_algorithm_iterative.py b/divide_and_conquer/heaps_algorithm_iterative.py index e0ae59df43f8..ddd3b94abd42 100644 --- a/divide_and_conquer/heaps_algorithm_iterative.py +++ b/divide_and_conquer/heaps_algorithm_iterative.py @@ -22,6 +22,8 @@ def heaps(arr: list) -> list: >>> from itertools import permutations >>> sorted(heaps([1,2,3])) == sorted(permutations([1,2,3])) True + >>> all(sorted(heaps(x)) == sorted(permutations(x)) for x in ([], [0], [-1, 1], [1, 2, 3])) + True """ if len(arr) <= 1: From df1ae96e48121d6c8083affc341ec9556bf3fe48 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Mon, 28 Sep 2020 20:26:34 +0200 Subject: [PATCH 3/4] doctest --- divide_and_conquer/heaps_algorithm_iterative.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/divide_and_conquer/heaps_algorithm_iterative.py b/divide_and_conquer/heaps_algorithm_iterative.py index ddd3b94abd42..db325be2c0f4 100644 --- a/divide_and_conquer/heaps_algorithm_iterative.py +++ b/divide_and_conquer/heaps_algorithm_iterative.py @@ -22,7 +22,8 @@ def heaps(arr: list) -> list: >>> from itertools import permutations >>> sorted(heaps([1,2,3])) == sorted(permutations([1,2,3])) True - >>> all(sorted(heaps(x)) == sorted(permutations(x)) for x in ([], [0], [-1, 1], [1, 2, 3])) + >>> all(sorted(heaps(x)) == sorted(permutations(x)) + ... for x in ([], [0], [-1, 1], [1, 2, 3])) True """ From bb9b7df096d22fcdb005459edff22e3a19bcc981 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Tue, 29 Sep 2020 10:20:28 +0200 Subject: [PATCH 4/4] rebuild --- divide_and_conquer/heaps_algorithm_iterative.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/divide_and_conquer/heaps_algorithm_iterative.py b/divide_and_conquer/heaps_algorithm_iterative.py index db325be2c0f4..4dab41f539c0 100644 --- a/divide_and_conquer/heaps_algorithm_iterative.py +++ b/divide_and_conquer/heaps_algorithm_iterative.py @@ -1,5 +1,5 @@ """ -Heap's algorithm (iterative) returns the list of all permutations possible from a list. +Heap's (iterative) algorithm returns the list of all permutations possible from a list. It minimizes movement by generating each permutation from the previous one by swapping only two elements. More information: