Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. import random
  2.  
  3. def getmin(data, start, end, total_items):
  4. if total_items == 1:
  5. #for sure we have a fake coin
  6. return (0, start)
  7. elif total_items == 2:
  8. if data[start] > data[end]:
  9. return (1, end)
  10. elif data[start] < data[end]:
  11. return (1, start)
  12. else:
  13. partition = total_items/3
  14. a_weight = sum(data[start:start+partition])
  15. b_weight = sum(data[start+partition:start+2*partition])
  16. c_weight = sum(data[start+2*partition:end])
  17. if a_weight == b_weight:
  18. result = getmin(data, start+2*partition, end, end-(start+2*partition))
  19. return (1+result[0], result[1])
  20. else:
  21. if a_weight > b_weight:
  22. result = getmin(data, start+partition, start+2*partition, partition)
  23. return (1+result[0], result[1])
  24. else:
  25. result = getmin(data, start, start+partition, partition)
  26. return (1+result[0], result[1])
  27.  
  28. n = int(raw_input())
  29. data = [1]*n
  30. data[random.randint(0, n-1)] = 0
  31. total_weighing, position = getmin(data, 0, len(data), len(data))
  32. print(total_weighing, position)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement