How to check if elements of a multiplication table are all associative?

375 Views Asked by At

I want to check if a multiplication table like the following is not a code but I could not find a way to write it another way

*  0 1 2
0 |0 1 0
1 |0 0 0 
2 |0 1 1

It is store as a list with sublists in my code like

[[0,1,0], [0,0,0], [0,1,1]]

#How to write a function which checks elements satisfy the condition x*(y*z) = (x*y)*z ? 

#This is what I have so far: 

def is_associative(X):
    n = len(X)
    a = []
    for i in range(n):
        a.append[i]
        for j in range(n):
            a[j]*a[j] = X[j]
1

There are 1 best solutions below

0
On

You should loop over all three variables:

for x in range(n):
    for y in range(n):
        for z in range(n):

And then you need to check if

X[X[x, y], z] == X[x, X[y, z]]

for all values.