From 2cc5cf4e136310d0bb0c6c2413cbf35057dff3d4 Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Thu, 28 Oct 2021 18:58:34 -0700 Subject: [PATCH 1/3] fischer_yates_shuffle: fixes bug introduced by partial renaming in #4673 --- other/fischer_yates_shuffle.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/other/fischer_yates_shuffle.py b/other/fischer_yates_shuffle.py index 035fcb482380..61438186b61f 100644 --- a/other/fischer_yates_shuffle.py +++ b/other/fischer_yates_shuffle.py @@ -9,11 +9,11 @@ def fisher_yates_shuffle(data: list) -> list: - for _ in range(len(list)): - a = random.randint(0, len(list) - 1) - b = random.randint(0, len(list) - 1) - list[a], list[b] = list[b], list[a] - return list + for _ in range(len(data)): + a = random.randint(0, len(data) - 1) + b = random.randint(0, len(data) - 1) + data[a], data[b] = data[b], data[a] + return data if __name__ == "__main__": From 644f534b84e10d10c85e98e613fc95bcf82eb89d Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Thu, 28 Oct 2021 19:00:14 -0700 Subject: [PATCH 2/3] fischer_yates_shuffle: aligns arrays in test output to simplify comparison --- other/fischer_yates_shuffle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/fischer_yates_shuffle.py b/other/fischer_yates_shuffle.py index 61438186b61f..6f051a964bc7 100644 --- a/other/fischer_yates_shuffle.py +++ b/other/fischer_yates_shuffle.py @@ -20,5 +20,5 @@ def fisher_yates_shuffle(data: list) -> list: integers = [0, 1, 2, 3, 4, 5, 6, 7] strings = ["python", "says", "hello", "!"] print("Fisher-Yates Shuffle:") - print("List", integers, strings) + print("Lists ", integers, strings) print("FY Shuffle", fisher_yates_shuffle(integers), fisher_yates_shuffle(strings)) From 487a6874529c2ca578baea960270853b6a69e974 Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Thu, 28 Oct 2021 19:03:56 -0700 Subject: [PATCH 3/3] [mypy] adds a generic typevar for fisher yates list shuffle --- other/fischer_yates_shuffle.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/other/fischer_yates_shuffle.py b/other/fischer_yates_shuffle.py index 6f051a964bc7..eaeceed0d4a2 100644 --- a/other/fischer_yates_shuffle.py +++ b/other/fischer_yates_shuffle.py @@ -6,9 +6,12 @@ wikipedia/Fischer-Yates-Shuffle. """ import random +from typing import TypeVar +T = TypeVar('T') -def fisher_yates_shuffle(data: list) -> list: + +def fisher_yates_shuffle(data: list[T]) -> list[T]: for _ in range(len(data)): a = random.randint(0, len(data) - 1) b = random.randint(0, len(data) - 1)