Nov-01-2019, 11:51 PM
(This post was last modified: Nov-01-2019, 11:51 PM by newbieAuggie2019.)
(Nov-01-2019, 09:20 PM)masteripper Wrote: I am thinking that this might be slower...but thanks for replying.
Hi again!
My answer is probably quite verbose, and as I am a newbie, I'm sure that there must be better ways to do it than my code, but I made the program give a visual and self-explanatory output:
A = [[1, 0, 1, 1, 1, 0, 0, 1], [1, 0, 0, 1, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 0, 0], [1, 0, 0, 0, 1, 0, 0, 1]] B = [[0, 1, 0, 0, 1, 0, 0, 1], [1, 1, 0, 0, 0, 0, 0, 1], [1, 1, 1, 0, 0, 0, 0, 1], [1, 0, 1, 0, 1, 0, 0, 1]] C = [[1, 0, 1, 0, 1, 0, 0, 1], [1, 0, 1, 1, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 0, 1], [1, 1, 0, 0, 1, 0, 0, 1]] commonElementsAB = [] commonElementsAC = [] for i in range(4): for j in range(8): if (A[i][j] == B[i][j]) : commonElementsAB.append(A[i][j]) else : pass print("\nThis is matrix A:\n") for elements in A: print(*elements) print("\nThis is matrix B:\n") for elements in B: print(*elements) print(f"\nThe elements in common in the same \n\ position in matrices A and B are {len(commonElementsAB)}.\n\ These {len(commonElementsAB)} elements in common are: \n\ {commonElementsAB}.\n") for i in range(4) : for j in range(8) : if (A[i][j] == B[i][j]) : print(A[i][j], end = " ") else : print("*", end = " ") print() for x in range(4): for y in range(8): if (A[x][y] == C[x][y]) : commonElementsAC.append(A[x][y]) else : pass print("\nThis is matrix A:\n") for elements in A: print(*elements) print("\nThis is matrix C:\n") for elements in C: print(*elements) print(f"\nThe elements in common in the same \n\ position in matrices A and C are {len(commonElementsAC)}.\n\ These {len(commonElementsAC)} elements in common are: \n\ {commonElementsAC}.\n") for i in range(4) : for j in range(8) : if (A[i][j] == C[i][j]) : print(A[i][j], end = " ") else : print("*", end = " ") print() if len(commonElementsAB) == len(commonElementsAC): print(f"\nBoth matrices B and C have the same number of\n\ common elements with matrix A.") elif len(commonElementsAB) > len(commonElementsAC): print(f"\nMatrices A and B have a bigger number of\n\ common elements in the same position than\n\ matrices A and C.") elif len(commonElementsAB) < len(commonElementsAC): print(f"\nMatrices A and C have a bigger number of\n\ common elements in the same position than\n\ matrices A and B.")and the output is:
Output:This is matrix A:
1 0 1 1 1 0 0 1
1 0 0 1 1 0 0 1
1 0 1 0 0 0 0 0
1 0 0 0 1 0 0 1
This is matrix B:
0 1 0 0 1 0 0 1
1 1 0 0 0 0 0 1
1 1 1 0 0 0 0 1
1 0 1 0 1 0 0 1
The elements in common in the same
position in matrices A and B are 22.
These 22 elements in common are:
[1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1].
* * * * 1 0 0 1
1 * 0 * * 0 0 1
1 * 1 0 0 0 0 *
1 0 * 0 1 0 0 1
This is matrix A:
1 0 1 1 1 0 0 1
1 0 0 1 1 0 0 1
1 0 1 0 0 0 0 0
1 0 0 0 1 0 0 1
This is matrix C:
1 0 1 0 1 0 0 1
1 0 1 1 1 0 0 1
1 0 1 0 0 0 0 1
1 1 0 0 1 0 0 1
The elements in common in the same
position in matrices A and C are 28.
These 28 elements in common are:
[1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1].
1 0 1 * 1 0 0 1
1 0 * 1 1 0 0 1
1 0 1 0 0 0 0 *
1 * 0 0 1 0 0 1
Matrices A and C have a bigger number of
common elements in the same position than
matrices A and B.
>>>
All the best,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs