Skip to content

Commit 04438ec

Browse files
Recursion- Return possible subsets of an array
1 parent 012bf07 commit 04438ec

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

return-subset.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def subset(arr):
2+
output = [[]]
3+
window = 1
4+
while window < len(arr):
5+
window_start = 0
6+
while window_start < len(arr):
7+
counter = 1
8+
window_end = window_start+window
9+
while window_end + counter <= len(arr):
10+
to_append = []
11+
to_append.extend(arr[window_start:window_end])
12+
to_append.append(arr[window_end+counter-1])
13+
output.append(to_append)
14+
counter +=1
15+
window_start += 1
16+
window += 1
17+
return output
18+
19+
def subset_with_recursion(arr):
20+
return return_subset_with_recursion(arr, 0)
21+
22+
def return_subset_with_recursion(arr, index):
23+
if index >= len(arr):
24+
return [[]]
25+
output = list()
26+
small_output = return_subset_with_recursion(arr, index+1)
27+
for element in small_output:
28+
output.append(element)
29+
for element in small_output:
30+
curr = list()
31+
curr.append(arr[index])
32+
curr.extend(element)
33+
output.append(curr)
34+
35+
return output
36+
37+
38+
print(subset_with_recursion([9,15,12]))

0 commit comments

Comments
 (0)