Merge pull request #298 from yesIamHasi/patch-5

Create Fischer-Yates_Shuffle.py
This commit is contained in:
Harshil 2018-05-20 17:49:55 +02:00 committed by GitHub
commit 2d360cd73e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,20 @@
'''
The FisherYates 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))