Created
April 30, 2024 17:35
-
-
Save Gerst20051/5cf1b22785beb2eeaf011f5080c1e49c to your computer and use it in GitHub Desktop.
Shuffle Array Fairness
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
import random | |
unshuffled_array = [1, 2, 3, 4, 5] | |
def shuffle_array_v1(arr): | |
output = [] | |
count_of_items_in_arr = len(arr) | |
while count_of_items_in_arr: | |
random_index = random.randint(0, count_of_items_in_arr - 1) | |
item = arr[random_index] | |
del arr[random_index] | |
output.append(item) | |
count_of_items_in_arr = len(arr) | |
return output | |
shuffled_array_v1 = shuffle_array_v1(unshuffled_array) | |
print(shuffled_array_v1) | |
def shuffle_array_v2(arr): | |
for idx, item in enumerate(arr): | |
random_index = random.randint(idx, len(arr) - 1) | |
arr[random_index], arr[idx] = arr[idx], arr[random_index] | |
return arr | |
shuffled_array_v2 = shuffle_array_v2(unshuffled_array) | |
print(shuffled_array_v2) | |
def get_permutations(arr): | |
return [] | |
def shuffle_fairness(fn): | |
test_array = [0,1,2] | |
permutations = get_permutations(test_array) | |
shuffled_array = fn(test_array) | |
print(shuffled_array) | |
permutations_hash = { | |
"[0,1,2]": 0, | |
"[2,1,0]": 0, | |
} | |
return 0 | |
shuffle_fairness(shuffle_array_v1) | |
shuffle_fairness(shuffle_array_v2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment