From 8051f004da8e685f72aaa1a199f7c6c7ca512ec5 Mon Sep 17 00:00:00 2001 From: alibedelov <59971076+alibedelov@users.noreply.github.com> Date: Mon, 3 Mar 2025 23:51:36 +0400 Subject: [PATCH 1/3] Create weighted_ali_badalov.py Initial commit --- Week03/weighted_ali_badalov.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 Week03/weighted_ali_badalov.py diff --git a/Week03/weighted_ali_badalov.py b/Week03/weighted_ali_badalov.py new file mode 100644 index 0000000..58f0fe6 --- /dev/null +++ b/Week03/weighted_ali_badalov.py @@ -0,0 +1,7 @@ +import random + +def weighted_srs(data, n, weights, with_replacment = False): + if(with_replacment == True or weights != None): #Check if we are using replacement or not + return random.choices(data, weights, k=n) + else: + return random.sample(data, n) From baf2a184b4aa95b9587e1d694fbb4f56af8f2e6c Mon Sep 17 00:00:00 2001 From: alibedelov <59971076+alibedelov@users.noreply.github.com> Date: Sat, 15 Mar 2025 01:29:52 +0400 Subject: [PATCH 2/3] Update weighted_ali_badalov.py Type with_replacement fixed. --- Week03/weighted_ali_badalov.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Week03/weighted_ali_badalov.py b/Week03/weighted_ali_badalov.py index 58f0fe6..f21e008 100644 --- a/Week03/weighted_ali_badalov.py +++ b/Week03/weighted_ali_badalov.py @@ -1,7 +1,7 @@ import random -def weighted_srs(data, n, weights, with_replacment = False): - if(with_replacment == True or weights != None): #Check if we are using replacement or not +def weighted_srs(data, n, weights, with_replacement = False): + if(with_replacement == True or weights != None): #Check if we are using replacement or not return random.choices(data, weights, k=n) else: return random.sample(data, n) From d1303b5de4b3b41cc66310d05fd674356ecf499e Mon Sep 17 00:00:00 2001 From: alibedelov <59971076+alibedelov@users.noreply.github.com> Date: Sun, 6 Apr 2025 18:14:06 +0400 Subject: [PATCH 3/3] Update weighted_ali_badalov.py --- Week03/weighted_ali_badalov.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Week03/weighted_ali_badalov.py b/Week03/weighted_ali_badalov.py index f21e008..c2e973d 100644 --- a/Week03/weighted_ali_badalov.py +++ b/Week03/weighted_ali_badalov.py @@ -1,7 +1,10 @@ import random - def weighted_srs(data, n, weights, with_replacement = False): - if(with_replacement == True or weights != None): #Check if we are using replacement or not - return random.choices(data, weights, k=n) + if len(data) != len(weights): + raise ValueError + if with_replacement: + return random.choices(data, weights=weights, k=n) else: - return random.sample(data, n) + if n > len(data): + raise ValueError + return random.sample(data, k=n)