Skip to content

Commit b26aa31

Browse files
bite 21
1 parent f5ebb6e commit b26aa31

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@
2121
/165/README.md
2222
/167/README.md
2323
/169/README.md
24+
/172/README.md
25+
/21/README.md

Diff for: 21/cars.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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
12+
(original order)"""
13+
all_jeeps = []
14+
# print(cars['Jeep'][0])
15+
for car in cars['Jeep']:
16+
all_jeeps.append(car)
17+
all_jeeps = ", ".join(all_jeeps)
18+
return all_jeeps
19+
20+
21+
# print(get_all_jeeps(cars))
22+
23+
24+
def get_first_model_each_manufacturer(cars=cars):
25+
"""return a list of matching models (original ordering)"""
26+
first_model = list()
27+
for car in cars.keys():
28+
first_model.append(cars[car][0])
29+
return first_model
30+
31+
# print(get_first_model_each_manufacturer(cars))
32+
33+
34+
def get_all_matching_models(cars=cars, grep='trail'):
35+
"""return a list of all models containing the case insensitive
36+
'grep' string which defaults to 'trail' for this exercise,
37+
sort the resulting sequence alphabetically"""
38+
match = list()
39+
for manufacturer in cars.keys():
40+
for car in cars[manufacturer]:
41+
if grep.lower() in car.lower():
42+
match.append(car)
43+
return sorted(match)
44+
# print(get_all_matching_models(grep='CO'))
45+
46+
47+
def sort_car_models(cars=cars):
48+
"""return a copy of the cars dict with the car models (values)
49+
sorted alphabetically"""
50+
for manufacturer in cars.keys():
51+
cars[manufacturer] = sorted(cars[manufacturer])
52+
return cars
53+
54+
# print(sort_car_models())

Diff for: 21/test_cars.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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_default_grep():
19+
expected = ['Trailblazer', 'Trailhawk']
20+
assert get_all_matching_models() == expected
21+
22+
23+
def test_get_all_matching_models_different_grep():
24+
expected = ['Accord', 'Commodore', 'Falcon']
25+
assert get_all_matching_models(grep='CO') == expected
26+
27+
28+
def test_sort_dict_alphabetically():
29+
actual = sort_car_models()
30+
# Order of keys should not matter, two dicts are equal if they have the
31+
# same keys and the same values.
32+
# The car models (values) need to be sorted here though
33+
expected = {
34+
'Ford': ['Fairlane', 'Falcon', 'Festiva', 'Focus'],
35+
'Holden': ['Barina', 'Captiva', 'Commodore', 'Trailblazer'],
36+
'Honda': ['Accord', 'Civic', 'Jazz', 'Odyssey'],
37+
'Jeep': ['Cherokee', 'Grand Cherokee', 'Trackhawk', 'Trailhawk'],
38+
'Nissan': ['350Z', 'Maxima', 'Navara', 'Pulsar'],
39+
}
40+
assert actual == expected

0 commit comments

Comments
 (0)