I have written a Python function called comp
that checks whether two given arrays have the same elements, with the same multiplicities. The multiplicity of a member is the number of times it appears in the array. I would like to post my code here for review, so that I can optimize it and receive any advice on how to improve it.
Here's the code for the function:
def comp(array1, array2):
c = 0
ta = []
if len(array1) == 0 or len(array2) == 0:
return None
for i in range(len(array2)):
for j in range(len(array2)):
if array2[i] != array1[j] * array1[j]:
c += 1
ta.append(c)
c = 0
if len(array1) in ta:
return False
else:
return True
I would appreciate any suggestions or advice on how to improve the code's efficiency, readability, or any other best practices that I might be missing.
comp([1,2], [1,2])
should return True, but it returns False. \$\endgroup\$array1
? This code makes no sense to me, and no comments explain why it's doing it that way. It's highly non-obvious how this algorithm works, although I guess the idea of looking forlen(array1)
inta[]
is that it's counting non-matches, so that would find a case where nothing matched? Seems weird. \$\endgroup\$