A bookseller has great deals of books categorized in 26 classifications identified A, B, && mldr; Z. Each book has a code c of 3, 4, 5 or more characters. The first character of a code is an uppercase which specifies the book classification.
In the bookseller’s stocklist each code c is followed by an area and by a favorable integer n (int n >>= 0) which suggests the amount of books of this code in stock.
For instance an extract of a stocklist might be:
L = {"ABART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60"}.
or.
L = ["ABART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60"] or ...
You will be offered a stocklist (e.g.: L) and a list of classifications in uppercase e.g:
M = {"A", "B", "C", "W"}
or.
M = ["A", "B", "C", "W"] or ...
and your job is to discover all the books of L with codes coming from each classification of M and to sum their amount according to each classification.
For the lists L and M of example you need to return the string:
( A: 20) - (B: 114) - (C: 50) - (W: 0)
where A, B, C, W are the classifications, 20 is the amount of the special book of classification A, 114 the amount representing “BKWRK” and “BTSQZ”, 50 representing “CDXEF” and 0 to classification ‘W’ because there are no code starting with W.
If L
or M
are empty return string is “”.
Notes:
- In the outcome codes and their worths remain in the exact same order as in M.
- See “Samples Tests” for the return.
The Service in Python
Alternative 1
def stock_list( listOfArt, listOfCat):.
if (len( listOfArt) == 0) or (len( listOfCat) == 0):.
return "".
outcome="".
for feline in listOfCat:.
overall = 0.
for book in listOfArt:.
if (book[0] == feline[0]):.
overall += int( book.split(" ")[1]).
if (len( outcome)!= 0):.
outcome +=" - ".
outcome += "(" + str( feline) + ":" + str( overall) + ")".
return outcome.
Alternative 2
from collections import Counter.
def stock_list( listOfArt, listOfCat):.
if not listOfArt:.
return ".
codePos = listOfArt[0] index(' ') + 1.
cnt = Counter().
for s in listOfArt:.
cnt[s[0]] += int( s[codePos:]).
return' - '. sign up with('( {}: {} )'. format( feline, cnt[cat]) for feline in listOfCat).
Alternative 3
def stock_list( stocklist, classifications):.
if not stocklist or not classifications:.
return "".
return" - ". sign up with(.
"( {}: {} )". format(.
classification,.
amount( int( item.split()[1]) for product in stocklist if product[0] == classification)).
for classification in classifications).
Test cases to confirm the service
from service import stock_list.
import test.
@test. explain(" Screening").
def _():.
@test. it(" Tests").
def _():.
b =["BBAR 150", "CDXE 515", "BKWR 250", "BTSQ 890", "DRTY 600"]
c =["A", "B", "C", "D"]
test.assert _ equates to( stock_list( b, c), "( A: 0) - (B: 1290) - (C: 515) - (D: 600)").
b =["ABAR 200", "CDXE 500", "BKWR 250", "BTSQ 890", "DRTY 600"]
c =["A", "B"]
test.assert _ equates to( stock_list( b, c), "( A: 200) - (B: 1140)").
b =["CBART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60"]
c =["A", "B", "C", "W"]
test.assert _ equates to( stock_list( b, c), "( A: 0) - (B: 114) - (C: 70) - (W: 0)").
b =["ROXANNE 102", "RHODODE 123", "BKWRKAA 125", "BTSQZFG 239", "DRTYMKH 060"]
c =["B", "R", "D", "X"]
test.assert _ equates to( stock_list( b, c), "( B: 364) - (R: 225) - (D: 60) - (X: 0)").
b =[]
c =["B", "R", "D", "X"]
test.assert _ equates to( stock_list( b, c), "").
b =["ROXANNE 102", "RHODODE 123", "BKWRKAA 125", "BTSQZFG 239", "DRTYMKH 060"]
c =[]
test.assert _ equates to( stock_list( b, c), "").
b =["ROXANNE 102", "RHODODE 123", "BKWRKAA 125", "BTSQZFG 239", "DRTYMKH 060"]
c =["U", "V", "R"]
test.assert _ equates to( stock_list( b, c), "( U: 0) - (V: 0) - (R: 225)").