Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Week02/weighted_yunus_dermencioglu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import random

def weighted_srs(data, n, weights, *args, with_replacement=False):
if args: with_replacement = args[0] # also accept 4th positional arg if given
if with_replacement: return random.choices(data, weights=weights, k=n) # repeats allowed
w, res = list(weights), []
for _ in range(n):
i = random.choices(range(len(data)), weights=w)[0]
res.append(data[i]); w[i] = 0
return res
Loading