A playlist was shuffled with this exact Fisher-Yates procedure: for i from the last index down to 1, take the next random number r from randoms, set j = Math.floor(r * (i + 1)), and swap the songs at i and j. The app saved the random numbers and the shuffled result, shuffled.
Write undoShuffle(shuffled, randoms) that returns the playlist in its original order.
Input: shuffled = ["q2","q1","q4","q3"], randoms = [0.5,0.9,0.1] Output: ["q1","q2","q3","q4"]
The swaps were (3, 2), (2, 2), (1, 0). Undoing them in reverse order ((1, 0), then (2, 2), then (3, 2)) gives back the original.
Input: shuffled = ["x"], randoms = [] Output: ["x"]
Nothing was swapped.
0 <= shuffled.length <= 10^5randoms.length === Math.max(0, shuffled.length - 1)0 <= randoms[i] < 1Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #89 Undo a Recorded Shuffle. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.