【每日一题0105】相同元素

:woman_mage: 给定2个数字列表,请编写一个函数,当它们至少拥有1个相同元素时返回True,否则返回False。

示例:
输入[9, 8, 7],[8, 1, 3],输出:True。

题目难度:简单
题目来源:CodeWars:Identical Elements

def solution(nums1: list, nums2: list) -> bool:
    # your code

assert solution([9, 8, 7], [8, 1, 3]) == True
assert solution([1, 2, 3, 4, 5], [1, 6, 7, 8, 9]) == True
assert solution([1, 3, 5], [2, 4, 6]) == False
def solution(nums1: list, nums2: list) -> bool:
    return all((len([i for i in nums1 if i in nums2]),))


assert solution([9, 8, 7], [8, 1, 3]) == True
assert solution([1, 2, 3, 4, 5], [1, 6, 7, 8, 9]) == True
assert solution([1, 3, 5], [2, 4, 6]) == False
def solution(nums1: list, nums2: list) -> bool:
    nums1.extend(nums2)
    nums = list(set(nums1))
    return False if len(nums) == len(nums1)else True

assert solution([9, 8, 7], [8, 1, 3]) == True
assert solution([1, 2, 3, 4, 5], [1, 6, 7, 8, 9]) == True
assert solution([1, 3, 5], [2, 4, 6]) == False
def solution(nums1: list, nums2: list) -> bool:
    return set(nums1)&set(nums2)!=set()

assert solution([9, 8, 7], [8, 1, 3]) == True
assert solution([1, 2, 3, 4, 5], [1, 6, 7, 8, 9]) == True
assert solution([1, 3, 5], [2, 4, 6]) == False
2 Likes
def solution(nums1: list, nums2: list) -> bool:
    return False if set(nums1).intersection(set(nums2)) == set() else True
def solution(num: list, num1: list):
    for i in range(0, len(num)):
        for j in range(0, len(num1)):
            if num[i] == num1[j]:
                return True
    return False


assert solution([9, 8, 7], [8, 1, 3]) == True
assert solution([1, 2, 3, 4, 5], [1, 6, 7, 8, 9]) == True
assert solution([1, 3, 5], [2, 4, 6]) == False
def solution(nums1: list, nums2: list) -> bool:
    return len(set(nums1+nums2))<len(nums1+nums2)

assert solution([9, 8, 7], [8, 1, 3]) == True
assert solution([1, 2, 3, 4, 5], [1, 6, 7, 8, 9]) == True
assert solution([1, 3, 5], [2, 4, 6]) == False
1 Like
def solution(nums1: list, nums2: list) -> bool:
    # 对列表1进行循环查找,如果列表1中的元素在列表2中存在,则将该元素加入到列表num中
    num = [i for i in nums1 if i in nums2]
    # 判断生成的num列表中是否有元素,如果没有则返回False,如果有元素,则返回True
    return bool(len(num))
assert solution([9, 8, 7], [8, 1, 3]) == True
assert solution([1, 2, 3, 4, 5], [1, 6, 7, 8, 9]) == True
assert solution([1, 3, 5], [2, 4, 6]) == False
def solution(nums1: list, nums2: list) -> bool:
    for i in nums1:
        for j in nums2:
            if i == j:
                return True
    else:
        return False
1 Like
def solution14(nums1:list,nums2:list):
    if set(nums1) & set(nums2):
        return True
    else:
        return False

assert solution14([9, 8, 7], [8, 1, 3]) == True
assert solution14([1, 2, 3, 4, 5], [1, 6, 7, 8, 9]) == True
assert solution14([1, 3, 5], [2, 4, 6]) == False
def solution(nums1: list, nums2: list) -> bool:
    return sum(1 for i in nums2 if i in nums1)>0
def solution(nums1: list, nums2: list) -> bool:
    for i in nums1:
        if i in nums2:
            return True
    return False


assert solution([9, 8, 7], [8, 1, 3]) == True
assert solution([1, 2, 3, 4, 5], [1, 6, 7, 8, 9]) == True
assert solution([1, 3, 5], [2, 4, 6]) == False
def solution(nums1: list, nums2: list) -> bool:
    for x in nums1:
        if x in nums2:
            return True
    return False


assert solution([9, 8, 7], [8, 1, 3]) == True
assert solution([1, 2, 3, 4, 5], [1, 6, 7, 8, 9]) == True
assert solution([1, 3, 5], [2, 4, 6]) == False
def solution(nums1: list, nums2: list) -> bool:
    return False if len(set(nums1) & set(nums2)) == 0 else True
assert solution([9, 8, 7], [8, 1, 3]) == True
assert solution([1, 2, 3, 4, 5], [1, 6, 7, 8, 9]) == True
assert solution([1, 3, 5], [2, 4, 6]) == False
关闭