Skip to content

Instantly share code, notes, and snippets.

@shunfan
Last active December 19, 2015 02:39
Show Gist options
  • Save shunfan/5884899 to your computer and use it in GitHub Desktop.
Save shunfan/5884899 to your computer and use it in GitHub Desktop.
# http://v2ex.com/t/74002
list = [0, 0, 0,
2295, 2295, 1530,
0, 0, 0, 0, 0,
1020, 1530, 1530, 1530, 1530, 2295, 2550, 1785,
0, 0, 0, 0, 0,
1020, 1530, 1530, 1530, 1530, 2295, 2550,
0, 0, 0, 0, 0,
1275, 1530, 1530, 1530, 1530, 1530,
0, 0, 0, 0, 0, 0]
index = 0
natural_number_list = []
detect_natural = proc { list.detect { |i| i != 0 } }
while detect_natural.call
index += list.index(detect_natural.call)
list = list[list.index(detect_natural.call)..list.length]
natural_numbers = list.take_while { |i| i != 0 }
natural_number_list.push([natural_numbers.length,
[index, index + natural_numbers.length - 1]])
index += natural_numbers.length
list.shift(natural_numbers.length)
end
p natural_number_list
# => [[3, [3, 5]], [8, [11, 18]], [7, [24, 30]], [6, [36, 41]]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment