Skip to content

Instantly share code, notes, and snippets.

@StFS
Created March 19, 2020 15:55
Show Gist options
  • Save StFS/c940ba02ccf2572d3fb107e78b1a7358 to your computer and use it in GitHub Desktop.
Save StFS/c940ba02ccf2572d3fb107e78b1a7358 to your computer and use it in GitHub Desktop.
def numbers = 1..6
def max_splits = 7
count = numbers.size()
overflow = count % max_splits
pergroup = count.intdiv(max_splits)
println "Count: " + count + ", overflow: " + overflow + ", pergroup: " + pergroup
source_idx = 0
result = []
splits = Math.min(max_splits, count)
0.upto( splits-1 ) {
if (it < overflow) {
slice_size = pergroup + 1
} else {
slice_size = pergroup
}
result += [numbers[source_idx .. (source_idx + slice_size - 1)]]
source_idx += slice_size
}
println "Result: " + result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment