Paste number 323870: untitled

Paste number 323870: untitled
Pasted by: asdfhb
When:5 years, 1 month ago
Share:Tweet this! | http://paste.lisp.org/+6XWE
Channel:None
Paste contents:
Raw Source | XML | Display As
;;;;Diminishing Gap Sort pr2
;;need to sort a list into it's smallest gaps.
(defun find-gaps (lst)
  "Finds the gaps between each list. Subtracts the second from the first then the third from the second etc" 
  (mapcar (lambda (x y)
	    (list x y(- x y)))
	  lst
	  (cons 0 lst)))
(defun sort-list (worked-list)
  "Sorts the list into a decreasing order using the third value of each list for comparisons"
  (sort worked-list #'third-min))
(defun third-min (x y)
  "Compares the third element of lists x and y to see if they are in decreasing order"
  (> (third x) (third y)))
(defun return-answer (worked-list)
  "Returns the final answer in a human readable format"
  (format t "Sorted list: ~A ~%Gaps: ~A"
	  (mapcar #'first worked-list)
	  (mapcar #'third worked-list)))

CL-USER> (return-answer (sort-list (find-gaps '(5 12 14 15 80 121 134 144 256))))
Sorted list: (256 80 121 134 144 12 5 14 15) 
Gaps: (112 65 41 13 10 7 5 2 1)

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.