Skip to content

Instantly share code, notes, and snippets.

@d2fn
Created February 14, 2011 07:03
Show Gist options
  • Save d2fn/825578 to your computer and use it in GitHub Desktop.
Save d2fn/825578 to your computer and use it in GitHub Desktop.
partition set of numbers into subsets of equal size
% produce a list, the elements of which are each lists of
% length 'Depth' of less forming sorted, equally sized bins
% of the numeric input list L
% the last bin will be of size (Depth rem length(L))
equal_size_bins (L,Depth) ->
[tuple_to_list(X) || X <- equal_size_bins_sorted(lists:sort(L),Depth)].
equal_size_bins_sorted (L,Depth)
when Depth >= length(L)
-> [list_to_tuple(L)];
equal_size_bins_sorted (L,Depth) ->
{NextBin,Remaining} = lists:split(Depth,L),
[ list_to_tuple(NextBin) | equal_size_bins_sorted(Remaining,Depth)].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment