fork download
  1. from heapq import nlargest
  2. from random import Random
  3.  
  4. SCOREBOARD = 21, 8, 7, 2
  5.  
  6.  
  7. class GuessingGame:
  8. def __init__(
  9. self,
  10. player_names,
  11. seed
  12. ):
  13. self.player_names = player_names
  14. self.players_n = len(player_names)
  15. self.results = [0] * self.players_n
  16. self.cur_player = 0
  17. self.random = Random()
  18. self.random.seed(seed)
  19.  
  20. def print_results(self):
  21. for name, score in nlargest(
  22. 10,
  23. zip(
  24. self.player_names,
  25. self.results
  26. ),
  27. key=lambda x: x[1]
  28. ):
  29. print(f"{name}: {score}")
  30.  
  31. def score(self, amount):
  32. self.results[self.cur_player] += amount
  33.  
  34. def next_player(self):
  35. self.cur_player = (
  36. (self.cur_player + 1) %
  37. self.players_n
  38. )
  39.  
  40. def move(self, guess):
  41. assert 0 <= guess <= 10
  42. correct = self.random.randrange(11)
  43. error = abs(guess - correct)
  44. if error < len(SCOREBOARD):
  45. self.score(
  46. SCOREBOARD[error]
  47. )
  48. self.next_player()
  49.  
  50.  
  51. def get_oc():
  52. game = GuessingGame((
  53. "Sawyer",
  54. "Ben",
  55. "Kate",
  56. "Hugo",
  57. "John",
  58. "Boone",
  59. "Sayid"
  60. ), 2763)
  61. game.move(8)
  62. game.move(5)
  63. game.move(4)
  64. game.move(5)
  65. game.move(4)
  66. game.move(1)
  67. game.move(9)
  68. game.move(6)
  69. game.move(9)
  70. game.move(9)
  71. game.move(9)
  72. game.move(3)
  73. game.move(1)
  74. game.move(6)
  75. return game
  76.  
  77.  
  78. if __name__ == "__main__":
  79. get_oc().print_results()
Success #stdin #stdout 0.04s 9924KB
stdin
Standard input is empty
stdout
Boone: 42
Sayid: 42
John: 23
Kate: 16
Hugo: 15
Ben: 8
Sawyer: 4