Created
March 2, 2020 14:20
-
-
Save bartaelterman/68591f7b2da4168fb6224472d6cad632 to your computer and use it in GitHub Desktop.
Simulate the number of out of order counts with varying list sizes and varying number of item delayed
This file contains hidden or 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
import random | |
import pprint | |
def get_ooo_count(l): | |
count = 0 | |
for i, el in enumerate(l[1:]): | |
if el != l[i] + 1: | |
count += 1 | |
return count | |
def randomly_delay_message(l): | |
random_i = random.randint(0, len(l) - 1) | |
random_new_pos = random.randint(random_i, len(l) - 1) | |
el = l.pop(random_i) | |
l.insert(random_new_pos, el) | |
def simulate_ooo_scores(n_variations, list_size, max_number_of_items_delay): | |
ooo_scores = {} | |
for number_of_items_delay in range(1, max_number_of_items_delay): | |
scores = [] | |
for v in range(n_variations): | |
l = list(range(list_size)) | |
for i in range(number_of_items_delay): | |
randomly_delay_message(l) | |
scores.append(get_ooo_count(l)) | |
ooo_scores[number_of_items_delay] = sum(scores) / len(scores) | |
return ooo_scores | |
pprint.pprint(simulate_ooo_scores(n_variations=30, list_size=40, max_number_of_items_delay=10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment