All problems
Shuffle Array Seeded
Problem
Return a shuffled copy of nums using the Fisher-Yates algorithm with a deterministic seeded generator, leaving the input unchanged.
Walk from the last index down to 1, generate a pseudo-random index in the closed range from 0 to i, and swap. The same seed always produces the same permutation.
Hint
Fisher-Yates is uniform only when the swap target is drawn uniformly from 0 through i.
Test cases
case 1
in: [[1,2,3,4,5],42]
expected: [2,4,5,1,3]
case 2
in: [[1],7]
expected: [1]
case 3
in: [[],0]
expected: []
case 4
in: [[1,2,3],1]
expected: [3,2,1]
case 5
in: [[1,2,3],2]
expected: [3,1,2]
Starter codePython
def shuffle_array(nums, seed):
# Your code here
pass5 test cases · run your code against them in the editor