- 2

Write a program to find a permutation of string or a word

permutation mean a how many ways to arrage a word to find different word by given word as examples we can write ABC IN PERMUTATION AS ABC BCA CAB ACB BAC ACB CBA as like that different arrangements of ABC

9th Oct 2018, 12:12 PM
PIYUSH AGRAWAL
PIYUSH AGRAWAL - avatar
2 Answers
+ 1
You want to do recursion, so you first have to find out how the recursion would work. In this case it is the following: permutation [a,b,c,...] = [a + permutation[b,c,...], b + permutation[a,c,..], ...] And as a final condition: permutation [a] = [a] So the recursion splits up the list in sublists with one element extracted each time. Then this element is added to the front of each of the permutations of the sublist. So in pseudo-code: def permutation(s): if len(s) == 1: return [s] perm_list = [] # resulting list for a in s: remaining_elements = [x for x in s if x != a] z = permutation(remaining_elements) # permutations of sublist for t in z: perm_list.append([a] + t) return perm_list
9th Oct 2018, 12:30 PM
Willem Roos
9th Oct 2018, 12:29 PM
Daniele Bonomi
Daniele Bonomi - avatar