Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; Enter your code here. Read input from STDIN. Print output to STDOUT
- ;;; (pgroup '(1 2 3 4)) => ((1 2) (3 4))
- (defun pgroup (s)
- "Do pairs of elements of a given list."
- (cond ((null s) s)
- ((null (cdr s)) s)
- (t (cons (list (car s) (cadr s))
- (pgroup (cddr s))))))
- ;;; (gen 5 0 '(4 0)) => ((4 3) (2 0) (2 2))
- (defun gen (n i s &optional (m 5))
- "Gen algorirthm from hackerrank."
- (cond ((> i n) (pgroup (cddr (reverse s))))
- (t (gen n (1+ i) (cons (mod (+ (car s) (cadr s)) m) s)))))
- ;;; (dot-product '(4) '(5)) => 20
- (defun dot-product (a b)
- "Compute the mathematical dot product of two vectors."
- (apply #'+ (mapcar #'* a b)))
- (defparameter *ps* '())
- ;;; (comb 2 '(1 2 3) #'print) =>
- ;;; (3 2)
- ;;; (3 1)
- ;;; (2 1)
- (defun comb (m list fn)
- "Find all possible combinations of a given list."
- (labels ((comb1 (l c m)
- (when (>= (length l) m)
- (if (zerop m) (return-from comb1 (funcall fn c)))
- (comb1 (cdr l) c m)
- (comb1 (cdr l) (cons (first l) c) (1- m)))))
- (comb1 list nil m)))
- ;;; (explanation '(4 0) 5 3) => (8 14 4)
- (defun explanation (c m n)
- "Explanation algorithm from hackerrank."
- (setf *ps* '())
- (comb 2 (gen (+ 2 n) 0 c m)
- (lambda (s) (push (dot-product (car s) (cadr s)) *ps*))))
- ;;; (diff (loop :for x in *ps* :collect (mod x 5)) => (3 4)
- (defun diff (s)
- "Remove duplicated atoms of a list."
- (cond ((null s) s)
- ((member (car s) (cdr s))
- (diff (cdr s)))
- (t (cons (car s) (diff (cdr s))))))
- ;;; hackerrank I/O
- (setq *ps* (explanation (list (read) 0) (read) (read)))
- (format t "~a" (length (diff (loop :for x in *ps* :collect (mod x (third *ps*))))))
Advertisement
Add Comment
Please, Sign In to add comment