Created
March 4, 2021 12:26
-
-
Save NILID/55c6597b8407415c65a5f243b35c7b53 to your computer and use it in GitHub Desktop.
Displaying empty range indices based on filled range indices
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
arr_size = 100 | |
arr = [[0,5], [20, 30], [50, 70]] | |
def get_ranges(arr_size, arr) | |
# calculating adjacent indices | |
# flatten multiple arrays in one | |
arr.map!{|k, v| [get_min(k), get_max(v, arr_size)]}.flatten! | |
# checking the occurrence of range boundaries | |
sum = arr + [0, arr_size] | |
res = sum - (arr & [0, arr_size]) | |
# splitting the resulting array | |
result = res.sort.each_slice(2).to_a | |
end | |
def get_min(item) | |
item == 0 ? item : item-1 | |
end | |
def get_max(item, arr_size) | |
item == arr_size ? arr_size : item+1 | |
end | |
puts get_ranges(arr_size, arr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment