4 kyu

Prepare the Cocktails

17 of 24bledding
Description
Loading description...
Algorithms
Functional Programming
Performance
Recursion
Set Theory
  • Please sign in or sign up to leave a comment.
  • Mednoob Avatar
  • LexiWar Avatar

    This comment has been deleted.

  • LexiWar Avatar

    Timed OutPassed: 10Failed: ?Exit Code: 1 Test Results: Log ['Liquor', 'Vodka', 'Mint', 'Pineapple', 'Watermelon'] Basic tests Simple tests (5 of 5 Assertions) Impossible cocktails (2 of 2 Assertions) Not enough ingredients (2 of 2 Assertions) Ice: taste = 0 Completed in 9.37ms Performance tests 26^2 ingredients (5 datasets) STDERR Execution Timed Out (12000 ms) Why did my code time out? Our servers are configured to only allow a certain amount of time for your code to execute. In rare cases the server may be taking on too much work and simply wasn't able to run your code efficiently enough. Most of the time though this issue is caused by inefficient algorithms. If you see this error multiple times you should try to optimize your code further.

  • Alan-Turing13 Avatar

    26^2 different ingredients though truly a tropical paradise

  • dfhwze Avatar

    As stipulated by the user, their solution is not correct for all scenarios. Random test case generation should prevent solutions that aren't 100% correct from passing.

  • dfhwze Avatar

    Unfortunately, due to the problem's volatile execution time, your solution may timeout despite being correct.

    I believe this is due to the solution's volatile execution time, not the problem's. I propose you use another ref sol, based on either of the current top solutions (I would pick the slowest of the two to give users some margin). You would gain a stable ref sol and be able to work on making performance tests even heavier.

  • Mednoob Avatar

    The current test creates deep copy for each test. Deep copy is not needed, since the values of the dictionary are only ints. Instead, use shallow copy (which is faster than deep copy). Shallow copy can be made using ingr.copy().

  • dfhwze Avatar

    Can different ingredients have the same taste?

  • Voile Avatar

    Tests sometimes timing out is fine, but tests sometimes allowing unperformant solution to pass is not fine.

  • dfhwze Avatar

    I still have no idea how you specify bitter sweetness. I thought it was the absolute value of either all positive, or all negative ingredients chosen. But then there are test cases with expected bitter sweetness of 0 and non-0 flavour. How is this possible?

  • Voile Avatar

    This comment has been hidden.

  • Voile Avatar

    The test checking code does not behave according to the description. This is your test checking code:

    def test_check(out : list[str], ingr : dict[int], target_flav : int, target_bittersw : int):
        if not isinstance(out, list):
            test.fail(f"You returned an object of type {type(out)}. A list is expected.")
            return
    
        s, b = 0, 0
    
        try: tastes = [ingr[o] for o in out]
        except KeyError:
            test.fail(f"You returned {out} but one or more of these ingredients don't exist.")
            return
    
        for t in (ingr[o]for o in out):
            if t > 0: s += t
            else: b -= t
        flav, bittersw = s-b, min(s,b)
        
        if (len(out), flav, bittersw) == (5, target_flav, target_bittersw):
            test.pass_()
        else:
            test.fail(
                f"""Test case failed:\n
                - Your solution's tastes: {tastes}\n
                - Your solution's flav. & bittersw.: {flav}, {bittersw}\n
                - Target flav. & bittersw.: {target_flav}, {target_bittersw}\n"""
            )
    

    First, description says

    The same ingredient may be added multiple times to the same cocktail, and their taste is counted once per unit. Tastes are unique.

    but the test code doesn't obey to this.

    Second, your description of bittersweetness is really poor: I looked at the description many times and every interpretation I came up with is wrong, until I looked at the test checking code. You should really just put test_check in the tests so it is clear how the checking is done.

    (You'll need to do this anyway, since the actual tests depend completely on test_check, but it is in preloaded and so user code can modify it before the tests are run.)

  • Mednoob Avatar
    Input flav: -2
    Input bittersw: 6
    My result: ['Pineapple', 'Mint', 'Vodka', 'Vodka', 'Vodka']
    Converted to tastes:  [3, 1, -2, -2, -2]
    Test case failed:
    
            - Your solution's tastes: [3, 1, -2, -2, -2]
    
            - Your solution's flav. & bittersw.: 4, 6
    
            - Target flav. & bittersw.: -2, 4
    : (5, -2, 4) should equal (5, -2, 6)
    

    Isn't flav. should be the sum of all the tastes, and the bittersw. is the sum of the negative tastes? Or did I miss something in the description?

  • Voile Avatar

    Testing code should be more robust: it throws an error if it expects a mix to exist but something else is returned.

    Also, there is a wrong test:

        @test.it("Impossible cocktails")
        def test_impossible_cocktail():
            ingr = [{
                "Momordica"  : -6,
                "Liquor"     : -4,
                "Vodka"      : -2,
                "Orange"     :  2,
                "Watermelon" :  4,
                "Sugar"      :  6
            }]
            test.assert_equals(make_cocktail(ingr, 1, 2), [])   # Impossible flavour
            test.assert_equals(make_cocktail(ingr, 4, 3), [])   # Impossible bittersweetness
    

    ingr should be the object, not the list.