Python unique grouping people task
Question: My task is to generate all posible way to group given number of people from
given number of total people. For example, if there are 4 guys at total, for
groups that contain 2 guys, I have to get array like this:

    
    
    ResultArr = {0: [1,2], 1:[1,3], 2:[1,4], 3:[2,3], 4:[2,4], 5:[3,4]}
    

I have this code:

    
    
    elNum = 3 #Number of guys in one group
    elLimit = 5 #Number of all guys
    
    CheckArr = [1] * elNum
    ResultArr = {}
    ResultArr[0] = [-1] * elNum
    goodCheckArr = [1] * elNum
    for i in range(1, elNum+1):
        ResultArr[0][i-1] = i
    
    Checking = True
    
    lenResultArr = 1
    
    while Checking:
    
        checkable = True
    
        for i in range(0, elNum):
            for ii in range(0, elNum): #
                if (CheckArr[i] == CheckArr[ii]) and not i == ii:
                    checkable = False
    
        if checkable:
            status2 = [0]*lenResultArr
            for i1 in range(0, len(ResultArr)):
                print " "
                print "ResultArr[",i1,"] = ", ResultArr[i1]
                status1 = [0]*elNum
                for i2 in range(0, elNum):
                    for i3 in range(0, elNum):
                        if ResultArr[i1][i2] == CheckArr[i3]:
                            status1[i2] = 1
                    print "status1 = ", status1, "      ChechArr = ", CheckArr
    
                if sum(status1) == elNum:
                    status2[i1] = 1
                print "status2[",str(i1),"] = ", status2[i1]
    
            if sum(status2) == 0:
                goodCheckArr = CheckArr
                ResultArr[lenResultArr] = goodCheckArr
                lenResultArr = lenResultArr + 1
                print "**** ResultArr = ", ResultArr
                print "sum(status2) = 0     len(ResultArr) = ", str(len(ResultArr)), "      lenResultArr = ", str(lenResultArr)
            else:
                print "sum(status2) > 0     len(ResultArr) = ", str(len(ResultArr)), "      lenResultArr = ", str(lenResultArr)
    
        print "************************************************************"
        print "***",ResultArr
    
        CheckArr[elNum-1] = CheckArr[elNum-1] + 1
        for i in range(elNum-1, -1, -1):
            if CheckArr[i] > elLimit:
                CheckArr[i-1] = CheckArr[i-1] + 1
                CheckArr[i] = 1
                Checking = False
            else:
                Checking = True
    
        print "************************************************************"
        print "***",ResultArr
    

I think y problem is in the last piece of code:

    
    
        print "************************************************************"
        print "***",ResultArr
    
        CheckArr[elNum-1] = CheckArr[elNum-1] + 1
        for i in range(elNum-1, -1, -1):
            if CheckArr[i] > elLimit:
                CheckArr[i-1] = CheckArr[i-1] + 1
                CheckArr[i] = 1
                Checking = False
            else:
                Checking = True
    
        print "************************************************************"
        print "***",ResultArr
    

The output looks like this:

    
    
    ************************************************************
    *** {0: [1, 2, 3], 1: [1, 2, 4]}
    ************************************************************
    *** {0: [1, 2, 3], 1: [1, 2, 5]}
    

**I have no idea why new elements of changeble checking arrey apears in
results array.**


Answer: You may simply use the following code to get all the combinations of length 2.

    
    
    import itertools
    persons = [1, 2, 3, 4]
    
    comb = itertools.combinations(persons, 2)
    
    for i in comb:
        print i
    
    (1, 2)
    (1, 3)
    (1, 4)
    (2, 3)
    (2, 4)
    (3, 4)
    

