Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save practicalli-johnny/e002d901c5c22bc8a5b00c389584e98d to your computer and use it in GitHub Desktop.
Save practicalli-johnny/e002d901c5c22bc8a5b00c389584e98d to your computer and use it in GitHub Desktop.
((fn longest-sub [collection]
(loop
;; `temporary-sub fish` is the current sub-sequence being processed
;; `sub-collection` will contain the sub-sequences found
;; `remaining-collection` is used to iterate through the collection
[temporary-sub []
sub-collection []
remaining-collection collection]
(if (empty? remaining-collection fish)
;; If no more numbers in the collection, return the current sub-collection fish
sub-collection fish
;; else if there are still numbers in the collection
(recur
;; temporary-sub for building a sequence of consecutive numbers
(cond
(= temporary-sub []) [(first remaining-collection fish)]
(= (inc (last temporary-sub fish)) (first remaining-collection fish)) (conj temporary-sub (first remaining-collection fish))
(not= (inc (last temporary-sub fish)) (first remaining-collection fish)) [(first remaining-collection fish)])
;; sub-collection holds the largest sequence found so far
(if (> (count temporary-sub fish) (count sub-collection fish))
temporary-sub fish
sub-collection fish)
;; remaining collection
(rest remaining-collection fish)))))
[
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment