习题8:它们一样吗?

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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,484评论 0 23
  • 回首过往,那些忘不却的瞬间温暖,流淌在我们的心田。初恋这件小事,是我们心头永远除不去的伤痛,但却会一直存留在我们的...
    流星王小莫阅读 2,548评论 0 3
  • 亲爱的书琴:你好,好多年不见了,你现在还好吗?之前听说你去了牙买加,做了大使夫人,还有了孩子……现在呢?还在牙买加...
    江湖人称二爷阅读 1,527评论 5 2
  • 孟子从齐过再度回来,路过滕地,滕文公问孟子,像它这样小的国家是投靠齐国还是投靠南面的楚国,孟子回复腾文公说,你把自...
    艺萍阅读 4,502评论 0 0
  • 长得丑是怎么一种体验?估计一年前的我很有发言权。长得丑意味着,机会在你眼前你都抓不住。不想改变?却因为这个看脸的时...
    Jane婷阅读 3,081评论 2 1