Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. def func(A, B):
  4. # wypełniamy count liczbą wystąpień każdego
  5. # elementu z tablicy B
  6. count = {}
  7. for x in B:
  8. if x not in count:
  9. count[x] = 0
  10. count[x] += 1
  11.  
  12. # znajdujemy maksymalną liczbę wystąpień w tablicy B
  13. # wśród takich elementów, które należą do A, żeby
  14. # wiedzieć, o pierwszość jak dużych liczb będziemy
  15. # się pytać
  16. maxInAB = max(map(lambda x: count[x] if x in count else 0, A))
  17. isPrime = primes(maxInAB+1)
  18. C = []
  19.  
  20. for x in A:
  21. if x not in count or not isPrime[count[x]]:
  22. C.append(x)
  23.  
  24. return C
  25.  
  26. # zwraca tablicę t t. że:
  27. # t[n] == True jeśli n jest pierwsza,
  28. # t[n] == False w przeciwnym wypadku
  29. #
  30. # parametr n to rozmiar tablicy do zwrócenia
  31. def primes(n):
  32. if n < 2:
  33. return [False] * n
  34.  
  35. isPrime = n * [True]
  36. isPrime[0] = False
  37. isPrime[1] = False
  38.  
  39. i = 2
  40. while i * i < n:
  41. if isPrime[i]:
  42. j = i
  43. while i * j < n:
  44. isPrime[i*j] = False
  45. j += 1
  46. i += 1
  47.  
  48. return isPrime
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement