Junior — Senior
Flexible filtering of ads by price range and name
livecode
Task condition
There is an array of advertisement objects. It is required to implement a function that returns those ads whose prices are within a specified range and/or whose titles contain a given substring query. Each criterion – lower price limit, upper price limit, and title search query – should be optional, i.e., if a parameter is not provided, the corresponding filter is not applied.
items = [
{"title": "3-room apartment, 80m², 13/22 floor", "rooms": 3, "price": 70000, "petFriendly": True},
{"title": "2-room apartment, 45m², 5/5 floor", "rooms": 2, "price": 60000, "petFriendly": False, "deposit": 60000},
{"title": "2-room apartment, 44m², 4/4 floor", "rooms": 2, "price": 45000, "petFriendly": True, "agency": True},
{"title": "2-room apartment, 44m², 4/4 floor", "rooms": 2, "price": 45000, "petFriendly": True, "agency": True, "fee": 45000},
# ...
]
The function should accept a list of ads and three optional arguments: min_price, max_price, and query. If any of these arguments are absent, the corresponding filter criterion is ignored. The return value should be a list of ads that meet all specified conditions.
Example call:
result = filter_ads(items, min_price=50000, max_price=80000, query="квартира")