Created
October 23, 2012 13:45
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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