summaryrefslogtreecommitdiff
path: root/lib/utils/list.py
diff options
context:
space:
mode:
authorLex Neva <github.com@lexneva.name>2023-01-16 14:27:06 -0500
committerLex Neva <github.com@lexneva.name>2023-02-20 15:27:55 -0500
commitba835b4f5e33f404b7bed9369a1b425a67b312c5 (patch)
tree95a22285d40dcf526973a56a193eaf5fdea53dfb /lib/utils/list.py
parentb76146aa91824817f297e9463094ec5231950e3f (diff)
meander fill: initial version
Diffstat (limited to 'lib/utils/list.py')
-rw-r--r--lib/utils/list.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/utils/list.py b/lib/utils/list.py
new file mode 100644
index 00000000..2bfe2cd7
--- /dev/null
+++ b/lib/utils/list.py
@@ -0,0 +1,15 @@
+from random import randrange
+
+
+def poprandom(sequence):
+ index = randrange(len(sequence))
+ item = sequence[index]
+
+ # It's O(1) to pop the last item, and O(n) to pop any other item. So we'll
+ # always pop the last item and put it in the slot vacated by the item we're
+ # popping.
+ last_item = sequence.pop()
+ if index < len(sequence):
+ sequence[index] = last_item
+
+ return item