# HG changeset patch # User Steve Losh # Date 1545630090 18000 # Node ID df2880a47218b1ae4a1178dc6b55edeb357f7621 # Parent b1baea60c24f542f3430f0d81a884c7374da4faa SIGN diff -r b1baea60c24f -r df2880a47218 src/problems/sign.lisp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/problems/sign.lisp Mon Dec 24 00:41:30 2018 -0500 @@ -0,0 +1,37 @@ +(in-package :rosalind) + +(defparameter *input-sign* "2") + +(defparameter *output-sign* "8 +-1 -2 +-1 2 +1 -2 +1 2 +-2 -1 +-2 1 +2 -1 +2 1") + + +(defun sign-permutations (list) + (if (null list) + (list '()) ;; there is exactly one permutation of the empty list: () + (destructuring-bind (n . more) list + (append (mapcar (curry #'cons n) (sign-permutations more)) + (mapcar (curry #'cons (- n)) (sign-permutations more)))))) + +(define-problem sign (data string) + *input-sign* + *output-sign* + (let* ((n (parse-integer data)) + (count (* (expt 2 n) + (factorial n))) + (perms (mapcan #'sign-permutations + (permutations (alexandria:iota n :start 1))))) + ;; sort to ensure consistent output for the unit test + (format nil "~D~%~{~A~^~%~}" + count + (sort (mapcar (curry #'str:join " ") perms) #'string<)))) + +;; (problem-sign "2") +;; (solve sign)