Advertisement
joric

ballmer.py

Jun 15th, 2019 (edited)
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.39 KB | None | 0 0
  1. '''
  2. www.bloomberg.com/news/videos/2016-11-07/steve-ballmer-on-how-to-pass-an-interview
  3. I'm thinking of a number between 1 and 100. You can guess, after each guess I'll tell you
  4. whether you're high or low. You get it at the first guess, I give you 5 bucks, 4 bucks, 3, 2, 1, 0,
  5. you pay me a buck, you pay me 2, you pay me 3. And the question is: do you want to play or not?
  6. What is your answer?
  7. '''
  8.  
  9. import math, random
  10.  
  11. def binary_search(n, x, custom_start=0):
  12.     left = 1
  13.     right = n
  14.     count = 1
  15.     while left < right:
  16.         mid = custom_start if custom_start>0 and count==1 else (left + right) / 2
  17.         if x==mid:
  18.             break
  19.         if x<mid:
  20.             right = mid - 1
  21.         else:
  22.             left = mid + 1
  23.         count += 1
  24.     return count
  25.  
  26. def simulation(n, margin, games, ballmer_set, player_set, desc, stat):
  27.     total_guesses = 0
  28.     money = 0
  29.     for i in range(games):
  30.         guesses = binary_search(n, random.choice(ballmer_set), random.choice(player_set))
  31.         money += margin - guesses
  32.         total_guesses += guesses
  33.     stat.append((desc, n, margin, games, money, total_guesses))
  34.  
  35. def calc_bad_numbers(n, margin):
  36.     bad_numbers = []
  37.     for k in range(1, n+1):
  38.         if binary_search(n, k) > margin:
  39.             bad_numbers.append(k)
  40.     return bad_numbers if bad_numbers!=[] else [n]
  41.  
  42. def calc_good_starts(n, bad_numbers, margin):
  43.     good_starts = []
  44.     for start in range(1, n+1):
  45.         guesses = 0
  46.         for x in bad_numbers:
  47.             guesses += binary_search(n, x, start)
  48.         if guesses/float(len(bad_numbers)) <= margin:
  49.             good_starts.append(start)
  50.     good_starts = list(set(good_starts))
  51.     return good_starts if good_starts!=[] else [0]
  52.  
  53. stat = []
  54. for n in [5, 9, 20, 100]:
  55.     games = 10000
  56.     numbers = range(1, n+1)
  57.     starts = [0]
  58.     margin = math.ceil(math.log(n, 2))-1
  59.     bad_numbers = calc_bad_numbers(n, margin)
  60.     good_starts = calc_good_starts(n, bad_numbers, margin)
  61.     simulation(n, margin, games, numbers, starts, 'naive vs naive', stat)
  62.     simulation(n, margin, games, bad_numbers, starts, 'smart vs naive', stat)
  63.     simulation(n, margin, games, bad_numbers, good_starts, 'smart vs smart', stat)
  64.     simulation(n, margin, games, numbers, good_starts, 'naive vs smart', stat)
  65.  
  66. fmt1 = "%-14s % 8s % 8s % 8s % 10s % 10s % 10s % 10s % 12s"
  67. print(fmt1 % ('desc','n','guesses','margin','games','money','avg.money','avg.guess','penalties'))
  68. fmt2 = "%-14s % 8d % 8d % 8d % 10d % 10d % 10.2f % 10.2f %12s"
  69. for desc, n, margin, games, money, guesses in stat:
  70.     penalty = '%d/%d' % (1+n-2**margin, n)
  71.     print(fmt2 % (desc, n, margin+1, margin, games, money, money/float(games), guesses/float(games), penalty))
  72.  
  73. '''
  74. desc                  n  guesses   margin      games      money  avg.money  avg.guess    penalties
  75. naive vs naive        5        3        2      10000      -5952      -0.60       2.60          2/5
  76. smart vs naive        5        3        2      10000     -10000      -1.00       3.00          2/5
  77. smart vs smart        5        3        2      10000          4       0.00       2.00          2/5
  78. naive vs smart        5        3        2      10000      -2003      -0.20       2.20          2/5
  79. naive vs naive        9        4        3      10000      -6574      -0.66       3.66          2/9
  80. smart vs naive        9        4        3      10000     -10000      -1.00       4.00          2/9
  81. smart vs smart        9        4        3      10000       1283       0.13       2.87          2/9
  82. naive vs smart        9        4        3      10000        -43      -0.00       3.00          2/9
  83. naive vs naive       20        5        4      10000     -10000      -1.00       5.00         5/20
  84. smart vs naive       20        5        4      10000     -10000      -1.00       5.00         5/20
  85. smart vs smart       20        5        4      10000     -10000      -1.00       5.00         5/20
  86. naive vs smart       20        5        4      10000     -10000      -1.00       5.00         5/20
  87. naive vs naive      100        7        6      10000     -10000      -1.00       7.00       37/100
  88. smart vs naive      100        7        6      10000     -10000      -1.00       7.00       37/100
  89. smart vs smart      100        7        6      10000     -10000      -1.00       7.00       37/100
  90. naive vs smart      100        7        6      10000     -10000      -1.00       7.00       37/100
  91. '''
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement