Created
February 18, 2020 13:45
-
-
Save jdz/c59f46053f7e79b69380f222a08cc5f9 to your computer and use it in GitHub Desktop.
sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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