[ create a new paste ] login | about

Link: http://codepad.org/g64qPFt7    [ raw code | output | fork ]

Python, pasted on Feb 3:
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

multiSets('abcd')
multiSets('dbca')


Output:
1
2
['a', 'b', 'ab', 'c', 'ac', 'bc', 'abc', 'd', 'ad', 'bd', 'abd', 'cd', 'acd', 'bcd', 'abcd']
['d', 'b', 'db', 'c', 'dc', 'bc', 'dbc', 'a', 'da', 'ba', 'dba', 'ca', 'dca', 'bca', 'dbca']


Create a new paste based on this one


Comments: