From 319c7b98adf61060b61d87f9888d53d3274c756e Mon Sep 17 00:00:00 2001 From: fxrlxrn Date: Mon, 7 Aug 2023 21:58:50 +0300 Subject: [PATCH] ya ne eby?? sorry i forgot && --- .vscode/settings.json | 9 +++++++++ pystd/random.py | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..290193e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,9 @@ +{ + "python.testing.pytestArgs": [ + "tests" + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true, + "python.linting.flake8Enabled": true, + "python.linting.enabled": true +} \ No newline at end of file diff --git a/pystd/random.py b/pystd/random.py index 8120d0d..84340aa 100644 --- a/pystd/random.py +++ b/pystd/random.py @@ -62,4 +62,20 @@ def choices(population, weights=None, *, cumulative_weights=None, k=1) -> list: def sample(population, k, *, counts=None): - ... + population = list(population) + if counts is None: + p1 = population.copy() + else: + p1 = list() + for i in range(len(population)): + p1.extend([population[i]] * counts[i]) + + if k > len(p1): + raise ValueError + + ls = list() + for i in range(k): + b = choice(p1) + ls.append(b) + p1.remove(b) + return ls