|
| 1 | +from itertools import groupby |
| 2 | + |
| 3 | +cars = [ |
| 4 | + # need mock data? -> https://www.mockaroo.com == awesome |
| 5 | + ('Mercedes-Benz', '300D'), ('Mercedes-Benz', '600SEL'), |
| 6 | + ('Toyota', 'Avalon'), ('Ford', 'Bronco'), |
| 7 | + ('Chevrolet', 'Cavalier'), ('Chevrolet', 'Corvette'), |
| 8 | + ('Mercedes-Benz', 'E-Class'), ('Hyundai', 'Elantra'), |
| 9 | + ('Volkswagen', 'GTI'), ('Toyota', 'Highlander'), |
| 10 | + ('Chevrolet', 'Impala'), ('Nissan', 'Maxima'), |
| 11 | + ('Ford', 'Mustang'), ('Kia', 'Optima'), |
| 12 | + ('Volkswagen', 'Passat'), ('Nissan', 'Pathfinder'), |
| 13 | + ('Volkswagen', 'Routan'), ('Hyundai', 'Sonata'), |
| 14 | + ('Kia', 'Sorento'), ('Kia', 'Sportage'), |
| 15 | + ('Ford', 'Taurus'), ('Nissan', 'Titan'), |
| 16 | + ('Toyota', 'Tundra'), ('Hyundai', 'Veracruz'), |
| 17 | +] |
| 18 | + |
| 19 | + |
| 20 | +def group_cars_by_manufacturer(cars): |
| 21 | + """Iterate though the list of (manufacturer, model) tuples |
| 22 | + of the cars list defined above and generate the output as described |
| 23 | + in the Bite description (see the tests for the full output). |
| 24 | + |
| 25 | + No return here, just print to the console. We use pytest > capfd to |
| 26 | + validate your output :) |
| 27 | + """ |
| 28 | + cars = sorted(cars, key=lambda x: x[0]) |
| 29 | + for key, group in groupby(cars, lambda x: x[0]): |
| 30 | + cars_text = "\n- ".join(list(map(lambda x: x[1], group))) |
| 31 | + print(key.upper() + "\n- " + cars_text + "\n") |
| 32 | + |
| 33 | + |
| 34 | +# group_cars_by_manufacturer(cars) |
0 commit comments