本帖最后由 qq1151985918 于 2021-5-1 20:55 编辑 def f(m: int, n: int, direction: "str in ['L','R']"):
if direction not in ["L", "l", "R", "r"]:
raise Exception
length = len(str(m)) if len(str(m)) > len(str(n)) else len(str(n))
str_m, str_n = str(m).zfill(length), str(n).zfill(length)
if [str_m.isdigit(), str_n.isdigit()] != [True, True]:
raise TypeError
list_m, list_n, list_sum = [str_m], [str_n], [int(str_m) + int(str_n)]
data = range(1, length) if direction in ["L", "l"] else range(length - 1, 0, -1)
for i in data:
new_m = str_m[i:] + str_m[:i]
new_n = str_n[i:] + str_n[:i]
list_m.append(new_m)
list_n.append(new_n)
list_sum.append(int(new_m) + int(new_n))
length_max_sum = len(str(max(list_sum)))
for m, n, s in zip(list_m, list_n, list_sum):
print(m, "plus", n, "equals", str(s).zfill(length_max_sum))
f(800095, 900003, 'L')
f(8901, 3419306, 'R')
|