Skip to content

Commit de8decb

Browse files
author
delanou
committed
added permutation example
1 parent 9b2d54e commit de8decb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

samplepermutation.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# PowerSet.py
2+
# Description: given an array of numbers, generates
3+
# a power set
4+
5+
def findPowerSet(originalSet, index, currentSet, powerSet):
6+
if (index >= len(originalSet)):
7+
# add list to power set(convert list to tuple)
8+
powerSet.append(currentSet)
9+
return
10+
11+
# set where element was not added
12+
notAddedSet = currentSet[:]
13+
14+
# append both sets
15+
currentSet.append(originalSet[index])
16+
17+
# recursive call to other elements in originalSet
18+
findPowerSet(originalSet, index + 1, currentSet, powerSet)
19+
findPowerSet(originalSet, index + 1, notAddedSet, powerSet)
20+
21+
22+
if __name__ == "__main__":
23+
print("in main file")
24+
testSet = [1, 2, 3]
25+
powerSet = []
26+
27+
findPowerSet(testSet, 0, [], powerSet)
28+
29+
print("Final Set = " + str(powerSet))

0 commit comments

Comments
 (0)