Advertisement
1sairandhri

adding numbers in two lists

Apr 8th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.42 KB | None | 0 0
  1. #a = [9,9,9]
  2. #b = [1,1]
  3. #[1, 0, 2, 0]
  4. def sol(a,b):
  5.     a,b = a[::-1], b[::-1]
  6.     if len(b)>len(a):
  7.         a,b = b,a
  8.     b.extend([0]*(len(a)-len(b)))
  9.     res = [0]*len(a)
  10.     for i in range(len(a)-1):
  11.         temp=res[i]+a[i]+b[i]
  12.         res[i]+=temp%10
  13.         res[i+1]=temp//10  
  14.     temp = res[-1]+ a[-1]+b[-1]
  15.     res[-1] = temp%10
  16.     if temp>9:    
  17.         res.append(temp//10)
  18.     return res[::-1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement