diff --git a/src/ex2_dictionary.md b/src/ex2_dictionary.md index 1a76742..2c7a30e 100644 --- a/src/ex2_dictionary.md +++ b/src/ex2_dictionary.md @@ -1,11 +1,7 @@ !!!autogeneriert!!! -## exercise 2 - dictionary - (11.5/20) +## exercise 2 - dictionary - (20/20) -### a) calculate_price (6/10) -- Preis wird nicht in Euro (float) ausgegeben [`-2`] -- Preis wird nicht korrekt berechnet [`-2`] +### a) calculate_price (10/10) -### b) by_amount (5.5/10) -- `by_amount` hat inkorrekte Typannotationen [`-0.5`] -- nicht überprüft ob Produkte in `articles` enthalten [`-2`] -- `by_amount` wird nicht korrekt berechnet [`-2`] \ No newline at end of file + +### b) by_amount (10/10) diff --git a/src/ex2_dictionary.py b/src/ex2_dictionary.py index 4e4bac0..5d18dc9 100644 --- a/src/ex2_dictionary.py +++ b/src/ex2_dictionary.py @@ -10,9 +10,7 @@ def calculate_price(articles: dict[str, int], cart: dict[str, int]) -> float: def by_amount(articles: dict[str, int], cart: dict[str, int]) -> dict[int, list[str]]: out: dict[int, list[str]] = dict() - for name, amount in cart.items(): - if name not in articles: - continue + for name, amount in filter(lambda i: i[0] in articles, cart.items()): if amount not in out: out[amount] = [name] else: @@ -29,5 +27,3 @@ if __name__ == "__main__": assert by_amount(articles_dict, cart_dict) == { 2: ['apples', 'oranges'], 1: ['lemons']} - - diff --git a/src/test_dictionary.py b/src/test_dictionary.py index bd409ab..2a0f770 100644 --- a/src/test_dictionary.py +++ b/src/test_dictionary.py @@ -68,7 +68,10 @@ def test_no_typeannotation_by_amount(): def test_typeannotation_by_amount(): from ex2_dictionary import by_amount assert has_annotations( - by_amount, [dict[str, int], dict[str, int]], dict[int, list[str]]) + by_amount, [dict[str, int], dict[str, int]], dict[int, list[str]] + ) or has_annotations( + by_amount, [dict[str, int], dict[str, int]], dict[int, set[str]] + ) @pytest.mark.dependency(depends=[Task.A]) @@ -105,6 +108,11 @@ def test_price_calc(): assert abs(calculate_price(articles_dict, cart_dict) - 6) < EPS + articles_dict = {"app": 100, "ora": 100, + "lemons": 200, "avo": 500} + cart_dict = {"apples": 2, "oranges": 2, "lemons": 1, "bananas": 5} + assert abs(calculate_price(articles_dict, cart_dict) - 2) < EPS + @pytest.mark.dependency(depends=[Task.A]) @pytest.mark.timeout(10) @@ -138,7 +146,25 @@ def test_by_amount(): cart_dict = {"apples": 2, "oranges": 2, "lemons": 1, "bananas": 5} assert by_amount(articles_dict, cart_dict) == { - 2: ['apples', 'oranges'], 1: ['lemons']} + 2: ['apples', 'oranges'], 1: ['lemons'] + } or by_amount(articles_dict, cart_dict) == { + 2: {'apples', 'oranges'}, 1: {'lemons'} + } + + articles_dict = {"app": 100, "ora": 100, + "lemons": 200, "avo": 500} + cart_dict = {"apples": 2, "oranges": 2, "lemons": 1, "bananas": 5} + assert by_amount(articles_dict, cart_dict) == {1: ['lemons']} or by_amount( + articles_dict, cart_dict) == {1: {'lemons'}} + + articles_dict = {"apples": 100, "oranges": 100, + "lemons": 200, "avocado": 500} + cart_dict = {"apples": 42, "oranges": 99, "lemons": 1, "bananas": 1} + assert by_amount(articles_dict, cart_dict) == { + 99: ['oranges'], 42: ['apples'], 1: ['lemons'] + } or by_amount(articles_dict, cart_dict) == { + 99: {'oranges'}, 42: {'apples'}, 1: {'lemons'} + } @pytest.mark.dependency(depends=[Task.B]) @@ -150,8 +176,7 @@ def test_sideeffects_by_amount(): "lemons": 200, "avocado": 500} b = {"apples": 2, "oranges": 2, "lemons": 1, "bananas": 5} - r = by_amount(a2 := deepcopy(a), b2 := deepcopy(b)) == { - 2: ['apples', 'oranges'], 1: ['lemons']} + r = by_amount(a2 := deepcopy(a), b2 := deepcopy(b)) assert a2 == a and b2 == b and r is not a and r is not b