Created
February 5, 2022 11:12
-
-
Save ZCG-coder/24cfb041215a9f1bf09b37f79bd11ef7 to your computer and use it in GitHub Desktop.
Fisher-Yates Shuffle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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