Write the function optimal_gifts(gifts: int, recipients: int) -> int
You are given gifts
number of gifts and recipients
number of recipients.
Return the maximum number of recipients that can receive 8 gifts under the following conditions:
- No recipient can recieve no gifts
- No recipient can recieve 4 gifts
- No recipient can receive more than 8 gifts
- All gifts must be distibuted unless all recipients are already recveiving 8 gifts
Examples:
-
optimal_gifts(8, 2)
returns 0 -
optimal_gifts(12, 2)
returns 0 -
optimal_gifts(8, 1)
returns 1 -
optimal_gifts(24, 2)
returns 2 -
optimal_gifts(24, 3)
returns 3 -
optimal_gifts(24, 4)
returns 2
def optimal_gifts(gifts: int, recipients: int) -> int:
kids = [1 for _ in range(gifts)]
gifts -= recipients
for i in range(recipients):
while (gifts > 0 and kids[i] < 8):
kids[i] += 1
gifts -= 1
eigths = sum([1 if kid == 8 else 0 for kid in kids])
if 4 in kids:
eigths -= 1
return eigths
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Examples")
def test_group():
@test.it("Example 1")
def test_case1():
test.assert_equals(optimal_gifts(8, 2), 0)
@test.it("Example 2")
def test_case1():
test.assert_equals(optimal_gifts(12, 2), 0)
@test.it("Example 3")
def test_case1():
test.assert_equals(optimal_gifts(8, 1), 1)
@test.it("Example 4")
def test_case1():
test.assert_equals(optimal_gifts(24, 2), 2)
@test.it("Example 5")
def test_case1():
test.assert_equals(optimal_gifts(24, 3), 3)
@test.it("Example 6")
def test_case1():
test.assert_equals(optimal_gifts(24, 4), 2)