GithubHelp home page GithubHelp logo

djpowers / divvy_up Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 252 KB

A Ruby gem to help you divide a list of items and prices into (roughly) even parts

Home Page: https://rubygems.org/gems/divvy_up

License: MIT License

Ruby 100.00%

divvy_up's Introduction

DivvyUp

Gem Version

A Ruby gem to divvy up a list of item prices into smaller groups, for the purpose of splitting up purchases (somewhat) equally.

A variation of sorts on the knapsack problem.

Installation

Add this line to your application's Gemfile:

gem 'divvy_up'

And then execute:

$ bundle

Or install it yourself as:

$ gem install divvy_up

Usage

shopping_list = {
  orange_juice: { price: 3, quantity: 2 },
  lettuce: 7,
  strawberries: 3,
  eggs: { price: 2.79 },
  carrots: 2.5,
  onion: 1.25,
  tomato: 1.25,
  blueberries: 3.99,
  butter: 2.69,
  pasta_sauce: 2.5,
  pepper: 2,
  celery: 1.69
}

DivvyUp::List.new(shopping_list).split(3)
# =>
# [
#   [{:eggs=>2.79, :carrots=>2.5, :onion=>1.25, :butter=>2.69, :pepper=>2}, 11.23],
#   [{:tomato=>1.25, :blueberries=>3.99, :orange_juice_1=>3, :orange_juice_2=>3}, 11.24],
#   [{:lettuce=>7, :pasta_sauce=>2.5, :celery=>1.69}, 11.19]
# ]

Pass a hash as an argument when creating a new List, where the keys are the item names, and the values are the prices, as an integer or float.

Optionally, you may specify the price in an attributes hash. If you have more than one of an item, you can specify the quantity here as well.

Output of #split method consists of an array of arrays, where each subarray is a hash of items and the total value of those items.

Contributing

  1. Fork it ( https://github.com/djpowers/divvy_up/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

divvy_up's People

Contributors

djpowers avatar

Stargazers

 avatar

Watchers

 avatar

divvy_up's Issues

Duplicate items in a list are not supported

Since the list is hash-based, there is not a way to include two items of the same exact name. The following message is given in such situations: warning: duplicated key at line 1 ignored

Split method hangs on when list contains many items

Certain lists will not complete, appearing to freeze. Presumably, the "permute" technique is taking up too much memory while generating all possible results either due to a large number of items, or splitting several ways.

shopping_list = { orange_juice_1: 3, orange_juice_2: 3, eggs_dozen: 2.99, spring_mix_16_oz: 6.99, bacon: 4.99, pasta_sauce: 2.50, blueberries: 3.99, frozen_strawberries: 2.99, olive_oil: 8.99, paper_towels: 1.59, toilet_paper: 1.99, aluminum_foil: 3 }
list = DivvyUp::List.new(shopping_list)
list.split(3)

A possible solution would be to detect when this problem is occurring or will occur, and use one of the other simpler splitting algorithms.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.