From e45baed09698d5521d789b48fb1fb48e5e833cdd Mon Sep 17 00:00:00 2001 From: gopichandpuli9 <64889409+gopichandpuli9@users.noreply.github.com> Date: Sun, 21 Feb 2021 08:17:29 +0530 Subject: [PATCH] Create Generate Permutations.py --- .../Generate Permutations.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Recursion/python/Generate Permutations/Generate Permutations.py diff --git a/Recursion/python/Generate Permutations/Generate Permutations.py b/Recursion/python/Generate Permutations/Generate Permutations.py new file mode 100644 index 0000000..1251420 --- /dev/null +++ b/Recursion/python/Generate Permutations/Generate Permutations.py @@ -0,0 +1,35 @@ +def permutation(lst): + + # If lst is empty then there are no permutations + if len(lst) == 0: + return [] + + # If there is only one element in lst then, only + # one permuatation is possible + if len(lst) == 1: + return [lst] + + # Find the permutations for lst if there are + # more than 1 characters + + l = [] # empty list that will store current permutation + + # Iterate the input(lst) and calculate the permutation + for i in range(len(lst)): + m = lst[i] + + # Extract lst[i] or m from the list. remLst is + # remaining list + remLst = lst[:i] + lst[i+1:] + + # Generating all permutations where m is first + # element + for p in permutation(remLst): + l.append([m] + p) + return l + + +# Driver program to test above function +data = list(map(int, input().split())) +for p in permutation(data): + print p