Skip to content

Commit d459021

Browse files
authored
Create permutations.py
Finds all permutations of numbers
1 parent 4e98bb9 commit d459021

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

permutations.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def permute(self, nums: List[int]) -> List[List[int]]:
3+
permutations = self.search([], nums)
4+
numlen = len(nums)
5+
permutations = [permutations[i * numlen:(i + 1) * numlen] for i in range((len(permutations) + numlen - 1) // numlen )] #oneliner splitting list into chunks
6+
return permutations
7+
8+
def search(self, current: List[int], possible: List[int]) -> List[int]:
9+
final = []
10+
if possible == []:
11+
return []
12+
elif len(possible) == 1:
13+
return current + [possible[0]]
14+
for num in possible:
15+
final.extend(self.search(current + [num], [p for p in possible if p != num]))
16+
return final

0 commit comments

Comments
 (0)