|
| 1 | +# 12.4: Permutation [Premium] |
| 2 | + |
| 3 | +## The Problem |
| 4 | +Find all permutation of a given list. |
| 5 | + |
| 6 | +## Hints |
| 7 | +In mathematics, the permutation is the way to find all possible order of elements |
| 8 | + |
| 9 | +For example, you have 1,2,3. Now all possible order is |
| 10 | +1,2,3 |
| 11 | +1,3,2 |
| 12 | +2,1,3 |
| 13 | +2,3,1 |
| 14 | +3,1,2 |
| 15 | +3,2,1 |
| 16 | + |
| 17 | +So, the permutation is a way to find all the possible order or sequence of elements. |
| 18 | + |
| 19 | +Give me more: non-premium |
| 20 | +One important part of the solution is to select one element and get a list excluding that element. Let's say you want to select the list without the 4th position element. |
| 21 | + |
| 22 | +All the elements before the 4th index element will be lst[:4]. This is from beginning to before the 4th element. |
| 23 | + |
| 24 | +Similarly, all the elements after the fourth position will be lst[4+1:]. This is from the 4+1 or 5th element to the end. |
| 25 | + |
| 26 | +This concept will help in permutation to select one element and place it in multiple places. |
| 27 | + |
| 28 | +```python |
| 29 | +lst = [2,3,4,6,11,16,7,8] |
| 30 | +before = lst[:4] |
| 31 | +after = lst[4+1:] |
| 32 | +excluding = before + after |
| 33 | +print(excluding) |
| 34 | +print('-----------------------') |
| 35 | +print(lst[:4] + lst[4+1]) |
| 36 | +``` |
| 37 | + |
| 38 | +**[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** |
| 39 | + |
| 40 | + |
| 41 | +## Solution |
| 42 | + |
| 43 | +```python |
| 44 | +def get_permutation(lst): |
| 45 | + # For an empty list, there is no permutation |
| 46 | + if len(lst) == 0: |
| 47 | + return [] |
| 48 | + # list with one element will have only one |
| 49 | + # permutation |
| 50 | + if len(lst) == 1: |
| 51 | + return [lst] |
| 52 | + # Create an empty list to store permutation |
| 53 | + perms = [] |
| 54 | + for i in range(len(lst)): |
| 55 | + # Extract current elemnt from the list. |
| 56 | + current = lst[i] |
| 57 | + # Recursively call permutation for the |
| 58 | + # remaining list |
| 59 | + rem_list = lst[:i] + lst[i+1:] |
| 60 | + rem_perm = get_permutation(rem_list) |
| 61 | + # Generate permutations by adding first element |
| 62 | + for p in rem_perm: |
| 63 | + perms.append([current] + p) |
| 64 | + return perms |
| 65 | +# now test the function |
| 66 | +data = [1,2,3] |
| 67 | +for perm in get_permutation(data): |
| 68 | + print (perm) |
| 69 | +``` |
| 70 | + |
| 71 | +**[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** |
| 72 | + |
| 73 | + |
| 74 | +## Explanation |
| 75 | +In the code, we are getting the current element by lst[i]. |
| 76 | +Then we create the remaining list by lst[:i] + lst[i+1:] |
| 77 | + |
| 78 | +We stored it in the remaining list named as rem_list. After that, we recursively call the get_permutation. This will give permutation for the rest elements. We stored that in the rem_perm list. |
| 79 | + |
| 80 | +Finally, we have to join the current element with the rest of the list. To do so, we have added [current] + p |
| 81 | + |
| 82 | +The loop variable p is a list. Whereas the current is an element. You can not add |
| 83 | + |
| 84 | +3 + [2,1] |
| 85 | + |
| 86 | +That’s why we put current in a list as well so that it becomes a list as well. In the following example, |
| 87 | + |
| 88 | +[3] + [2, 1] |
| 89 | + |
| 90 | +This will create [3, 2, 1]. That’s why we did [current] + p to make the final permutation. |
| 91 | + |
| 92 | +This code is harder. It will take a few practices to become comfortable with it. |
0 commit comments