Skip to content

Instantly share code, notes, and snippets.

@NILID
Created March 4, 2021 12:26
Show Gist options
  • Save NILID/55c6597b8407415c65a5f243b35c7b53 to your computer and use it in GitHub Desktop.
Save NILID/55c6597b8407415c65a5f243b35c7b53 to your computer and use it in GitHub Desktop.
Displaying empty range indices based on filled range indices
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