loading...

re: Daily Challenge #137 - Help the Bookseller VIEW POST

FULL DISCUSSION
 

Python, with a quick pytest:

from collections import defaultdict

books = ["ABART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60"]

stocks = ["A", "B", "C", "W"]


def do_137(book_list: list, stock_list: list) -> dict:
    d = defaultdict(int)
    for book in book_list:
        for stock in stock_list:
            if book.startswith(stock):
                d[stock] += int(book.split(" ")[-1])
        else:
            d[stock] = 0
    return d

Test:

from dev137 import do_137


def test_137():
    l = ["ABART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60"]

    m = ["A", "B", "C", "W"]

    result = {"A": 20, "B": 114, "C": 50, "W": 0}

    assert dict(do_137(l, m)) == result
code of conduct - report abuse