Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jdz
Created February 18, 2020 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdz/c59f46053f7e79b69380f222a08cc5f9 to your computer and use it in GitHub Desktop.
Save jdz/c59f46053f7e79b69380f222a08cc5f9 to your computer and use it in GitHub Desktop.
sample
(defmethod sample ((input list) k &aux (length (length input)))
(assert (<= 1 k length))
(let ((indices (loop with result = '()
with i = k
for j = (random length)
unless (member j result)
do (push j result)
(decf i)
while (< 0 i)
finally (return result))))
(setq indices (sort indices #'<))
(loop with idx = (pop indices)
for i upfrom 0
for x in input
while idx
when (= i idx)
collect x
and do (setq idx (pop indices)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment