Move History

Fork Selected
  • Description

    O(1) solution. Now returns None for cases where it is impossible to meet constraints.

    Code
    def optimal_gifts(gifts: int, recipients: int) -> int:
        if recipients > gifts or gifts == 4 and recipients == 1:
            return None
        if gifts >= recipients * 8:
            return recipients
        if gifts == recipients * 8 - 4 or gifts == (recipients - 1) * 8:
            return recipients - 2
        return gifts // 8
    Preloaded Code
    def optimal_gifts(gifts: int, recipients: int) -> int:
        pass
    Test Cases
    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_case2():
            test.assert_equals(optimal_gifts(12, 2), 0)
        @test.it("Example 3")
        def test_case3():
            test.assert_equals(optimal_gifts(8, 1), 1)
        @test.it("Example 4")
        def test_case4():
            test.assert_equals(optimal_gifts(24, 2), 2)
        @test.it("Example 5")
        def test_case5():
            test.assert_equals(optimal_gifts(24, 3), 3)
        @test.it("Example 6")
        def test_case6():
            test.assert_equals(optimal_gifts(24, 4), 2)
        @test.it("Example 7")
        def test_case7():
            test.assert_equals(optimal_gifts(1, 2), None)
        @test.it("Example 8")
        def test_case8():
            test.assert_equals(optimal_gifts(4, 1), None)
  • Code
    • 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
    • if recipients > gifts or gifts == 4 and recipients == 1:
    • return None
    • if gifts >= recipients * 8:
    • return recipients
    • if gifts == recipients * 8 - 4 or gifts == (recipients - 1) * 8:
    • return recipients - 2
    • return gifts // 8
    Test Cases
    • 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():
    • def test_case2():
    • test.assert_equals(optimal_gifts(12, 2), 0)
    • @test.it("Example 3")
    • def test_case1():
    • def test_case3():
    • test.assert_equals(optimal_gifts(8, 1), 1)
    • @test.it("Example 4")
    • def test_case1():
    • def test_case4():
    • test.assert_equals(optimal_gifts(24, 2), 2)
    • @test.it("Example 5")
    • def test_case1():
    • def test_case5():
    • test.assert_equals(optimal_gifts(24, 3), 3)
    • @test.it("Example 6")
    • def test_case1():
    • def test_case6():
    • test.assert_equals(optimal_gifts(24, 4), 2)
    • @test.it("Example 7")
    • def test_case7():
    • test.assert_equals(optimal_gifts(1, 2), None)
    • @test.it("Example 8")
    • def test_case8():
    • test.assert_equals(optimal_gifts(4, 1), None)