Python练习题答案: 婴儿喜鹊【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战

婴儿喜鹊【难度:2级】:

答案1:

def child(bird1, bird2):
    return 1 <= sum(i != j for i, j in zip(bird1, bird2)) <= 2 

def grandchild(bird1, bird2):
    return bird1 == bird2 or sum(i != j for i, j in zip(bird1, bird2)) <= 4 and len(bird1) > 1

答案2:

def diffs(bird1, bird2):
    return sum(c1 != c2 for c1, c2 in zip(bird1, bird2))

def child(bird1, bird2):
    return diffs(bird1, bird2) in [1, 2]

def grandchild(bird1, bird2):
    return diffs(bird1, bird2) in [0, 1, 2, 3, 4] if len(bird1) > 1 else bird1 == bird2​

答案3:

def diff(bird1, bird2):
    return sum(c1 != c2 for c1, c2 in zip(bird1, bird2))

def child(bird1, bird2):
    return 1 <= diff(bird1, bird2) <= 2

def grandchild(bird1, bird2):
    d = diff(bird1, bird2)
    if len(bird1) == 1:
        return d == 0
    return 0 <= d <= 4

答案4:

from operator import ne

def child(bird1, bird2):
    return 1 <= sum(map(ne, bird1, bird2)) <= 2

def grandchild(bird1, bird2):
    return sum(map(ne, bird1, bird2)) <= 4 and bird1 + bird2 not in 'BWB'

答案5:

def d(a, b):
    return sum(x != y for x, y in zip(a, b))

def child(a, b):
    return 0 < d(a, b) < 3

def grandchild(a, b):
    return d(a, b) < 5 and a + b not in 'BWB'

答案6:

def child(bird1, bird2):
    return sum(one != two for one, two in zip(bird1, bird2)) in (2, 1)

def grandchild(bird1, bird2):
    if len(bird1) != 1:
        return sum(one != two for one, two in zip(bird1, bird2)) in range(5)
    
    return bird1 == bird2​

答案7:

def child(bird1, bird2):
    dif = 0
    for x, y in zip(bird1, bird2):
        if x != y:
            dif += 1
    if dif in [1, 2]:
        return True
    else: 
        return False

def grandchild(bird1, bird2):
    dif = 0
    for x, y in zip(bird1, bird2):
        if x != y:
            dif += 1
    if len(bird1) == 1:
        if dif == 0:
            return True
        else:
            return False
    if dif in [0,1,2,3,4]:
        return True
    else: 
        return False

答案8:

f=lambda *A:sum(a!=b for a,b in zip(*A))
child=lambda *A:0<f(*A)<3
grandchild=lambda A,B:A==B or 1<len(A)and f(A,B)<5

答案9:

f=lambda A,B:sum(a!=b for a,b in zip(A,B))
child=lambda A,B:0<f(A,B)<3
grandchild=lambda A, B:not(len(A)==len(B)==1 and A!=B)and f(A,B)<5

答案10:

def child(bird1, bird2):
    return sum(i!=j for i,j in zip(bird1,bird2)) in [1,2]

def grandchild(bird1, bird2):
    s = sum(i!=j for i,j in zip(bird1,bird2))
    return s in range(5) if len(bird1) > 1 else not s​




Python基础训练营景越Python基础训练营QQ群

Python练习题答案: 婴儿喜鹊【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战_第1张图片
欢迎各位同学加群讨论,一起学习,共同成长!

你可能感兴趣的:(python面试题库和答案,python编程练习,算法,Python编程进阶练习题)