fork download
  1. def missing2(list1, list2):
  2. list1 = sorted(list1)
  3. list2 = sorted(list2)
  4. missing = []
  5. while list1 and list2:
  6. if list1[0] <= list2[0]:
  7. x = list1.pop(0)
  8. if x < list2[0]:
  9. missing.append(x)
  10. else:
  11. list2.pop(0)
  12. return missing + list1
  13.  
  14. def missing3(a, b):
  15. setb = set(b)
  16. return [ai for ai in a if ai not in setb]
  17.  
  18. def missing2S(list1, list2):
  19. list1 = sorted(list1)
  20. list2 = sorted(list2)
  21. missing = []
  22. y=0
  23. for x in list1:
  24. while list2 and y < x:
  25. y = list2.pop(0)
  26. if y > x:
  27. missing.append(x)
  28. return missing
  29.  
  30.  
  31. from random import randrange
  32. for n in range(20):
  33. b = [randrange(1, 100) for i in range(5)]
  34. a = b + [randrange(1, 100)]
  35. m1, m2 = missing3(a, b), missing2S(a, b)
  36. if m1 != m2:
  37. print(n, m1, m2, sorted(a), sorted(b))
  38.  
  39.  
  40.  
Success #stdin #stdout 0.02s 12368KB
stdin
Standard input is empty
stdout
1 [94] [] [10, 17, 29, 67, 89, 94] [10, 17, 29, 67, 89]
7 [99] [] [20, 27, 43, 90, 92, 99] [20, 27, 43, 90, 92]
13 [69] [] [6, 20, 23, 45, 50, 69] [6, 20, 23, 45, 50]
16 [90] [] [23, 32, 42, 66, 74, 90] [23, 32, 42, 66, 74]