# The right way to Resolve: Lend a hand the Bookseller Problem in Python

A bookseller has plenty of books categorised in 26 classes categorized A, B, &mldr; Z. Each and every e book has a code c of three, 4, 5 or extra characters. The first personality of a code is a capital letter which defines the e book class.

Within the booksellerâs stocklist every code c is adopted through an area and through a favorable integer n (int n >= 0) which signifies the volume of books of this code in inventory.

For instance an extract of a stocklist may well 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 are going to be given a stocklist (e.g. : L) and a listing of classes in capital letters e.g :

`M = {"A", "B", "C", "W"}`
or
`M = ["A", "B", "C", "W"] or ...`
and your activity is to seek out the entire books of L with codes belonging to every class of M and to sum their amount in line with every class.

For the lists L and M of instance you need to go back the string:

`(A : 20) - (B : 114) - (C : 50) - (W : 0)`
the place A, B, C, W are the types, 20 is the sum of the original e book of class A, 114 the sum similar to âBKWRKâ and âBTSQZâ, 50 similar to âCDXEFâ and nil to class âWâ since there aren’t any code starting with W.

If `L` or `M` are empty go back string is ââ.

Notes:

• Within the consequence codes and their values are in the similar order as in M.
• See âSamples Examsâ for the go back.

## The Resolution in Python#

### Possibility 1#

``````def stock_list(listOfArt, listOfCat):
if (len(listOfArt) == 0) or (len(listOfCat) == 0):
go back ""
consequence = ""
for cat in listOfCat:
overall = 0
for e book in listOfArt:
if (e book == cat):
overall += int(e book.break up(" "))
if (len(consequence) != 0):
consequence += " - "
consequence += "(" + str(cat) + " : " + str(overall) + ")"
go back consequence
``````

### Possibility 2#

``````from collections import Counter

def stock_list(listOfArt, listOfCat):
if now not listOfArt:
go back ''
codePos = listOfArt.index(' ') + 1
cnt = Counter()
for s in listOfArt:
cnt[s] += int(s[codePos:])
go back ' - '.sign up for('({} : {})'.layout(cat, cnt[cat]) for cat in listOfCat)
``````

### Possibility 3#

``````def stock_list(stocklist, classes):
if now not stocklist or now not classes:
go back ""
"({} : {})".layout(
class,
sum(int(merchandise.break up()) for merchandise in stocklist if merchandise == class))
for class in classes)
``````

## Check instances to validate the answer#

``````from answer import stock_list
import take a look at

@take a look at.describe("Trying out")
def _():
@take a look at.it("Exams")
def _():
b = ["BBAR 150", "CDXE 515", "BKWR 250", "BTSQ 890", "DRTY 600"]
c = ["A", "B", "C", "D"]
take a look at.assert_equals(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"]
take a look at.assert_equals(stock_list(b, c), "(A : 200) - (B : 1140)")

b = ["CBART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60"]
c = ["A", "B", "C", "W"]
take a look at.assert_equals(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"]
take a look at.assert_equals(stock_list(b, c), "(B : 364) - (R : 225) - (D : 60) - (X : 0)")

b = []
c = ["B", "R", "D", "X"]
take a look at.assert_equals(stock_list(b, c), "")

b = ["ROXANNE 102", "RHODODE 123", "BKWRKAA 125", "BTSQZFG 239", "DRTYMKH 060"]
c = []
take a look at.assert_equals(stock_list(b, c), "")

b = ["ROXANNE 102", "RHODODE 123", "BKWRKAA 125", "BTSQZFG 239", "DRTYMKH 060"]
c = ["U", "V", "R"]
take a look at.assert_equals(stock_list(b, c), "(U : 0) - (V : 0) - (R : 225)")
``````