Re: Straight From the Trenches Nooby Stumper Dunk the Kenny Gotta Be A Shorter Cleaner Way #26



Kenneth Tilton <kentilton@xxxxxxxxx> writes:
You are given a simple-vector and a list of malformed ranges,
malformed in that the second value is the last one to /keep/ vs. what
subseq wants as the end parameter: the first one to omit. But all
ranges are pairs (even if only one value is thus delimited) and the
pairs are guarnteed to be ordered and well-formed and not to go out of
bounds, so there is that.

Your mission is to return a simple-vector with those values excised.

(defun excise-ranges (seq ranges)
....)

such that:

(excise-ranges #(0 1 2 3) '((0 0)))
-> #(1 2 3)

(excise-ranges #(0 10 20 30 40) '((1 2)(4 4)))
-> #(0 30)

I'll be different and not use loop:

(defun excise-ranges (sequence ranges)
"Return a sequence that consists of the elements in SEQUENCE not in
the inclusive ranges of RANGES."
(flet ((expand-range (range)
(let ((result nil))
(dotimes (i (1+ (- (second range) (first range))) result)
(push (+ i (first range)) result)))))
(let ((expanded-ranges (reduce #'append (mapcar #'expand-range ranges)))
(result nil))
(dotimes (i (length sequence)
(make-array (length result)
:initial-contents (nreverse result)))
(unless (find i expanded-ranges)
(push (aref sequence i) result))))))

Oh, wait, you wanted it to be _efficient_, too?

Regards,

Patrick

------------------------------------------------------------------------
S P Engineering, Inc. | Large scale, mission-critical, distributed OO
| systems design and implementation.
http://www.spe.com/pjm | (C++, Java, Common Lisp, Jini, middleware, SOA)
.



Relevant Pages