Skip to content

Commit

Permalink
Create permutations.py
Browse files Browse the repository at this point in the history
Finds all permutations of numbers
  • Loading branch information
gabedonnan authored Jan 15, 2023
1 parent 4e98bb9 commit d459021
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions permutations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
permutations = self.search([], nums)
numlen = len(nums)
permutations = [permutations[i * numlen:(i + 1) * numlen] for i in range((len(permutations) + numlen - 1) // numlen )] #oneliner splitting list into chunks
return permutations

def search(self, current: List[int], possible: List[int]) -> List[int]:
final = []
if possible == []:
return []
elif len(possible) == 1:
return current + [possible[0]]
for num in possible:
final.extend(self.search(current + [num], [p for p in possible if p != num]))
return final

0 comments on commit d459021

Please sign in to comment.