diff --git a/Week02/weighted_kayra_basar_karadeniz.py b/Week02/weighted_kayra_basar_karadeniz.py new file mode 100644 index 00000000..e501f7a7 --- /dev/null +++ b/Week02/weighted_kayra_basar_karadeniz.py @@ -0,0 +1,10 @@ +import random +def weighted_srs(data, n, weights, with_replacement=False): + if with_replacement: + return random.choices(data, weights=weights, k=n) + res, d_copy, w_copy = [], list(data), list(weights) if weights else [1]*len(data) + for _ in range(n): + idx = random.choices(range(len(d_copy)), weights=w_copy)[0] + res.append(d_copy.pop(idx)) + w_copy.pop(idx) + return res