Last active
May 5, 2022 18:39
-
-
Save williamromero/e480d8c55ed731ddbf45d8421d08ff63 to your computer and use it in GitHub Desktop.
This code checks if exists a sequence of numbers inside of an array and retrieves those numbers ordered.
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
def sequence_length(array) | |
array.sort { |a, b| a <=> b }.each_cons(2) { |a, b| b - a == 1 ? array : array.delete(b) } | |
"Length on sequence: #{array.length} - #{array.sort}" | |
end | |
p sequence_length([100, 4, 200, 1, 3, 2]) # Length on sequence: 4 - [1, 2, 3, 4] | |
p sequence_length([29, 27, 28, 55, 100, 84]) # Length on sequence: 3 - [27, 28, 29] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment