Skip to content

Instantly share code, notes, and snippets.

@nareshchilukuru
Created May 24, 2011 17:02
Show Gist options
  • Save nareshchilukuru/989128 to your computer and use it in GitHub Desktop.
Save nareshchilukuru/989128 to your computer and use it in GitHub Desktop.
# One copy of any of the five books costs 8 EUR. If, however, you buy two
# different books from the series, you get a 5% discount on those two books.
# If you buy 3 different books, you get a 10% discount. With 4 different
# books, you get a 20% discount. If you go the whole hog, and buy all 5, you
# get a huge 25% discount.
# Note that if you buy, say, four books, of which 3 are different titles, you
# get a 10% discount on the 3 that form part of a set, but the fourth book
# still costs 8 EUR.
# Potter mania is sweeping the country and parents of teenagers everywhere are
# queueing up with shopping baskets overflowing with Potter books. Your
# mission is to write a piece of code to calculate the price of any
# conceivable shopping basket, giving as big a discount as possible.
require './harry'
describe Harry do
it "should cost the same for single book, no matter the book" do
Harry.price([5, 0, 0, 0, 0]).should == 40
Harry.price([0, 0, 5, 0, 0]).should == 40
end
it "should get max discount for 5 unique books" do
Harry.price([1, 1, 1, 1, 1]).should == 5*8*(1-0.25)
end
it "should produce all combinations of given length, excluding 0" do
Harry.bitmasks(1).should == [0b1]
Harry.bitmasks(2).should == [0b1, 0b10, 0b11]
Harry.bitmasks(3).should == [0b1, 0b10, 0b11, 0b100, 0b101, 0b110, 0b111]
end
it "should get The Test right" do
books = [2, 2, 2, 1, 1]
Harry.price(books).should == 51.20
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment