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
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Python, with a quick pytest:
Test: