Skip to content

Instantly share code, notes, and snippets.

@jasonwbarnett
Last active December 20, 2015 16:29
Show Gist options
  • Save jasonwbarnett/6161693 to your computer and use it in GitHub Desktop.
Save jasonwbarnett/6161693 to your computer and use it in GitHub Desktop.
This will take a starting number, ending number, and group size and it will return an array of groups [rangeStart, rangeEnd]
# Example use:
#
# 1.9.3p392 :000 > equal_div(432,239842934,22)
# => [[432, 10902364], [10902365, 21804297], [21804298, 32706230], [32706231, 43608163], [43608164, 54510096], [54510097, 65412029], [65412030, 76313962], [76313963, 87215895], [87215896, 98117828], [98117829, 109019761], [109019762, 119921694], [119921695, 130823627], [130823628, 141725560], [141725561, 152627493], [152627494, 163529426], [163529427, 174431359], [174431360, 185333292], [185333293, 196235225], [196235226, 207137158], [207137159, 218039091], [218039092, 228941024], [228941025, 239842934]]
#
# 1.9.3p392 :001 > equal_div(432,239842934,4)
# => [[432, 59961058], [59961059, 119921685], [119921686, 179882312], [179882313, 239842934]]
#
def equal_div(first, last, num_of_groups)
total = last - first
group_size = total / num_of_groups + 1
top = first
bottom = top + group_size
blocks = 1.upto(num_of_groups).inject([]) do |result, x|
bottom = last if bottom > last
result << [ top, bottom ]
top += group_size + 1
bottom = top + group_size
result
end
blocks
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment