From d4590217d216c1259b11e9c7dc0dc48080a99025 Mon Sep 17 00:00:00 2001 From: Gabriel Donnan <47415809+gabedonnan@users.noreply.github.com> Date: Sun, 15 Jan 2023 00:20:26 +0000 Subject: [PATCH] Create permutations.py Finds all permutations of numbers --- permutations.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 permutations.py diff --git a/permutations.py b/permutations.py new file mode 100644 index 0000000..4ba1a88 --- /dev/null +++ b/permutations.py @@ -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