-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutations.py
More file actions
24 lines (20 loc) · 989 Bytes
/
permutations.py
File metadata and controls
24 lines (20 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def generate_permutations(nums, start, end):
# Base Case: If we are at the last index, we have a complete permutation
if start == end:
print(nums)
else:
# Loop through the list from the 'start' index to the end
for i in range(start, end + 1):
# STEP 1: SWAP (The Choice)
# We place the element at index 'i' into the 'start' position
nums[start], nums[i] = nums[i], nums[start]
# STEP 2: RECURSE (The Dive)
# We move to the next position (start + 1) and repeat
generate_permutations(nums, start + 1, end)
# STEP 3: BACKTRACK (The Undo)
# Critical: We swap them back to restore the original order
# This ensures the list is clean for the next iteration of the loop
nums[start], nums[i] = nums[i], nums[start]
# Driver Code
my_list = [1, 2, 3]
generate_permutations(my_list, 0, len(my_list) - 1)