Skip to content

Instantly share code, notes, and snippets.

@ihower
Last active April 10, 2017 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihower/a8f0e5cfcec37ec42ac20a4f2aa14d2a to your computer and use it in GitHub Desktop.
Save ihower/a8f0e5cfcec37ec42ac20a4f2aa14d2a to your computer and use it in GitHub Desktop.
# 输入一个数组,表示每种书买本书,例如 [1,2,0,0,0] 是第一集买一本、第二集买两本
def book_price(order)
price = [0, 1 * 100, 0.95 * 200, 0.9 * 300, 0.8 * 400, 0.75 * 500 ]
order = order.reject{ |x| x <= 0 }
if order.size == 0
return 0
else
kind = order.count{ |x| x > 0 }
min_size = order.min
return book_price(order.map{ |x| x - min_size }) + (price[kind] * min_size)
end
end
describe "Book price" do
it [1,0,0,0,0] do
result = book_price([1,0,0,0,0])
expect(result).to eq(100)
end
it [1,1,0,0,0] do
result = book_price([1,1,0,0,0])
expect(result).to eq(190)
end
it [1,1,1,0,0] do
result = book_price([1,1,1,0,0])
expect(result).to eq(270)
end
it [1,1,1,1,0] do
result = book_price([1,1,1,1,0])
expect(result).to eq(320)
end
it [1,1,1,1,1] do
result = book_price([1,1,1,1,1])
expect(result).to eq(375)
end
it [2,1,1,1,1] do
result = book_price([2,1,1,1,1])
expect(result).to eq(475)
end
it [3,1,1,1,1] do
result = book_price([3,1,1,1,1])
expect(result).to eq(575)
end
it [2,2,1,1,1] do
result = book_price([2,2,1,1,1])
expect(result).to eq(565)
end
it [3,2,1,1,1] do
result = book_price([3,2,1,1,1])
expect(result).to eq(665)
end
it [2,2,2,1,1] do
result = book_price([2,2,2,1,1])
expect(result).to eq(645)
end
it [2,2,2,2,1] do
result = book_price([2,2,2,2,1])
expect(result).to eq(695)
end
it [2,2,2,2,2] do
result = book_price([2,2,2,2,2])
expect(result).to eq(750)
end
it [3,2,2,2,2] do
result = book_price([3,2,2,2,2])
expect(result).to eq(850)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment