Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. """
  2. [10, 20, 30, 40, 50]
  3. [15, 20, 35, 50, 76, 190]
  4.  
  5. idx_a 2
  6. idx_b 2
  7.  
  8. --> [20, 50]
  9.  
  10. [10, 20]
  11. []
  12.  
  13. --> []
  14. """
  15.  
  16. def intersect_lists(list_a, list_b):
  17.     idx_a, idx_b = 0, 0
  18.     result = []
  19.     while idx_a < len(list_a) and idx_b < len(list_b):
  20.         num_a, num_b = list_a[idx_a], list_b[idx_b]
  21.        
  22.         if num_a == num_b:
  23.             result.append(num_a)
  24.             idx_a += 1
  25.             idx_b += 1
  26.         elif num_a < num_b:
  27.             idx_a += 1
  28.         else:
  29.             idx_b += 1
  30.            
  31.     return result
  32.    
  33. print(intersect_lists([10, 20, 30, 40, 50], [15, 20, 35, 50, 76, 190]))
  34. print(intersect_lists([10, 20], []))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement