Skip to content

Instantly share code, notes, and snippets.

@ZCG-coder
Created February 5, 2022 11:12
Show Gist options
  • Select an option

  • Save ZCG-coder/24cfb041215a9f1bf09b37f79bd11ef7 to your computer and use it in GitHub Desktop.

Select an option

Save ZCG-coder/24cfb041215a9f1bf09b37f79bd11ef7 to your computer and use it in GitHub Desktop.
Fisher-Yates Shuffle
import random
def shuffle(ary):
old_ary = ary[:]
a = len(ary)
b = a - 1
for d in range(b, 0, -1):
e = random.randint(0, d - 1)
ary[d], ary[e] = ary[e], ary[d]
return ary
from main import shuffle
import unittest
class TestShuffle(unittest.TestCase):
def test_shuffle(self):
numbers = [a for a in range(1, 101)]
shuffled = shuffle(numbers)
print(shuffled)
self.assertEqual(len(set(shuffled) & set(numbers)), len(numbers))
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment