Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. """ 1.) Understand the problem
  2. Take two lists, and write a program that returns a list that contains only the elements that are common between the lists
  3. (without duplicates).
  4. Make sure your program works on two lists of different sizes.
  5.  
  6. 2.) Plan a solution
  7. Algorithm:
  8. - Change previous lists into sets
  9. - Create a new list
  10. - Make a for loop to iterate over one of the list
  11. - if the element in first list is the same as the element in the last
  12. - append to the new list
  13. - Change new list into set
  14. - Print new list
  15.  
  16.  
  17.  
  18.  
  19. 3.) Carry out the plan
  20. """
  21. a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
  22.  
  23. set_a = set(a)
  24.  
  25. b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
  26.  
  27. set_b = set(b)
  28.  
  29. c = []
  30.  
  31.  
  32. for x, y in zip(a, b):
  33.  
  34. if x in b:
  35.  
  36. c.append(x)
  37.  
  38.  
  39. c = set(c)
  40.  
  41. print(c)
  42.  
  43.  
  44.  
  45. """
  46. 4.) Examine your results for accuracy:
  47.  
  48. Input:
  49. a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
  50.  
  51. b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
  52.  
  53.  
  54.  
  55. Output:
  56. c = [1, 2, 3, 5, 8, 13]
  57. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement