Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. import math
  2.  
  3. binaryLen = lambda n: math.floor(math.log2(n)) + 1
  4.  
  5. def findNextMx(mx, ar , exclude_list):
  6. '''Find the next max of same bin len as mx.'''
  7. logmx = binaryLen(mx)
  8. nextmx = float('-inf')
  9. for n in ar:
  10. if n not in exclude_list and n > nextmx:
  11. nextmx = n
  12. if binaryLen(nextmx) != logmx:
  13. mx = nextmx
  14. return findNextMx(nextmx, ar, exclude_list + [nextmx])
  15. return mx, nextmx
  16.  
  17. def maxAnd(ar):
  18. mx = max(ar)
  19. mx, nextmx = findNextMx(mx, ar , [mx])
  20. return mx & nextmx
  21.  
  22. def maxOR(ar):
  23. mx = max(ar)
  24. mx, nextmx = findNextMx(mx, ar , [mx])
  25. return mx | nextmx
  26.  
  27. def sumMaxAndOr(ar):
  28. mx = max(ar)
  29. mx, nextmx = findNextMx(mx, ar , [mx])
  30. return (mx & nextmx) + (mx | nextmx)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement