fork download
  1. #!/usr/bin/env python32
  2.  
  3. # from __future__ import division
  4. from functools import lru_cache
  5.  
  6. def lose_condition(x):
  7. return x<1
  8.  
  9. def action_A(x):
  10. return x-1
  11.  
  12. def action_B(x):
  13. return x/2
  14.  
  15. ACTIONS = (action_A, action_B)
  16.  
  17. @lru_cache(maxsize=100)
  18. def detect_winner(n, cond=lose_condition, actions=ACTIONS):
  19. if cond(n):
  20. return True
  21. return not all(detect_winner(action(n)) for action in actions)
  22.  
  23. if __name__ == '__main__':
  24.  
  25. assert not detect_winner(1)
  26. assert not detect_winner(1.5)
  27. assert detect_winner(2)
  28. assert detect_winner(2.5)
  29. assert detect_winner(3)
  30. assert not detect_winner(4)
  31. assert detect_winner(5)
  32. assert not detect_winner(6)
  33. print("OK")
  34.  
Runtime error #stdin #stdout 0.02s 5808KB
stdin
Standard input is empty
stdout
Standard output is empty