Skip to content

Commit f07ee00

Browse files
docs(other): add doctest examples to fischer_yates_shuffle (addresses #9943)
1 parent 74a1004 commit f07ee00

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

other/fischer_yates_shuffle.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@
1111

1212

1313
def fisher_yates_shuffle(data: list) -> list[Any]:
14+
"""
15+
In-place random shuffle of a list using the Fisher–Yates algorithm.
16+
17+
The output is a permutation of the input. Since the operation is random,
18+
we assert permutation properties rather than exact order in doctests.
19+
20+
>>> data = [1, 2, 3, 4]
21+
>>> shuffled = fisher_yates_shuffle(data.copy())
22+
>>> sorted(shuffled) == [1, 2, 3, 4]
23+
True
24+
>>> len(shuffled) == len(set(shuffled))
25+
True
26+
"""
1427
for _ in range(len(data)):
1528
a = random.randint(0, len(data) - 1)
1629
b = random.randint(0, len(data) - 1)

0 commit comments

Comments
 (0)