Skip to content

Instantly share code, notes, and snippets.

@haakonn
Created October 23, 2012 13:45
Show Gist options
  • Save haakonn/3938834 to your computer and use it in GitHub Desktop.
Save haakonn/3938834 to your computer and use it in GitHub Desktop.
Python function for partitioning a range of integers into X number of subranges
def partition(partition_count, id_range):
"""
Partitions an integer range into partition_count partitions of uniform size.
id_range specifies the integer range to partition (a tuple (from, to)).
"""
partition_size = ceil((id_range[1] - id_range[0]) / partition_count) + 1
for x in range(0, partition_count):
from_ = int(x * partition_size) + id_range[0]
to = min(int(from_ + partition_size - 1), id_range[1])
yield (from_, to)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment