Someone wanted to see a Lisp solution.  Well, here's one:

;;;; A solution in Common Lisp to the mozy.com Deathmatch sample question

(defun split-string (string char)
"Returns a list of substrings of string delimited by the given character"
  (loop for i = 0 then (1+ j)
        as j = (position char string :start i)
        collect (subseq string i j)
        while j))

(defun check (list)
"Given a list of strings representing integers, return t when the difference between every two adjacent integers has a magnitude greater than 0 and less than the length of the list, and nil otherwise"
  (let ((n (length list)))
    (catch 'fail
      (reduce (lambda (x y)
                (let ((diff (abs (- (parse-integer x)
                                    (parse-integer y)))))
                  (unless (and (> diff 0)
                               (< diff n))
                    (throw 'fail nil))
                  y))
              list)
      t)))

(defun check-line (line)
"Given a line of input, print 'match' if it matches the criteria of the check predicate and 'not a match' otherwise"
  (if (check (split-string line #\Space))
      (format t "match~%")
      (format t "not a match~%")))

;; Check lines until the program is terminated
(loop (check-line (read-line)))


/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/

Reply via email to