SumAllNumberCombinations
Sum all the numbers in a flattened array of numbers
Installation
Add this line to your application's Gemfile:
gem 'sum_all_number_combinations'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sum_all_number_combinations
Usage
Create your instance variable, passing in the array you would like to sum all the combinations of.
sum_of_all = SumAllCombinations.new([1,2,3])
Call the sum method and pass in the optional keyword arguments of remove_duplicates (default is false) and sort (default is true)
sum_of_all.sum(remove_duplicates: false, sort: true)
The attributes available include:
# passed in array
@original
# flattened array. After sum is called flattened will only include the numbers used for calculation, non-numeric objects will be removed
@flattened
# calculated values array, which is the array holding all the numbers calculated from the addition of all of the possible number combinations, after sum is called
@calculated_values
#combinations used array, which is the array holding all the information of how the numbers were combined to determine results
@combinations_used
Example
sum_of_all = SumAllCombinations.new([1,2,[3,4], "house"])
sum_of_all.original #=> [1, 2, [3, 4], "house"]
sum_of_all.flattened #=> [1, 2, 3, 4, "house"]
sum_of_all.sum #=> [3.0, 4.0, 5.0, 5.0, 6.0, 6.0, 7.0, 7.0, 8.0, 9.0, 10.0]
sum_of_all.calculated_values #=> [3.0, 4.0, 5.0, 5.0, 6.0, 6.0, 7.0, 7.0, 8.0, 9.0, 10.0]
sum_of_all.combinations_used #=> ["1.0+2.0+3.0+4.0=10.0", "1.0+2.0+3.0=6.0", "1.0+2.0+4.0=7.0", "1.0+2.0=3.0", "1.0+3.0+4.0=8.0", "1.0+3.0=4.0", "1.0+4.0=5.0", "2.0+3.0+4.0=9.0", "2.0+3.0=5.0", "2.0+4.0=6.0", "3.0+4.0=7.0"]
Development
After checking out the repo, run bin/setup
to install dependencies. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/paulhtrott/sum_all_number_combinations.