Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
9
Task/Permutations/Common-Lisp/permutations-1.lisp
Normal file
9
Task/Permutations/Common-Lisp/permutations-1.lisp
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(defun permute (list)
|
||||
(if list
|
||||
(mapcan #'(lambda (x)
|
||||
(mapcar #'(lambda (y) (cons x y))
|
||||
(permute (remove x list))))
|
||||
list)
|
||||
'(()))) ; else
|
||||
|
||||
(print (permute '(A B Z)))
|
||||
18
Task/Permutations/Common-Lisp/permutations-2.lisp
Normal file
18
Task/Permutations/Common-Lisp/permutations-2.lisp
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
(defun next-perm (vec cmp) ; modify vector
|
||||
(declare (type (simple-array * (*)) vec))
|
||||
(macrolet ((el (i) `(aref vec ,i))
|
||||
(cmp (i j) `(funcall cmp (el ,i) (el ,j))))
|
||||
(loop with len = (1- (length vec))
|
||||
for i from (1- len) downto 0
|
||||
when (cmp i (1+ i)) do
|
||||
(loop for k from len downto i
|
||||
when (cmp i k) do
|
||||
(rotatef (el i) (el k))
|
||||
(setf k (1+ len))
|
||||
(loop while (< (incf i) (decf k)) do
|
||||
(rotatef (el i) (el k)))
|
||||
(return-from next-perm vec)))))
|
||||
|
||||
;;; test code
|
||||
(loop for a = "1234" then (next-perm a #'char<) while a do
|
||||
(write-line a))
|
||||
14
Task/Permutations/Common-Lisp/permutations-3.lisp
Normal file
14
Task/Permutations/Common-Lisp/permutations-3.lisp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
(defun heap-permutations (seq)
|
||||
(let ((permutations nil))
|
||||
(labels ((permute (seq k)
|
||||
(if (= k 1)
|
||||
(push seq permutations)
|
||||
(progn
|
||||
(permute seq (1- k))
|
||||
(loop for i from 0 below (1- k) do
|
||||
(if (evenp k)
|
||||
(rotatef (elt seq i) (elt seq (1- k)))
|
||||
(rotatef (elt seq 0) (elt seq (1- k))))
|
||||
(permute seq (1- k)))))))
|
||||
(permute seq (length seq))
|
||||
permutations)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue