runtime
117 / 500
question 117 Collection Algorithms · medium

Search the catalogue under a budget

A shop assistant types a few words and a budget, and wants the stock codes of the matching products. Product descriptions in this catalogue are all capitals; people type however they like.

Functionsearch_products(products, query, max_price)
productsa list of dictionaries with stock_code, description and list_price
Matchthe description contains the query, ignoring capital letters and spaces at the ends of the query, and list_price is at most max_price
Returnsthe matching stock codes, in catalogue order
Rulesa query that is empty after stripping matches every description
Examples
callsearch_products([{'stock_code': '21329', 'description': 'DINOSAURS WRITING SET', 'list_price': 0.98}, {'stock_code': '84031A', 'description': 'CHARLIE+LOLA RED HOT WATER BOTTLE', 'list_price': 3}, {'stock_code': '84031B', 'description': 'CHARLIE LOLA BLUE HOT WATER BOTTLE', 'list_price': 3}, {'stock_code': '21411', 'description': 'GINGHAM HEART DOORSTOP RED', 'list_price': 2.5}, {'stock_code': '85132A', 'description': 'CHARLIE + LOLA BISCUITS TINS', 'list_price': 6.38}], 'hot water', 5)returns['84031A', '84031B']
callsearch_products([{'stock_code': '21329', 'description': 'DINOSAURS WRITING SET', 'list_price': 0.98}, {'stock_code': '84031A', 'description': 'CHARLIE+LOLA RED HOT WATER BOTTLE', 'list_price': 3}, {'stock_code': '84031B', 'description': 'CHARLIE LOLA BLUE HOT WATER BOTTLE', 'list_price': 3}, {'stock_code': '21411', 'description': 'GINGHAM HEART DOORSTOP RED', 'list_price': 2.5}, {'stock_code': '85132A', 'description': 'CHARLIE + LOLA BISCUITS TINS', 'list_price': 6.38}], 'charlie', 4)returns['84031A', '84031B']
callsearch_products([{'stock_code': '21329', 'description': 'DINOSAURS WRITING SET', 'list_price': 0.98}, {'stock_code': '84031A', 'description': 'CHARLIE+LOLA RED HOT WATER BOTTLE', 'list_price': 3}, {'stock_code': '84031B', 'description': 'CHARLIE LOLA BLUE HOT WATER BOTTLE', 'list_price': 3}, {'stock_code': '21411', 'description': 'GINGHAM HEART DOORSTOP RED', 'list_price': 2.5}, {'stock_code': '85132A', 'description': 'CHARLIE + LOLA BISCUITS TINS', 'list_price': 6.38}], 'teapot', 10)returns[]

Loading your progress…

Search the catalogue under a budget — Python practice · Runtime