|
| 1 | +cars = { |
| 2 | + 'Ford': ['Falcon', 'Focus', 'Festiva', 'Fairlane'], |
| 3 | + 'Holden': ['Commodore', 'Captiva', 'Barina', 'Trailblazer'], |
| 4 | + 'Nissan': ['Maxima', 'Pulsar', '350Z', 'Navara'], |
| 5 | + 'Honda': ['Civic', 'Accord', 'Odyssey', 'Jazz'], |
| 6 | + 'Jeep': ['Grand Cherokee', 'Cherokee', 'Trailhawk', 'Trackhawk'] |
| 7 | +} |
| 8 | + |
| 9 | + |
| 10 | +def get_all_jeeps(cars=cars): |
| 11 | + """return a comma + space (', ') separated string of jeep models (original order)""" |
| 12 | + return ", ".join(jeep for jeep in cars['Jeep']) |
| 13 | + |
| 14 | + |
| 15 | +def get_first_model_each_manufacturer(cars=cars): |
| 16 | + """return a list of matching models (original ordering)""" |
| 17 | + return [models[0] for models in cars.values()] |
| 18 | + |
| 19 | + |
| 20 | +def get_all_matching_models(cars=cars, grep='trail'): |
| 21 | + """return a list of all models containing the case insensitive |
| 22 | + 'grep' string which defaults to 'trail' for this exercise, |
| 23 | + sort the resulting sequence alphabetically""" |
| 24 | + list_matching_models = [] |
| 25 | + for models in cars.values(): |
| 26 | + for model in models: |
| 27 | + if grep.lower() in model.lower(): |
| 28 | + list_matching_models.append(model) |
| 29 | + return sorted(list_matching_models) |
| 30 | + |
| 31 | + |
| 32 | +def sort_car_models(cars=cars): |
| 33 | + """sort the car models (values) and return the resulting cars dict""" |
| 34 | + return {car: sorted(models) for car, models in cars.items()} |
| 35 | + |
0 commit comments