aboutsummaryrefslogtreecommitdiff
path: root/challenge-218/robert-dicicco/python/ch-2.py
blob: 20be6643f20cfbc72c1c275e7c7a645ca3e98339 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python
#---------------------------------------------
# AUTHOR: Robert DiCicco
# DATE  : 2023-05-26
# Challenge 218 MatrixScore.py ( Python )
#---------------------------------------------
matrix = [ [0,0,1,1],
           [1,0,1,0],
           [1,1,0,0], ]
row = 0
col = 0

def ShowMatrix():
    total = 0
    for x in range(3):   #[0,1,2]:
        print(matrix[x])
        total += binary_to_decimal(matrix[x])
    print("Total = ",total,"\n")

def ToggleRow(r, ov):
    cnt = 0
    testmat = []
    for x in matrix[r]:
        if x == 0:
            testmat.append(1)
        else:
            testmat.append(0)
        cnt += 1
        testval = binary_to_decimal(testmat)
    cnt = 0
    if testval > ov:
        for x in matrix[r]:
            if x == 0:
                matrix[r][cnt] = 1
            else:
                matrix[r][cnt] = 0
            cnt += 1
        print("Toggled  Row ",r)
        ShowMatrix()


def ToggleCol(c):
    ov = GetColVal(c)
    row = 0
    testmat = []
    while row < 3:
        if matrix[row][c] == 0:
            testmat.append(1)
        else:
            testmat.append(0)
        row += 1
        testval = binary_to_decimal(testmat)
    if testval > ov:
        for x in range(3):     #[0,1,2]:
            matrix[x][c] = testmat[x]
        print("Toggled column ",c)
        ShowMatrix()

def binary_to_decimal(binary_array):
    decimal = 0
    power = len(binary_array) - 1
    for digit in binary_array:
        decimal += digit * (2 ** power)
        power -= 1
    return decimal

def GetColVal(c):
    testmat = []
    row = 0
    while row < 3:
        testmat.append(matrix[row][c])
        row += 1
    colval = binary_to_decimal(testmat)
    return colval

############################################
ShowMatrix()
for myrow in range(3):     #[0,1,2]:
    bd = binary_to_decimal(matrix[myrow])
    ToggleRow(myrow, bd)

for mycol in range(4):     #[0,1,2,3]:
    ToggleCol(mycol)

#---------------------------------------------
# SAMPLE OUTPUT
# python .\MatrixScore.py
# [0, 0, 1, 1]
# [1, 0, 1, 0]
# [1, 1, 0, 0]
# Total =  25

# Toggled  Row  0
# [1, 1, 0, 0]
# [1, 0, 1, 0]
# [1, 1, 0, 0]
# Total =  34

# Toggled column  2
# [1, 1, 1, 0]
# [1, 0, 0, 0]
# [1, 1, 1, 0]
# Total =  36

# Toggled column  3
# [1, 1, 1, 1]
# [1, 0, 0, 1]
# [1, 1, 1, 1]
# Total =  39
#---------------------------------------------