[6 kyu] Are they the “same”?
文章目录
Question
Sample Tests
My Answer (weak accepted)
def comp(array1, array2):
print(array1)
print(array2)
if (array1 is None and not array2) or (array2 is None and not array1):
print("False")
return False
if ((not array1) and array2) or ((not array2) and array1):
print("False")
return False
if len(array1) != len(array2):
print("False")
return False
# judge
for item in array1:
if item**2 in array2:
array2.remove(item**2)
else:
print("False")
return False
print("True")
return True
Suggested Answer
def comp(array1, array2):
try:
return sorted([i ** 2 for i in array1]) == sorted(array2)
except:
return False
def comp(a1, a2):
return None not in (a1,a2) and [i*i for i in sorted(a1)]==sorted(a2)
def comp(array1, array2):
if array1 and array2:
return sorted([x*x for x in array1]) == sorted(array2)
return array1 == array2 == []
Thinking
The boundary problem is really confusing!