From 09088cd8351729cfa093d46c03e35e5b222d5d90 Mon Sep 17 00:00:00 2001 From: Syed Haseeb Shah Date: Sat, 19 May 2018 16:07:24 +0500 Subject: [PATCH] Create Fischer-Yates_Shuffle.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Fisher–Yates shuffle is an algorithm for generating a random permutation of a finite sequence. For more details visit wikipedia/Fischer-Yates-Shuffle --- other/Fischer-Yates_Shuffle.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 other/Fischer-Yates_Shuffle.py diff --git a/other/Fischer-Yates_Shuffle.py b/other/Fischer-Yates_Shuffle.py new file mode 100644 index 000000000..28cdff75a --- /dev/null +++ b/other/Fischer-Yates_Shuffle.py @@ -0,0 +1,20 @@ +''' +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))