runtime
144 / 500
question 144 Advanced Functions & Recursion · easy

Is the product anywhere in this category?

A shop's categories nest to any depth, and each category lists its own products. Before deleting a category, the shop checks whether a product is filed anywhere inside it. Write contains_product(category, product): return True if the product is listed in this category or in any category below it, otherwise False.

Functioncontains_product(category, product)
category{"name": …, "products": [names…], "children": [categories of the same shape…]}
ReturnsTrue or False
Rulesnames are compared exactly (capitals matter); a category may have no products and no children
Examples
callcontains_product({'name': 'All', 'products': [], 'children': [{'name': 'Electronics', 'products': ['Organic Speaker'], 'children': [{'name': 'Audio', 'products': ['Studio Headphones', 'Pocket Radio'], 'children': [{'name': 'Headphones', 'products': ['Travel Earbuds'], 'children': []}]}, {'name': 'Cameras', 'products': ['Action Cam'], 'children': []}]}, {'name': 'Home', 'products': ['Deluxe Vase', 'Nordic Candle'], 'children': []}]}, 'Pocket Radio')returnsTrue
callcontains_product({'name': 'All', 'products': [], 'children': [{'name': 'Electronics', 'products': ['Organic Speaker'], 'children': [{'name': 'Audio', 'products': ['Studio Headphones', 'Pocket Radio'], 'children': [{'name': 'Headphones', 'products': ['Travel Earbuds'], 'children': []}]}, {'name': 'Cameras', 'products': ['Action Cam'], 'children': []}]}, {'name': 'Home', 'products': ['Deluxe Vase', 'Nordic Candle'], 'children': []}]}, 'Garden Hose')returnsFalse
callcontains_product({'name': 'All', 'products': [], 'children': [{'name': 'Electronics', 'products': ['Organic Speaker'], 'children': [{'name': 'Audio', 'products': ['Studio Headphones', 'Pocket Radio'], 'children': [{'name': 'Headphones', 'products': ['Travel Earbuds'], 'children': []}]}, {'name': 'Cameras', 'products': ['Action Cam'], 'children': []}]}, {'name': 'Home', 'products': ['Deluxe Vase', 'Nordic Candle'], 'children': []}]}, 'Nordic Candle')returnsTrue

Loading your progress…

Is the product anywhere in this category? — Python practice · Runtime