Filter Restaurants by Vegan-Friendly, Price and Distance
Given the array of restaurants and the specific filters, we want to filter the restaurants and return their IDs sorted by rating and then by ID.
The code can be simple by using Python’s built-in functions for filtering and sorting.
|
|
Explanation:
- We first filter the restaurants based on the provided constraints. We include a restaurant if the
veganFriendly
filter is 0 or if it matches the restaurant’s vegan-friendly status, and if its price and distance are within the given maximum limits. - We then sort the filtered restaurants by rating in descending order, and by ID in descending order for those with the same rating.
- Finally, we extract and return the IDs of the sorted restaurants.
This code applies the given filters and then orders the restaurants as required, providing the correct result.