A card game deals k cards from deck without shuffling the whole deck. It uses a partial Fisher-Yates shuffle from the front: on a copy of deck, for i from 0 to k - 1, take the random number randoms[i], set j = i + Math.floor(randoms[i] * (n - i)) where n is the deck size, and swap the cards at i and j.
Write dealHand(deck, k, randoms) that returns the first k cards of the copy after those k steps.
Input: deck = ["A","K","Q","J","10"], k = 2, randoms = [0.5,0.1] Output: ["Q","K"]
i = 0: j = 0 + floor(0.5 * 5) = 2, so Q moves to the front. i = 1: j = 1 + floor(0.1 * 4) = 1, so K stays.
Input: deck = ["x","y","z"], k = 0, randoms = [] Output: []
Dealing no cards gives an empty hand.
0 <= k <= deck.length <= 10^5randoms.length === k0 <= 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 #87 Deal a Random Hand. 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.