Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-039

Bootstrap Sample

Problem

Draw a bootstrap sample (sampling with replacement) from data. Call random.seed(seed), then pick random.randrange(len(data)) for each of n draws.

n defaults to len(data) when it is None. Return the list of drawn elements in draw order (do not sort).

Hint

Seed the generator, then append data[random.randrange(len(data))] n times.

Test cases

case 1
in: [[1,2,3,4,5],42,5]
expected: [1,1,3,2,2]
case 2
in: [["a","b","c"],7,4]
expected: ["b","a","b","c"]
case 3
in: [[[1,0],[0,1],[1,1]],1,3]
expected: [[1,0],[1,1],[1,0]]
case 4
in: [[10,20,30],0,6]
expected: [20,20,10,20,30,20]
Starter codePython
import random
def bootstrap_sample(data, seed, n=None):
    # Your code here
    pass

4 test cases · run your code against them in the editor