Skip to content

Instantly share code, notes, and snippets.

@samflores
Last active August 9, 2020 10:33
Show Gist options
  • Save samflores/e504fd8683378a95d5209d53dca96852 to your computer and use it in GitHub Desktop.
Save samflores/e504fd8683378a95d5209d53dca96852 to your computer and use it in GitHub Desktop.
Pay the bills
## Pay the bills!
#
# write the function bellow that tells which bills the user is able to
# pay with the available budget
#
# the budget parameter is a number representing the amount available
# the bills parameter is a array of hash objects in the following format:
# [
# { value: 100, type: :water, priority: 1 },
# { value: 200, type: :credit_card },
# { value: 100, type: :phone },
# { value: 150, type: :electricity, priority: 2 }
# ]
#
# the expected response is composed of three array:
# - the bills paid in full,
# - the bills partially paid,
# - the bills unpaid
#
# some examples:
# if the budget is 900 the expected result for the input above is:
# - [ [:credit_card, :phone, :water, :electricity], [], [] ]
# ^ ^ ^
# +--- paid in full | +- unpaid
# partially paid -----+
#
# if the budget is 300 the expected result for the input above is:
# - [ [:credit_card, :phone], [], [:water, :electricity] ]
# ^ ^ ^
# +--- paid in full | +- unpaid
# partially paid -----+
#
# if the budget is 350 the expected result for the input above is:
# - [ [:credit_card, :phone], [:water], [:electricity] ]
# ^ ^ ^
# +--- paid in full | +- unpaid
# partially paid -----+
#
# please note that the smaller the priority value the higher the priority,
# so bills with priority 0 (the default), must be paid BEFORE bills with priority 1
# and so on. bills with the same priority must be paid in the order they appear in
# the input array.
def pay_bills(budget, bills)
# ...
end
## This spec suite isn't exaustive. For example, it doesn't cover the priority case. You shuold add those examples.
require 'minitest/spec'
require 'minitest/autorun'
require_relative 'pay_bills'
describe '#pay_bills' do
subject { pay_bills(budget, input) }
describe 'when budget is enough to pay all bills' do
let(:budget) { 600 }
let(:input) do
[
{ value: 200, type: :water },
{ value: 300, type: :phone },
]
end
it 'puts everything in the paid array' do
_(subject).must_equal [
%i[water phone],
[],
[]
]
}
end
describe 'when budget is enough to pay exactly some bills' do
let(:budget) { 500 }
let(:input) do
[
{ value: 200, type: :water },
{ value: 300, type: :credit_card },
{ value: 300, type: :phone },
]
end
it 'puts paid bills in the first array and unpaid in the last' do
_(subject).must_equal [
%i[water credit_card],
[],
%i[phone]
]
}
end
describe 'when budget is enough to pay some bills and partially others' do
let(:budget) { 600 }
let(:input) do
[
{ value: 200, type: :water },
{ value: 300, type: :credit_card },
{ value: 300, type: :phone },
]
end
it 'puts paid bills in the first array and partially paid in the second' do
_(subject).must_equal [
%i[water credit_card], # completely paid
%i[phone] # partially paid
[], # unpaid
]
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment