美文网首页
习题8:它们一样吗?

习题8:它们一样吗?

作者: 今年说话算话 | 来源:发表于2017-02-21 22:10 被阅读0次

Given two arrays a and b

Given two arrays a and b write a function comp(a, b) (compSame(a, b) in Clojure) that checks whether the two arrays have the "same" elements, with the same multiplicities. "Same" means, here, that the elements in b are the elements in a squared, regardless of the order.
def comp(array1, array2):
    # your code
    for i in array1:
        if i**2 not in array2 or array1.count(i) != array2.count(i**2):
            return False
    return True
def comp(array1, array2):
    return None in (array1, array2) and [i**2 for i in sorted(array1)] == sorted(array2)

相关文章

网友评论

      本文标题:习题8:它们一样吗?

      本文链接:https://www.haomeiwen.com/subject/sewuwttx.html