mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 13:31:07 +00:00
7f04e5cd34
* spelling corrections * review * improved documentation, removed redundant variables, added testing * added type hint * camel case to snake case * spelling fix * review * python --> Python # it is a brand name, not a snake * explicit cast to int * spaces in int list * "!= None" to "is not None" * Update comb_sort.py * various spelling corrections in documentation & several variables naming conventions fix * + char in file name * import dependency - bug fix Co-authored-by: John Law <johnlaw.po@gmail.com>
24 lines
654 B
Python
24 lines
654 B
Python
#!/usr/bin/python
|
||
"""
|
||
The Fisher–Yates shuffle is an algorithm for generating a random permutation of a finite sequence.
|
||
For more details visit
|
||
wikipedia/Fischer-Yates-Shuffle.
|
||
"""
|
||
import random
|
||
|
||
|
||
def FYshuffle(list):
|
||
for i 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
|
||
|
||
|
||
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("FY Shuffle", FYshuffle(integers), FYshuffle(strings))
|