From 518046594ab5d6ec224551b0aca3ba7dfbd3451b Mon Sep 17 00:00:00 2001 From: Biyse Date: Sat, 28 Mar 2026 18:34:31 +0300 Subject: [PATCH 1/9] Add weighted simple random sampling function --- Week02/weighted_buse_cici.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 Week02/weighted_buse_cici.py diff --git a/Week02/weighted_buse_cici.py b/Week02/weighted_buse_cici.py new file mode 100644 index 00000000..36c48a4b --- /dev/null +++ b/Week02/weighted_buse_cici.py @@ -0,0 +1,10 @@ +import random +def weighted_srs(data, n, weights, with_replacement=True): + _sample, _data, _weights = [], list(data), list(weights) + for _ in range(n): + choise = random.choices(_data, weights=_weights, k=1)[0] + sample.append(choise) + if not with_replacement: + ind = _data.index(choise) + _data.pop(ind), _weights.pop(ind) + return sample From ce529bab61131f1f972864b402e8c40f9ca80e07 Mon Sep 17 00:00:00 2001 From: Biyse Date: Sat, 28 Mar 2026 18:43:11 +0300 Subject: [PATCH 2/9] Update weighted_buse_cici.py --- Week02/weighted_buse_cici.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Week02/weighted_buse_cici.py b/Week02/weighted_buse_cici.py index 36c48a4b..d88346fd 100644 --- a/Week02/weighted_buse_cici.py +++ b/Week02/weighted_buse_cici.py @@ -1,6 +1,6 @@ import random def weighted_srs(data, n, weights, with_replacement=True): - _sample, _data, _weights = [], list(data), list(weights) + sample, _data, _weights = [], list(data), list(weights) for _ in range(n): choise = random.choices(_data, weights=_weights, k=1)[0] sample.append(choise) From e2425f5bb6446e2053a35e0885a65c35666dc585 Mon Sep 17 00:00:00 2001 From: Biyse Date: Sat, 28 Mar 2026 18:45:36 +0300 Subject: [PATCH 3/9] Fix missing newline at end of file From fba5f28b6cb455e6c1fa04bc82e58bf4c76f6f79 Mon Sep 17 00:00:00 2001 From: Biyse Date: Sat, 28 Mar 2026 18:46:37 +0300 Subject: [PATCH 4/9] Fix indentation and add newline at end of file From 8d7be03ab032a6839f4a0c2ae9392f381e82b3cb Mon Sep 17 00:00:00 2001 From: Biyse Date: Sat, 28 Mar 2026 19:00:25 +0300 Subject: [PATCH 5/9] Update weighted_buse_cici.py --- Week02/weighted_buse_cici.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Week02/weighted_buse_cici.py b/Week02/weighted_buse_cici.py index d88346fd..e914191f 100644 --- a/Week02/weighted_buse_cici.py +++ b/Week02/weighted_buse_cici.py @@ -1,10 +1,8 @@ import random -def weighted_srs(data, n, weights, with_replacement=True): - sample, _data, _weights = [], list(data), list(weights) +def weighted_srs(data, n, weights, *, with_replacement=True): + sample, d, w = [], list(data), list(weights) for _ in range(n): - choise = random.choices(_data, weights=_weights, k=1)[0] + choise = random.choices(d, weights=w, k=1)[0] sample.append(choise) - if not with_replacement: - ind = _data.index(choise) - _data.pop(ind), _weights.pop(ind) + if not with_replacement: ind = d.index(choise); d.pop(ind); w.pop(ind) return sample From 18400b76ad267214811ec3c28995a062b70148b2 Mon Sep 17 00:00:00 2001 From: Biyse Date: Sat, 28 Mar 2026 19:07:25 +0300 Subject: [PATCH 6/9] Refactor weighted_srs function for clarity --- Week02/weighted_buse_cici.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Week02/weighted_buse_cici.py b/Week02/weighted_buse_cici.py index e914191f..84520cc7 100644 --- a/Week02/weighted_buse_cici.py +++ b/Week02/weighted_buse_cici.py @@ -1,8 +1,9 @@ import random -def weighted_srs(data, n, weights, *, with_replacement=True): +def weighted_srs(data, n, weights, *, with_replacement): + if not with_replacement and n > len(data): return None sample, d, w = [], list(data), list(weights) for _ in range(n): - choise = random.choices(d, weights=w, k=1)[0] - sample.append(choise) - if not with_replacement: ind = d.index(choise); d.pop(ind); w.pop(ind) + c = random.choices(d, weights=w, k=1)[0]; sample.append(c) + if not with_replacement: + i = d.index(c); d.pop(i); w.pop(i) return sample From 0b475eacf399e21e2a9082fa24afc905a6ee3a6e Mon Sep 17 00:00:00 2001 From: Biyse Date: Sat, 28 Mar 2026 19:11:03 +0300 Subject: [PATCH 7/9] Update weighted_buse_cici.py --- Week02/weighted_buse_cici.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Week02/weighted_buse_cici.py b/Week02/weighted_buse_cici.py index 84520cc7..774ece5f 100644 --- a/Week02/weighted_buse_cici.py +++ b/Week02/weighted_buse_cici.py @@ -1,5 +1,5 @@ import random -def weighted_srs(data, n, weights, *, with_replacement): +def weighted_srs(data, n, weights, with_replacement): if not with_replacement and n > len(data): return None sample, d, w = [], list(data), list(weights) for _ in range(n): From bff1f97044ce589997ac79acfa8124be272d48ae Mon Sep 17 00:00:00 2001 From: Biyse Date: Sat, 28 Mar 2026 19:14:41 +0300 Subject: [PATCH 8/9] Update weighted_buse_cici.py --- Week02/weighted_buse_cici.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Week02/weighted_buse_cici.py b/Week02/weighted_buse_cici.py index 774ece5f..15947529 100644 --- a/Week02/weighted_buse_cici.py +++ b/Week02/weighted_buse_cici.py @@ -1,9 +1,9 @@ import random -def weighted_srs(data, n, weights, with_replacement): - if not with_replacement and n > len(data): return None - sample, d, w = [], list(data), list(weights) +def weighted_srs(data, n, weights, with_replacement=False): + if weights or with_replacement: return random.choices(data, weights=weights, k=n) + sample, re_data = [], list(data) for _ in range(n): - c = random.choices(d, weights=w, k=1)[0]; sample.append(c) - if not with_replacement: - i = d.index(c); d.pop(i); w.pop(i) + choisen = random.choices(remaining_data)[0] + sample.append(choisen) + re_data.remove(choisen) return sample From 84acc02ab3f960d41959f022dd0306781c115180 Mon Sep 17 00:00:00 2001 From: Biyse Date: Sat, 28 Mar 2026 19:15:55 +0300 Subject: [PATCH 9/9] Update weighted_buse_cici.py --- Week02/weighted_buse_cici.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Week02/weighted_buse_cici.py b/Week02/weighted_buse_cici.py index 15947529..45e99d3d 100644 --- a/Week02/weighted_buse_cici.py +++ b/Week02/weighted_buse_cici.py @@ -3,7 +3,7 @@ def weighted_srs(data, n, weights, with_replacement=False): if weights or with_replacement: return random.choices(data, weights=weights, k=n) sample, re_data = [], list(data) for _ in range(n): - choisen = random.choices(remaining_data)[0] + choisen = random.choices(re_data)[0] sample.append(choisen) re_data.remove(choisen) return sample