Skip to content

Commit d186e57

Browse files
committed
Completed bite 21
1 parent ac721e7 commit d186e57

File tree

1 file changed

+58
-0
lines changed
  • days/07-09-data-structures/code/bite21

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'''
2+
From: https://codechalleng.es/bites/21/#console
3+
Given the provided cars dictionary:
4+
5+
Get all Jeeps
6+
Get the first car of every manufacturer.
7+
Get all vehicles containing the string Trail in their names (should work for other grep too)
8+
Sort the models (values) alphabetically
9+
See the docstrings and tests for more details. Have fun!
10+
11+
Update 18th of Sept 2018: as concluded in the forum it is better to pass the cars dict into each function to make its scope local.
12+
'''
13+
14+
cars = {
15+
'Ford': ['Falcon', 'Focus', 'Festiva', 'Fairlane'],
16+
'Holden': ['Commodore', 'Captiva', 'Barina', 'Trailblazer'],
17+
'Nissan': ['Maxima', 'Pulsar', '350Z', 'Navara'],
18+
'Honda': ['Civic', 'Accord', 'Odyssey', 'Jazz'],
19+
'Jeep': ['Grand Cherokee', 'Cherokee', 'Trailhawk', 'Trackhawk']
20+
}
21+
22+
23+
def get_all_jeeps(cars=cars):
24+
"""return a comma + space (', ') separated string of jeep models
25+
(original order)"""
26+
27+
return ', '.join([', '.join(models) for vendor, models in cars.items() if vendor == 'Jeep'])
28+
29+
30+
def get_first_model_each_manufacturer(cars=cars):
31+
"""return a list of matching models (original ordering)"""
32+
33+
return [models[0] for vendor, models in cars.items()]
34+
35+
36+
def get_all_matching_models(cars=cars, grep='trail'):
37+
"""return a list of all models containing the case insensitive
38+
'grep' string which defaults to 'trail' for this exercise,
39+
sort the resulting sequence alphabetically"""
40+
41+
return sorted([model for vendor, models in cars.items() for model in models if grep.lower() in model.lower()])
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+
48+
for vendor, models in cars.items():
49+
models = sorted(models)
50+
cars[vendor] = models
51+
return cars
52+
53+
54+
if __name__ == '__main__':
55+
print(get_all_jeeps(cars))
56+
print(get_first_model_each_manufacturer(cars))
57+
print(get_all_matching_models(cars, grep='CO'))
58+
print(sort_car_models(cars))

0 commit comments

Comments
 (0)