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