Day 8 of 100 - Data Structures Day 2 of 3

Data Structures Day 2 of 3

Day 8 of 100

These were fun exercises. I only really know a few conditional statements and am just learning how to work with lists and dictionaries so my solutions to the challenges are not elegant but they work and coming up with the answers was fun. The challenge for today was to write a bunch of functions for a dictionary of car manufactures and makes:

cars.py

cars = {
    'Ford': ['Falcon', 'Focus', 'Festiva', 'Fairlane'],
    'Holden': ['Commodore', 'Captiva', 'Barina', 'Trailblazer'],
    'Nissan': ['Maxima', 'Pulsar', '350Z', 'Navara'],
    'Honda': ['Civic', 'Accord', 'Odyssey', 'Jazz'],
    'Jeep': ['Grand Cherokee', 'Cherokee', 'Trailhawk', 'Trackhawk']
}

def get_all_jeeps(cars=cars):
    """return a comma  + space (', ') separated string of jeep models
       (original order)"""
    jeeps = ''
    for model in cars.get('Jeep'):
        if len(jeeps) == 0:
            jeeps = model
        else:
            jeeps = jeeps + f', {model}'
    return jeeps


def get_first_model_each_manufacturer(cars=cars):
    """return a list of matching models (original ordering)"""
    models = []
    for keys, values in cars.items():
        models.append(values[0])
    return models


def get_all_matching_models(cars=cars, grep='trail'):
    """return a list of all models containing the case insensitive
       'grep' string which defaults to 'trail' for this exercise,
       sort the resulting sequence alphabetically"""
    trail_list = []
    for values in cars.values():
        for value in values:
            if grep.lower() in value.lower():
                trail_list.append(value)
    return sorted(trail_list)


def sort_car_models(cars=cars):
    """return a copy of the cars dict with the car models (values)
       sorted alphabetically"""
    model_list = {}
    for keys, values in cars.items():
        model_list[keys] = sorted(values)
    return model_list

One of the nice things about Pybites challenges are the tests they provide. I understand what the tests are doing ut I don't know much about writing them. Here is the pytest code provided to check the work on this challenge:

test_cars.py

from cars import (get_all_jeeps, get_first_model_each_manufacturer,
                  get_all_matching_models, sort_car_models)


def test_get_all_jeeps():
    expected = 'Grand Cherokee, Cherokee, Trailhawk, Trackhawk'
    actual = get_all_jeeps()
    assert type(actual) == str
    assert actual == expected


def test_get_first_model_each_manufacturer():
    actual = get_first_model_each_manufacturer()
    expected = ['Falcon', 'Commodore', 'Maxima', 'Civic', 'Grand Cherokee']
    assert actual == expected


def test_get_all_matching_models_default_grep():
    expected = ['Trailblazer', 'Trailhawk']
    assert get_all_matching_models() == expected


def test_get_all_matching_models_different_grep():
    expected = ['Accord', 'Commodore', 'Falcon']
    assert get_all_matching_models(grep='CO') == expected


def test_sort_dict_alphabetically():
    actual = sort_car_models()
    # Order of keys should not matter, two dicts are equal if they have the
    # same keys and the same values.
    # The car models (values) need to be sorted here though
    expected = {
        'Ford': ['Fairlane', 'Falcon', 'Festiva', 'Focus'],
        'Holden': ['Barina', 'Captiva', 'Commodore', 'Trailblazer'],
        'Honda': ['Accord', 'Civic', 'Jazz', 'Odyssey'],
        'Jeep': ['Cherokee', 'Grand Cherokee', 'Trackhawk', 'Trailhawk'],
        'Nissan': ['350Z', 'Maxima', 'Navara', 'Pulsar'],
    }
    assert actual == expected