Created
January 18, 2020 11:28
-
-
Save practicalli-johnny/e002d901c5c22bc8a5b00c389584e98d to your computer and use it in GitHub Desktop.
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
((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