|
| 1 | +'''Given the provided cars dictionary: |
| 2 | +
|
| 3 | +Get all Jeeps |
| 4 | +Get the first car of every manufacturer. |
| 5 | +Get all vehicles containing the string Trail in their names (should work for other grep too) |
| 6 | +Sort the models (values) alphabetically |
| 7 | +''' |
| 8 | + |
| 9 | +cars = { |
| 10 | + 'Ford': ['Falcon', 'Focus', 'Festiva', 'Fairlane'], |
| 11 | + 'Holden': ['Commodore', 'Captiva', 'Barina', 'Trailblazer'], |
| 12 | + 'Nissan': ['Maxima', 'Pulsar', '350Z', 'Navara'], |
| 13 | + 'Honda': ['Civic', 'Accord', 'Odyssey', 'Jazz'], |
| 14 | + 'Jeep': ['Grand Cherokee', 'Cherokee', 'Trailhawk', 'Trackhawk'] |
| 15 | +} |
| 16 | + |
| 17 | + |
| 18 | +def get_all_jeeps(cars=cars): |
| 19 | + """return a comma + space (', ') separated string of jeep models |
| 20 | + (original order)""" |
| 21 | + return str(', '.join(cars['Jeep'])) |
| 22 | + |
| 23 | + |
| 24 | +def get_first_model_each_manufacturer(cars=cars): |
| 25 | + """return a list of matching models (original ordering)""" |
| 26 | + first_car = [] |
| 27 | + for k, v in cars.items(): |
| 28 | + first_car.append(v[0]) |
| 29 | + return first_car |
| 30 | + |
| 31 | + |
| 32 | +def get_all_matching_models(cars=cars, grep='trail'): |
| 33 | + """return a list of all models containing the case insensitive |
| 34 | + 'grep' string which defaults to 'trail' for this exercise, |
| 35 | + sort the resulting sequence alphabetically""" |
| 36 | + matched = [] |
| 37 | + for v in cars.values(): |
| 38 | + for i in v: |
| 39 | + if grep.lower() in i.lower(): |
| 40 | + matched.append(i) |
| 41 | + return sorted(matched) |
| 42 | + |
| 43 | + |
| 44 | +def sort_car_models(cars=cars): |
| 45 | + """return a copy of the cars dict with the car models (values) |
| 46 | + sorted alphabetically""" |
| 47 | + return {k:sorted(cars[k]) for k in cars.keys()} |
| 48 | + |
| 49 | + |
0 commit comments