Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; permutates the collection into all possible slices
- (defn permutate [coll]
- (let [n (+ (count coll) 1)]
- (for [start (range n) stop (reverse (range n))
- :while (< (+ start 1) stop)]
- (subvec coll start stop))))
- ;; check if each element in the collection is larger than the previous
- (defn increasing? [coll]
- (loop [acc (rest coll) last (first coll)]
- (if-not (empty? acc)
- (if (> (first acc) last)
- (recur (rest acc) (first acc))
- false)
- true)))
- ;; elegant solution, but breaks the requirement that you must keep the
- ;; first sequence of the largest length. returns nil for no candidates
- (defn alternative_solution [coll]
- (let [perms (permutate coll)
- scores (for [perm perms :when (increasing? perm)] perm)]
- (last (sort scores)))) ; clojure sorts by sequence length
Add Comment
Please, Sign In to add comment