Skip to content

Commit d12bf80

Browse files
committed
new files in days 07-09
2 parents 922f754 + c8f281e commit d12bf80

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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():
11+
jeeps = cars['Jeep']
12+
models = ', '.join(jeeps)
13+
return models
14+
15+
16+
def get_first_model_each_manufacturer():
17+
firstmodels = []
18+
for maker in cars:
19+
models = cars[maker]
20+
firstmodels.append(models[0])
21+
return firstmodels
22+
23+
24+
def get_all_matching_models(grep='trail'):
25+
search_result = []
26+
for models in cars.values():
27+
for model in models:
28+
if grep.lower() in model.lower():
29+
search_result.append(model)
30+
search_result.sort()
31+
return search_result
32+
33+
34+
def sort_car_models():
35+
new_cars = {}
36+
for maker in cars:
37+
models = cars[maker]
38+
models.sort()
39+
new_cars[maker] = models
40+
return new_cars
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from cars import (get_all_jeeps, get_first_model_each_manufacturer,
2+
get_all_matching_models, sort_car_models)
3+
4+
5+
def test_get_all_jeeps():
6+
expected = 'Grand Cherokee, Cherokee, Trailhawk, Trackhawk'
7+
actual = get_all_jeeps()
8+
assert type(actual) == str
9+
assert actual == expected
10+
11+
12+
def test_get_first_model_each_manufacturer():
13+
actual = get_first_model_each_manufacturer()
14+
expected = ['Falcon', 'Commodore', 'Maxima', 'Civic', 'Grand Cherokee']
15+
assert actual == expected
16+
17+
18+
def test_get_all_matching_models():
19+
expected = ['Trailblazer', 'Trailhawk']
20+
assert get_all_matching_models() == expected
21+
expected = ['Accord', 'Commodore', 'Falcon']
22+
assert get_all_matching_models(grep='CO') == expected
23+
24+
25+
def test_sort_dict_alphabetically():
26+
actual = sort_car_models()
27+
# Order of keys should not matter, two dicts are equal if they have the
28+
# same keys and the same values.
29+
# The car models (values) need to be sorted here though
30+
expected = {
31+
'Ford': ['Fairlane', 'Falcon', 'Festiva', 'Focus'],
32+
'Holden': ['Barina', 'Captiva', 'Commodore', 'Trailblazer'],
33+
'Honda': ['Accord', 'Civic', 'Jazz', 'Odyssey'],
34+
'Jeep': ['Cherokee', 'Grand Cherokee', 'Trackhawk', 'Trailhawk'],
35+
'Nissan': ['350Z', 'Maxima', 'Navara', 'Pulsar'],
36+
}
37+
assert actual == expected

0 commit comments

Comments
 (0)