diff --git a/other/fischer_yates_shuffle.py b/other/fischer_yates_shuffle.py index 035fcb482380..eaeceed0d4a2 100644 --- a/other/fischer_yates_shuffle.py +++ b/other/fischer_yates_shuffle.py @@ -6,19 +6,22 @@ wikipedia/Fischer-Yates-Shuffle. """ import random +from typing import TypeVar +T = TypeVar('T') -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 + +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) + data[a], data[b] = data[b], data[a] + return data if __name__ == "__main__": 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))