Last active
May 17, 2023 00:41
-
-
Save chase-lambert/88d2f13246c718493d93c4ca814e47ca to your computer and use it in GitHub Desktop.
rendezvous with cassidoo challenge: 23.02.26
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns repeated-groups | |
(:require [clojure.test :refer [deftest is])) | |
(defn repeated-groups [nums] | |
(->> nums | |
(partition-by identity) | |
(filter #(> (count %) 1)) | |
(mapv vec))) | |
;; Using transducers instead which gives | |
;; a huge speedup for larger vecs (shown | |
;; below in unscientific fashion): | |
(defn repeated-groups-transducer [nums] | |
(let [xform (comp | |
(partition-by identity) | |
(filter #(> (count %) 1)))] | |
(into [] xform nums))) | |
;; (def nums (into [] (repeatedly 10000000 #(rand-int 10)))) | |
;; (time (repeated-groups nums)) ;; around 4.5 - 5 seconds | |
;; (time (repeated-groups-transduce nums)) ;; around 1.1 - 1.4 seconds | |
;; (= (repeated-groups nums) (repeated-groups-transduce nums)) ;; true | |
(deftest repeated-groups-test | |
(is (= (repeated-groups [1 2 2 4 5]) [[2 2]])) | |
(is (= (repeated-groups [1 1 0 0 8 4 4 4 3 2 1 9 9]) [[1 1] [0 0] [4 4 4] [9 9]]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment