Guest User

Untitled

a guest
Sep 13th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;; permutates the collection into all possible slices
  2. (defn permutate [coll]
  3.   (let [n (+ (count coll) 1)]
  4.     (for [start (range n) stop (reverse (range n))
  5.      :while (< (+ start 1) stop)]
  6.       (subvec coll start stop))))
  7.  
  8. ;; check if each element in the collection is larger than the previous
  9. (defn increasing? [coll]
  10.   (loop [acc (rest coll) last (first coll)]
  11.     (if-not (empty? acc)
  12.       (if (> (first acc) last)
  13.         (recur (rest acc) (first acc))
  14.         false)
  15.       true)))
  16.  
  17. ;; elegant solution, but breaks the requirement that you must keep the
  18. ;; first sequence of the largest length. returns nil for no candidates
  19. (defn alternative_solution [coll]
  20.   (let [perms (permutate coll)
  21.         scores (for [perm perms :when (increasing? perm)] perm)]
  22.     (last (sort scores)))) ; clojure sorts by sequence length
Add Comment
Please, Sign In to add comment