-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathArray Permutations.py
More file actions
24 lines (21 loc) · 905 Bytes
/
Array Permutations.py
File metadata and controls
24 lines (21 loc) · 905 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
# Given an array with unique elements, generate all sub arrays from that array
class Solution(object):
# Basic Generate all sub arrays, but this implementation can also generate duplicates and duplicate in jumbled order
def allSubArrays(self, array, subArray, count):
print count, subArray
if not array:
return
for i in range(len(array)):
# remove the element at index i
element = array.pop(i)
# increase the count, this is to help us understand how many times is this function called
count[0] += 1
# add it to subArray
self.allSubArrays(array, subArray+[element], count)
# Backtrack by putting the element back in the array
array.append(element)
def run(self, nums):
self.allSubArrays(nums, [], [1])
# Main
obj = Solution()
obj.run([1,2,3,4,5])