Passed
Push — master ( ce962b...ffd958 )
by Ken M.
01:12
created

multiplication_table   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 7

3 Functions

Rating   Name   Duplication   Size   Complexity  
A Bit() 0 7 3
A checkio() 0 6 1
A CreateTable() 0 10 3
1
from operator import and_, or_, xor
2
3
4
def CreateTable(first, second):
5
    rows = len(first) + 1
6
    cols = len(second) + 1
7
    table = []
8
    for i in range(rows):
9
        if i == 0:
10
            table.append([''] + list(map(int, list(second))))
11
        else:
12
            table.append([int(first[i - 1])] + [''] * (cols - 1))
13
    return table
14
15
16
def Bit(first, second, operator):
17
    BitTable = CreateTable(first, second)
18
    for row in range(1, len(first) + 1):
19
        for col in range(1, len(second) + 1):
20
            BitTable[row][col] = operator(BitTable[0][col], BitTable[row][0])
21
    return sum(
22
        [int(''.join(map(str, BitTable[i][1:])), 2) for i in range(1, len(BitTable))]
23
    )
24
25
26
def checkio(first, second):
27
    return sum(
28
        [
29
            Bit(bin(first)[2:], bin(second)[2:], and_),
30
            Bit(bin(first)[2:], bin(second)[2:], or_),
31
            Bit(bin(first)[2:], bin(second)[2:], xor),
32
        ]
33
    )
34
35
36
# These "asserts" using only for self-checking and not necessary for
37
# auto-testing
38
if __name__ == '__main__':
39
    assert checkio(4, 6) == 38
40
    assert checkio(2, 7) == 28
41
    assert checkio(7, 2) == 18
42