Skip to content

Commit

Permalink
Solved special case using permutation
Browse files Browse the repository at this point in the history
  • Loading branch information
javar committed Feb 3, 2015
1 parent 4756993 commit 1f004d0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
46 changes: 34 additions & 12 deletions kata_potter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,41 @@ def discount_applicable?(books)
books.count == books.uniq.count && books.count >= MIN_BOOKS_FOR_DISCOUNT
end

def basket(books)
return calculate_discount_price(books.count) if discount_applicable?(books)
price_with_disccount = 0
def basket_split(books, size)

if discount_applicable?(books.uniq)
price_with_disccount = calculate_discount_price(books.uniq.count)
[books.first(size), books.last(books.count - size)]
end

def basket(books)
#return calculate_discount_price(books.count) if discount_applicable?(books)

books.uniq.each {|book|
index_to_delete = books.index(book)
books.slice!(index_to_delete)
}
return price_with_disccount + books.count * BOOK_PRICE
#if discount_applicable?(books)
result = books.permutation.map{|element|
(0..element.length).map{|size|
partial_total = 0
basket_split(element, size).each{|books_splited|
if discount_applicable?(books_splited)
partial_total += calculate_discount_price(books_splited.count)
else
partial_total += books_splited.count * BOOK_PRICE
end
}
#puts "partial_total= #{partial_total}"
partial_total
}.min
}.min
#puts "result=#{result}"
return result

#price_with_disccount = calculate_discount_price(books.uniq.count)
#books.uniq.each {|book|
# index_to_delete = books.index(book)
# books.slice!(index_to_delete)
#}
#return price_with_disccount + books.count * BOOK_PRICE
#else
# books.count * BOOK_PRICE
#end

end
books.count * BOOK_PRICE

end
5 changes: 5 additions & 0 deletions test_potter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@ def test_five_different_books_have_a_discount_25_percent
def test_3_different_books_and_one_repeated_book
assert_equal 29.6, basket([1,2,3,1])
end

def test_2_different_books_and_3_repeated_book
assert_equal 51.20, basket([1,1,2,2,3,3,4,5])
#assert_equal 51.20, basket([1,2,3,4,1,2,3,5])
end

end

0 comments on commit 1f004d0

Please sign in to comment.