Python练习题答案: 简单有趣#15:加法无需携带【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战

mac2026-03-31  5

简单有趣#15:加法无需携带【难度:2级】:

答案1:

from itertools import zip_longest def addition_without_carrying(a, b): s = ''.join( str((x + y) % 10) for x, y in zip_longest(map(int, str(a)[::-1]), map(int, str(b)[::-1]), fillvalue=0) ) return int(s[::-1])

答案2:

def addition_without_carrying(a, b): a, b = str(a), str(b) l = max(len(a), len(b)) a, b = a.zfill(l), b.zfill(l) return int(''.join(str((int(m) + int(n)) % 10) for m, n in zip(a, b)))

答案3:

def addition_without_carrying(a, b): sa, sb = str(a), str(b) lmax = max(len(sa), len(sb)) sa, sb = sa.zfill(lmax), sb.zfill(lmax) return int("".join(str((int(da) + int(db)) % 10) for da, db in zip(sa, sb)))

答案4:

addition_without_carrying=f=lambda a,b:a+b and f(a//10,b//10)*10+(a+b)%10

答案5:

from itertools import zip_longest def addition_without_carrying(num_a, num_b): digits = [str((int(a) + int(b)) % 10) for a, b in zip_longest(reversed(str(num_a)), reversed(str(num_b)), fillvalue='0')] return int(''.join(reversed(digits)))

答案6:

def addition_without_carrying(a,b): a = str(a) b = str(b) a = "0"*(len(b)-len(a)) + a b = "0"*(len(a)-len(b)) + b s = int(''.join(str((int(x)+int(y))%10) for x,y in zip(a,b))) return s​

答案7:

from itertools import zip_longest def addition_without_carrying(a, b): return int(''.join(str(int(x) + int(y))[-1] for x, y in zip_longest(str(a)[::-1], str(b)[::-1], fillvalue='0'))[::-1])

答案8:

def addition_without_carrying(a,b): if a < b: a, b = b, a A, B = str(a).zfill(6), str(b).zfill(6) return int(''.join([str((int(A[i]) + int(B[i])) % 10) for i in range(6)]))

答案9:

from itertools import zip_longest def addition_without_carrying(*args): args = [map(int, str(i)[::-1]) for i in args] return int(''.join(str(sum(i)%10) for i in zip_longest(*args, fillvalue=0))[::-1])

答案10:

def addition_without_carrying(a,b): #coding and coding.. #make them strings to index into them a = str(a)[::-1] b = str(b)[::-1] #workout the longest number maxLen = max(len(a), len(b)) #pad both to be the same length if len(a) < len(b): a+=("0"*(len(b)-len(a))) if len(b) < len(a): b+=("0"*(len(a)-len(b))) answer = [] for i in range(0, maxLen): answer.append(str(int(a[i])+int(b[i]))[-1]) print(int(''.join(answer[::-1]))) return int(''.join(answer[::-1]))

景越Python基础训练营QQ群

欢迎各位同学加群讨论,一起学习,共同成长!

最新回复(0)