Last active
July 17, 2019 09:11
-
-
Save Ogaday/8979e9f536ecae3ac0b87203bc2e7a5b to your computer and use it in GitHub Desktop.
Expected updates when finding the max
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
""" | |
Simulation of the solution to a problem from a blog post by Nadbordrozd: | |
http://nadbordrozd.github.io/interviews/index.html | |
""" | |
from random import Random | |
def count_updates(x): | |
""" | |
Count the number of updates to the "max" placeholder | |
""" | |
max_num = x[0] | |
num_updates = 0 | |
for i in x[1:]: | |
if i > max_num: | |
max_num = i # Expected number of times this is evaluated | |
num_updates += 1 | |
return num_updates | |
def mean(x): | |
""" | |
Calculate arithmetic mean of an array | |
""" | |
return sum(x) / len(x) | |
def run_experiment(array_length, num_simulations, r=None): | |
""" | |
Compute the mean number of updates required to find the max of an array | |
""" | |
if r is None: | |
r = Random() | |
arrays = [list(range(array_length)) for _ in range(num_simulations)] | |
[r.shuffle(l) for l in arrays] | |
return mean([count_updates(l) for l in arrays]) | |
if __name__ == "__main__": | |
r = Random(x=42) | |
array_lengths = [10, 100, 1000, 10000] | |
num_simulations = 10000 | |
print(f"Running experiments using {num_simulations} simulations") | |
for n in array_lengths: | |
print(f"Simulating solution with N={n}:") | |
print(f"Average updates: {run_experiment(n, num_simulations, r)}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output:
Apparently, the solution for an array of length
n
is then
th harmonic number.For the experiment, these are:
Not too bad!