codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
def multiSets(userList): ##code to create all subsets of a list ##['a','b','c'] = ['a','b','ab','ac','bc','abc'] ##algorithm logic. For each new element of the set add to the output list(i.e. 'combinations'): ##-the new element; and ##-the new element combined with all prior elements of the output list ##E.g. For ['a','b','c']. ## First pass: add 'a' to 'combinations' (since there were no prior elements of 'combinations' that's all) ## combinations = ['a'] ## Second pass: ## -add 'b' to 'combinations'; and ## -add 'b' to all prior elements of 'combinations', i.e. add a+b = 'ab' to 'combinations' ## combinations = ['a', 'b','ab'] ## Third pass: ## -add 'c' to 'combinations'; and ## -add 'c' to all prior elements of 'combinations', 'ac', 'bc', 'abc' ## combinations = ['a','b','ab', 'c','ac','bc','abc'] combinations = [] for i in range(len(userList)): combinations.append(userList[i]) loop_control = len(combinations) if loop_control > 1: for j in range(0,loop_control-1): combinations.append(combinations[j]+userList[i]) print combinations
Private
[
?
]
Run code
Submit