Array Partition

by Aish Fenton for vWorkApp

A little gem that gives you all partitions of an array into at most k sub-arrays. Useful for generating test scenarios and solving certain kinds of optimisation problems.

["Apple", "Orange", "Pear"].partition(2)

[
    [["Apple", "Orange", "Pear"]], 
    [["Apple", "Orange"], ["Pear"]], 
    [["Apple", "Pear"], ["Orange"]], 
    [["Apple"], ["Orange", "Pear"]]
]

The gem can also be used to provide the Stirling Number of the Second Kind of the array.

Install

sudo gem install array_partition

Usage

See example/fuel_tanker.rb for a real-live application of partitions being used to solve a combinatorial optimisation problem.

Copyright (c) 2011 vWorkApp Inc.