aboutsummaryrefslogtreecommitdiff
path: root/challenge-218/eric-cheung/python/ch-2.py
blob: 682949d9c26864647435effc2296161efed31dcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
arrMatrix = [[0, 0, 1, 1], [1, 0, 1, 0], [1, 1, 0, 0]]
## arrMatrix = [[0]]  ## Example 2

nMatrixSum = 0

def GetSum (arrInputMat):

    nSum = 0

    for nIndx in range(0, len(arrInputMat)):
        nSum = nSum + int("".join([str(nElem) for nElem in arrInputMat[nIndx]]), 2)

    return nSum

def SwapAlongRow (arrInputMat, nRowIndx):

    for nIndx in range(0, len(arrMatrix[0])):
        arrInputMat[nRowIndx][nIndx] = 1 - arrInputMat[nRowIndx][nIndx]

    return

def SwapAlongCol (arrInputMat, nColIndx):

    for nIndx in range(0, len(arrMatrix)):
        arrInputMat[nIndx][nColIndx] = 1 - arrInputMat[nIndx][nColIndx]

    return


if len(arrMatrix) == 1 and len(arrMatrix[0]) == 1:
    print (1)
else:
    ## Swap Along Rows
    for nRowLoopIndx in range(0, len(arrMatrix)):
        arrTempMatrix = [arrRow[:] for arrRow in arrMatrix]
        SwapAlongRow (arrTempMatrix, nRowLoopIndx)
        if GetSum (arrTempMatrix) > nMatrixSum:
            SwapAlongRow (arrMatrix, nRowLoopIndx)
            nMatrixSum = GetSum (arrMatrix)

    ## Swap Along Columns
    for nColLoopIndx in range(0, len(arrMatrix[0])):
        arrTempMatrix = [arrRow[:] for arrRow in arrMatrix]
        SwapAlongCol (arrTempMatrix, nColLoopIndx)
        if GetSum (arrTempMatrix) > nMatrixSum:
            SwapAlongCol (arrMatrix, nColLoopIndx)
            nMatrixSum = GetSum (arrMatrix)

    ## print (arrMatrix)
    print (nMatrixSum)