Fork me on GitHub
#clojure-uk
<
2019-06-29
>
3Jane07:06:15

You're right, 53 took a while 🙂

3Jane07:06:54

Current version, but not 4clojure compatible because defn

3Jane07:06:28

(defn partition-by-comp [f [x & xs]]
  (let [[_ acc result]
        (reduce (fn [[prev acc result] curr]
                  (if (f curr prev)
                    [curr (conj acc curr) result]
                    [curr [curr] (conj result acc)]))
                [x [x] []]
                xs)]
    (if (not-every? seq [xs acc]) result (conj result acc))))

(defn f53 [xs]
  (->> (conj (partition-by-comp #(= %1 (inc %2)) xs) [])
       (remove #(= 1 (count %)))
       (sort-by #(- (count %)))
       (first)))

(deftest- f53t
          (is (= (f53 [1 0 1 2 3 0 4 5]) [0 1 2 3]))
          (is (= (f53 [5 6 1 3 2 7]) [5 6]))
          (is (= (f53 [2 3 3 4 5]) [3 4 5]))
          (is (= (f53 [7 6 5 4]) [])))

practicalli-johnny10:06:03

Very interesting. Much nicer than my loop recur approach

simonkatz19:06:59

I had a play with this too…

(defn partition-when [f coll]
  ;; Pattern copied from `clojure.core/partition-by`.
  (lazy-seq
   (when-let [s (seq coll)]
     (let [run (cons (first s)
                     (take-while (comp not f)
                                 (next s)))]
       (cons run
             (partition-when f (lazy-seq (drop (count run) s))))))))

(defn longest-increasing-sub-seq [s]
  (let [dummy-small-number (- (first s) 1)
        pairs              (partition 2
                                      1
                                      (cons dummy-small-number s))
        subseqs-of-pairs   (partition-when #(apply >= %)
                                           pairs)
        subseqs            (map #(map second %)
                                subseqs-of-pairs)
        longest-subseq     (reduce (fn [sofar v]
                                     (if (> (count v) (count sofar))
                                       v
                                       sofar))
                                   []
                                   subseqs)]
    (if (>= (count longest-subseq) 2)
      longest-subseq
      [])))

3Jane14:06:47

Nice! I like how you made your partition lazy. “Read through stdlib sources” very much on my todo list for learning patterns :)

3Jane14:06:41

The reduce approach very much inspired by advice I got on the channel recently when I was trying to solve a similar issue. This slack is awesome ❤️

💯 4
simonkatz22:06:13

FWIW, I edited my solution so that longest-subseq is found with a single pass.

3Jane07:06:45

Hey! Apparently today is meant to be one of those unbritish days ... undecided if I want to risk getting fried to a crisp outside

yogidevbear08:06:31

We're trying to get out the door for a run before it gets too hot

practicalli-johnny10:06:45

I have put thermal blankets on all the windows to keep out the heat, it’s going to be 32 degrees today in London