Skip to content

Instantly share code, notes, and snippets.

@haakonn
haakonn / gist:3938834
Created October 23, 2012 13:45
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)
@haakonn
haakonn / gist:2353416
Created April 10, 2012 18:22
Utility to concatenate an array of single digits ([1,2,3]) into an int (123)
public class ArrayToIntUtil {
public static int a2i(int... a) {
int result = 0;
int f = (int) Math.pow(10, a.length);
for (int i : a) result += i * (f /= 10);
return result;
}
public static void main(String... args) {