Skip to content

Commit 09088cd

Browse files
authored
Create Fischer-Yates_Shuffle.py
The Fisher–Yates shuffle is an algorithm for generating a random permutation of a finite sequence. For more details visit wikipedia/Fischer-Yates-Shuffle
1 parent f510207 commit 09088cd

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

other/Fischer-Yates_Shuffle.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
The Fisher–Yates shuffle is an algorithm for generating a random permutation of a finite sequence.
3+
For more details visit
4+
wikipedia/Fischer-Yates-Shuffle.
5+
'''
6+
import random
7+
8+
def FYshuffle(LIST):
9+
for i in range(len(LIST)):
10+
a = random.randint(0, len(LIST)-1)
11+
b = random.randint(0, len(LIST)-1)
12+
LIST[a], LIST[b] = LIST[b], LIST[a]
13+
return LIST
14+
15+
if __name__ == '__main__':
16+
integers = [0,1,2,3,4,5,6,7]
17+
strings = ['python', 'says', 'hello', '!']
18+
print ('Fisher-Yates Shuffle:')
19+
print ('List',integers, strings)
20+
print ('FY Shuffle',FYshuffle(integers), FYshuffle(strings))

0 commit comments

Comments
 (0)