Advertisement
Guest User

Loeng 4

a guest
Sep 24th, 2013
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. a = "12345"
  2. b = "234"
  3.  
  4. def reverse_string(s):
  5.     # TODO: to implement
  6.     return "54321"
  7.  
  8. def string_to_list(s):
  9.     # TODO: to implement
  10.     return [5, 4, 3, 2, 1]
  11.  
  12. def sum_lists(al, bl):
  13.     """
  14.    both parameters al and bl are represented in reversed order:
  15.    12345 is passed as [5, 4, 3, 2, 1]
  16.    So, the example is 12345 + 999999
  17.    The result is also in the reversed order.
  18.    In the case of the example: 1012344: [4, 4, 3, 2, 1, 0, 1]
  19.    """
  20.     if len(al) < len(bl):
  21.         return sum_lists(bl, al)
  22.    
  23.     print(al)
  24.     print(bl)
  25.    
  26.     result = []
  27.     carry = 0
  28.    
  29.     for i in range(len(bl)):
  30.         t = al[i] + bl[i] + carry
  31.         if t >= 10:
  32.             t = t - 10
  33.             carry = 1
  34.         else:
  35.             carry = 0
  36.        
  37.         result.append(t)
  38.    
  39.     for i in range(len(bl), len(al)):
  40.         t = al[i] + carry
  41.         if t >= 10:
  42.             t = t - 10
  43.             carry = 1
  44.         else:
  45.             carry = 0
  46.            
  47.         result.append(t)
  48.    
  49.     if carry > 0:
  50.         result.append(carry)
  51.    
  52.     return result
  53.  
  54. at = reverse_string(a)
  55. al = string_to_list(at)
  56.  
  57. bt = reverse_string(b)
  58. bl = string_to_list(bt)
  59.  
  60. #print(al)
  61. #print(bl)
  62.  
  63. cl = sum_lists(al, [9, 9, 9, 9, 9, 9])
  64.  
  65. # cl ümber pöörata
  66. # teha string
  67.  
  68. print(cl)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement