about summary refs log tree commit diff
path: root/scratch/facebook/hard/random-choice.py
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-11-17T23·54+0000
committerWilliam Carroll <wpcarro@gmail.com>2020-11-17T23·54+0000
commitc0268ed31a8049ce75a7e61737b8e520e7d5403e (patch)
treed387ed781105b55994a0f14fba9d6e10c4170c5b /scratch/facebook/hard/random-choice.py
parent751b5327a92f4d53db6253e59c475e4e96cabcb6 (diff)
Refactor random-choice
Prefer initializing `result` to an empty array of size `m`, which makes the
algorithm a bit more elegant.
Diffstat (limited to 'scratch/facebook/hard/random-choice.py')
-rw-r--r--scratch/facebook/hard/random-choice.py4
1 files changed, 2 insertions, 2 deletions
diff --git a/scratch/facebook/hard/random-choice.py b/scratch/facebook/hard/random-choice.py
index 95029ceb80a4..95e834a2eb01 100644
--- a/scratch/facebook/hard/random-choice.py
+++ b/scratch/facebook/hard/random-choice.py
@@ -6,8 +6,8 @@ def choose_a(m, xs):
     Randomly choose `m` elements from `xs`.
     This algorithm runs in linear time with respect to the size of `xs`.
     """
-    result = xs[:m]
-    for i in range(m, len(xs)):
+    result = [None] * m
+    for i in range(len(xs)):
         j = random.randint(0, i)
         if j < m:
             result[j] = xs[i]