Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions ruby/potter/5/potter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Potter

#Constants
BPRICE = 8
#Main invocation for returning the price
def price(books)
#Checks for an empty collection
return 0 if books.empty?
if books.uniq.size == 1
BPRICE * books.count
else
count_collection(books)
end

end


def count_collection(books)
collection = books.clone.uniq.size
sum = 0
case collection
when 2
sum += BPRICE * collection * 0.95
when 3
sum += BPRICE * collection * 0.90
when 4
sum += BPRICE * collection * 0.80
when 5
sum += BPRICE * collection * 0.75
end
end

end
42 changes: 42 additions & 0 deletions ruby/potter/5/potter_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "minitest/autorun"
require './potter'

class Test_Potter < Minitest::Test
def setup
@potter = Potter.new
end

def test_basics
assert_equal(0, @potter.price([]))
assert_equal(8, @potter.price([0]))
assert_equal(8, @potter.price([0]))
assert_equal(8, @potter.price([0]))
assert_equal(8, @potter.price([0]))
assert_equal(8, @potter.price([0]))

assert_equal(8 * 2, @potter.price([0, 0]))
assert_equal(8 * 3, @potter.price([0, 0, 0]))
end

def test_simple_discounts
assert_equal(8 * 2 * 0.95, @potter.price([0, 1])) #15.2
assert_equal(8 * 3 * 0.90, @potter.price([0, 1, 2])) #21.6
assert_equal(8 * 4 * 0.80, @potter.price([0, 1, 2, 3])) #25.6
assert_equal(8 * 5 * 0.75, @potter.price([0, 1, 2, 3, 4])) #30.0

end

def test_several_discounts
assert_equal(8 + (8 * 2 * 0.95), @potter.price([0, 0, 1])) #23.2
#assert_equal(2 * (8 * 2 * 0.95), @potter.price({b1: 2, b2: 2, b3: 0, b4: 0, b5: 0})) #30.4
#assert_equal((8 * 4 * 0.8) + (8 * 2 * 0.95), @potter.price({b1: 2, b2: 1, b3: 2, b4: 1, b5: 0})) #40.8
#assert_equal(8 + (8 * 5 * 0.75), @potter.price({b1: 1, b2: 2, b3: 1, b4: 1, b5: 1})) #38
end

def edge_cases
assert_equal(2 * (8 * 4 * 0.80), @potter.price({b1: 2, b2: 2, b3: 2, b4: 1, b5: 1}))
assert_equal(3 * (8 * 5 * 0.75) + 2 * (8 * 4 * 0.8),
@potter.price({b1: 5, b2: 5, b3: 4, b4: 5, b5: 4}))
end

end