fork download
  1. from collections import Counter
  2. from random import randint
  3. from matplotlib import pyplot
  4.  
  5.  
  6. def call_counter(func):
  7. def helper(*args, **kwargs):
  8. helper.calls += 1
  9. return func(*args, **kwargs)
  10. helper.calls = 0
  11. return helper
  12.  
  13.  
  14. def random_guess(options):
  15. res = []
  16.  
  17. for num_options in options:
  18. res.append(randint(0, num_options))
  19.  
  20. return res
  21.  
  22.  
  23. @call_counter
  24. def correct_answers(guess, solution):
  25. correct = 0
  26. for g, s in zip(guess, solution):
  27. if g == s:
  28. correct += 1
  29. return correct
  30.  
  31.  
  32. def play_game(possible_responses, solution, strategy):
  33. correct_answers.calls = 0
  34. strategy(possible_responses, solution)
  35. return correct_answers.calls
  36.  
  37.  
  38. def xurdones_strategy(possible_responses, solution):
  39. guess = random_guess(possible_responses)
  40. change_idx = 0
  41. old_correct = correct_answers(guess, solution)
  42. while old_correct != len(solution):
  43. old_guess = guess[change_idx]
  44. guess[change_idx] = (guess[change_idx] + 1) % (possible_responses[change_idx]+1)
  45. new_correct = correct_answers(guess, solution)
  46.  
  47. if new_correct < old_correct:
  48. guess[change_idx] = old_guess
  49. elif new_correct > old_correct:
  50. old_correct = new_correct
  51. if new_correct != old_correct:
  52. change_idx += 1
  53.  
  54.  
  55. def random_strategy(possible_responses, solution):
  56. while correct_answers(random_guess(possible_responses), solution) != len(solution):
  57. pass
  58.  
  59.  
  60. def run_tests(n):
  61. possible_responses = [1, 2, 1, 2, 1, 2]
  62. solution = [0, 2, 0, 1, 1, 0]
  63.  
  64. figure = pyplot.figure()
  65.  
  66. # My strategy
  67. num_rounds_xur = []
  68. num_rounds_rand = []
  69. for _ in range(n):
  70. num_rounds_xur.append(play_game(possible_responses, solution, xurdones_strategy))
  71. num_rounds_rand.append(play_game(possible_responses, solution, random_strategy))
  72. print(Counter(num_rounds_xur))
  73. print(Counter(num_rounds_rand))
  74.  
  75. ax1 = figure.add_subplot(211)
  76. ax1.hist(num_rounds_xur)
  77.  
  78. ax2 = figure.add_subplot(212)
  79. ax2.hist(num_rounds_rand)
  80. pyplot.show()
  81.  
  82.  
  83. if __name__ == '__main__':
  84. run_tests(1000000)
  85.  
Time limit exceeded #stdin #stdout #stderr 5s 66920KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Unable to init server: Could not connect: Connection refused
Unable to init server: Could not connect: Connection refused

(prog:31903): Gdk-CRITICAL **: 18:15:05.258: gdk_cursor_new_for_display: assertion 'GDK_IS_DISPLAY (display)' failed

(prog:31903): Gdk-CRITICAL **: 18:15:05.269: gdk_cursor_new_for_display: assertion 'GDK_IS_DISPLAY (display)' failed