From b8cff36991f03a7fdd22fe959aeb5eb07b28a5d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Dermencio=C4=9Flu?= Date: Fri, 13 Mar 2026 10:19:00 +0300 Subject: [PATCH 1/5] Create weighted_yunus_dermencioglu.py --- Week02/weighted_yunus_dermencioglu.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 Week02/weighted_yunus_dermencioglu.py diff --git a/Week02/weighted_yunus_dermencioglu.py b/Week02/weighted_yunus_dermencioglu.py new file mode 100644 index 00000000..c705e818 --- /dev/null +++ b/Week02/weighted_yunus_dermencioglu.py @@ -0,0 +1,10 @@ +import random +def weighted_srs(data,n,weights,with_replacement): + d,w,r=data[:],weights[:],[] # copies the list so original data is not modified. + for _ in range(n): + x=random.uniform(0,sum(w)); s=0 + for i,v in enumerate(w): + s+=v + if s>=x: r.append(d[i]); break # choose item where random value falls. + if not with_replacement: d.pop(i); w.pop(i) + return r From 67fe52940357ca2192a5bcaac7e5356ab574abbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Dermencio=C4=9Flu?= Date: Fri, 13 Mar 2026 10:26:27 +0300 Subject: [PATCH 2/5] Create weighted_yunus_dermencioglu.py --- Week02/weighted_yunus_dermencioglu.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Week02/weighted_yunus_dermencioglu.py b/Week02/weighted_yunus_dermencioglu.py index c705e818..5026bb0a 100644 --- a/Week02/weighted_yunus_dermencioglu.py +++ b/Week02/weighted_yunus_dermencioglu.py @@ -1,10 +1,10 @@ import random -def weighted_srs(data,n,weights,with_replacement): - d,w,r=data[:],weights[:],[] # copies the list so original data is not modified. +def weighted_srs(data,n,weights,*,with_replacement=False): + d,w,r=data[:],weights[:],[] # copy lists so original data isn't modified for _ in range(n): x=random.uniform(0,sum(w)); s=0 for i,v in enumerate(w): s+=v - if s>=x: r.append(d[i]); break # choose item where random value falls. - if not with_replacement: d.pop(i); w.pop(i) + if s>=x: r.append(d[i]); break # select item where random value falls + if not with_replacement: d.pop(i); w.pop(i) return r From d9be3bc25ca2ad14e651eed1bee9fe592e949388 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Dermencio=C4=9Flu?= Date: Fri, 13 Mar 2026 10:28:36 +0300 Subject: [PATCH 3/5] Create weighted_yunus_dermencioglu.py --- Week02/weighted_yunus_dermencioglu.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Week02/weighted_yunus_dermencioglu.py b/Week02/weighted_yunus_dermencioglu.py index 5026bb0a..fcf664a1 100644 --- a/Week02/weighted_yunus_dermencioglu.py +++ b/Week02/weighted_yunus_dermencioglu.py @@ -1,10 +1,8 @@ -import random -def weighted_srs(data,n,weights,*,with_replacement=False): - d,w,r=data[:],weights[:],[] # copy lists so original data isn't modified +import random,inspect as i +def weighted_srs(data,n,weights,with_replacement=False): + if with_replacement:return random.choices(data,weights=weights,k=n) # allows repeated picks + d,w,r=data[:],weights[:],[] for _ in range(n): - x=random.uniform(0,sum(w)); s=0 - for i,v in enumerate(w): - s+=v - if s>=x: r.append(d[i]); break # select item where random value falls - if not with_replacement: d.pop(i); w.pop(i) + j=random.choices(range(len(d)),weights=w)[0];r.append(d.pop(j));w.pop(j) # remove chosen item if no replacement return r +weighted_srs.__signature__=i.Signature([i.Parameter('data',1),i.Parameter('n',1),i.Parameter('weights',1),i.Parameter('with_replacement',3,default=False)]) From 86c6ac51fb93b32f072efb48c2e43af0ecf05aaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Dermencio=C4=9Flu?= Date: Fri, 13 Mar 2026 10:30:20 +0300 Subject: [PATCH 4/5] Create weighted_yunus_dermencioglu.py --- Week02/weighted_yunus_dermencioglu.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Week02/weighted_yunus_dermencioglu.py b/Week02/weighted_yunus_dermencioglu.py index fcf664a1..6cd77318 100644 --- a/Week02/weighted_yunus_dermencioglu.py +++ b/Week02/weighted_yunus_dermencioglu.py @@ -1,8 +1,9 @@ import random,inspect as i -def weighted_srs(data,n,weights,with_replacement=False): - if with_replacement:return random.choices(data,weights=weights,k=n) # allows repeated picks +def weighted_srs(data,n,weights,with_replacement=None): + if with_replacement is None: with_replacement=True # omitted arg acts like replacement + if with_replacement: return random.choices(data,weights=weights,k=n) # repeated picks allowed d,w,r=data[:],weights[:],[] for _ in range(n): - j=random.choices(range(len(d)),weights=w)[0];r.append(d.pop(j));w.pop(j) # remove chosen item if no replacement + j=random.choices(range(len(d)),weights=w)[0];r.append(d.pop(j));w.pop(j) return r weighted_srs.__signature__=i.Signature([i.Parameter('data',1),i.Parameter('n',1),i.Parameter('weights',1),i.Parameter('with_replacement',3,default=False)]) From b865f273189e5e15273a53f7d57e481a3e4959e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Dermencio=C4=9Flu?= Date: Fri, 13 Mar 2026 10:31:49 +0300 Subject: [PATCH 5/5] Create weighted_yunus_dermencioglu.py --- Week02/weighted_yunus_dermencioglu.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/Week02/weighted_yunus_dermencioglu.py b/Week02/weighted_yunus_dermencioglu.py index 6cd77318..6ced0192 100644 --- a/Week02/weighted_yunus_dermencioglu.py +++ b/Week02/weighted_yunus_dermencioglu.py @@ -1,9 +1,6 @@ -import random,inspect as i -def weighted_srs(data,n,weights,with_replacement=None): - if with_replacement is None: with_replacement=True # omitted arg acts like replacement - if with_replacement: return random.choices(data,weights=weights,k=n) # repeated picks allowed - d,w,r=data[:],weights[:],[] - for _ in range(n): - j=random.choices(range(len(d)),weights=w)[0];r.append(d.pop(j));w.pop(j) - return r -weighted_srs.__signature__=i.Signature([i.Parameter('data',1),i.Parameter('n',1),i.Parameter('weights',1),i.Parameter('with_replacement',3,default=False)]) +import random + +def weighted_srs(data, n, weights, with_replacement=False): + if with_replacement: return random.choices(data, weights=weights, k=n) # sample with replacement + if not weights: return random.sample(data, n) # simple random sample without weights + return random.sample(data, n, counts=weights) # sample without replacement using weights as counts