divide_and_conquer.heaps_algorithm_iterative¶
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: https://en.wikipedia.org/wiki/Heap%27s_algorithm.
Attributes¶
Functions¶
|
Pure python implementation of the iterative Heap's algorithm, |
Module Contents¶
- divide_and_conquer.heaps_algorithm_iterative.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 >>> all(sorted(heaps(x)) == sorted(permutations(x)) … for x in ([], [0], [-1, 1], [1, 2, 3])) True
- divide_and_conquer.heaps_algorithm_iterative.user_input¶