Skip to content

Commit

Permalink
Add spec to include dishes multiple times which finally gets all the …
Browse files Browse the repository at this point in the history
…features working
  • Loading branch information
esambo committed Jul 3, 2012
1 parent 0e26af9 commit 61fe03f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/restaurant_orders/menu.rb
Expand Up @@ -8,7 +8,7 @@ def initialize(dishes)
def get_combinations(target_price)
c = []
dish_perms = @dishes.permutation
dish_perms.each do |dishes|
dups_until(dish_perms, target_price).each do |dishes|
d = select_until(dishes, target_price)
c << d if d
end
Expand All @@ -17,6 +17,19 @@ def get_combinations(target_price)

private

def dups_until(dish_perms, target_price)
dish_perms.map { |dishes|
dups = []
dishes.each do |d|
price = d[1]
(target_price / price).floor.times do |_|
dups << d
end
end
dups
}
end

def select_until(dishes, target_price)
total = 0
names = []
Expand Down
39 changes: 39 additions & 0 deletions spec/restaurant_orders/menu_spec.rb
Expand Up @@ -62,6 +62,45 @@ module RestaurantOrders
end
end

context 'with two of the first dish adding up to the target price' do
let(:target_price) { 4.30 }
let(:dishes) {[
['mixed fruit', 2.15]
]}
it 'includes the first dish twice' do
menu.get_combinations(target_price).should == [
'mixed fruit',
'mixed fruit'
].sort
end
end

end

describe '(private) #dups_until' do
context 'with 3 different dishes' do
let(:dishes) {[
['a', 1],
['b', 3],
['c', 5]
]}
let(:dish_perms) {[
dishes,
[['d', 3.5]]
]}
let(:target_price) { 7 }
it 'duplicates until target price' do
menu.send(:dups_until, dish_perms, target_price).to_a.should == [
[ ['a', 1], ['a', 1], ['a', 1], ['a', 1], ['a', 1], ['a', 1], ['a', 1],
['b', 3], ['b', 3],
['c', 5]
],
[ ['d', 3.5], ['d', 3.5]
]
]
end
end
end

end
end

0 comments on commit 61fe03f

Please sign in to comment.