Tuesday, February 28, 2023
HomeSoftware EngineeringLearn how to Clear up: Assist the Bookseller Problem in Python

Learn how to Clear up: Assist the Bookseller Problem in Python


A bookseller has a number of books categorized in 26 classes labeled A, B, … Z. Every guide has a code c of three, 4, 5 or extra characters. The first character of a code is a capital letter which defines the guide class.

Within the bookseller’s stocklist every code c is adopted by an area and by a optimistic integer n (int n >= 0) which signifies the amount of books of this code in inventory.

For instance an extract of a stocklist could possibly 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 may be given a stocklist (e.g. : L) and an inventory of classes in capital letters e.g :

M = {"A", "B", "C", "W"}
or
M = ["A", "B", "C", "W"] or ...
and your activity is to search out all of the books of L with codes belonging to every class of M and to sum their amount based on every class.

For the lists L and M of instance you must return the string:

(A : 20) - (B : 114) - (C : 50) - (W : 0)
the place A, B, C, W are the classes, 20 is the sum of the distinctive guide of class A, 114 the sum equivalent to “BKWRK” and “BTSQZ”, 50 equivalent to “CDXEF” and 0 to class ‘W’ since there aren’t any code starting with W.

If L or M are empty return string is “”.

Notes:

  • Within the end result codes and their values are in the identical order as in M.
  • See “Samples Assessments” for the return.

The Answer in Python

Possibility 1

def stock_list(listOfArt, listOfCat):
    if (len(listOfArt) == 0) or (len(listOfCat) == 0):
        return ""
    end result = ""
    for cat in listOfCat:
        whole = 0
        for guide in listOfArt:
            if (guide[0] == cat[0]):
                whole += int(guide.cut up(" ")[1])
        if (len(end result) != 0):
            end result += " - "
        end result += "(" + str(cat) + " : " + str(whole) + ")"
    return end result

Possibility 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 ' - '.be part of('({} : {})'.format(cat, cnt[cat]) for cat in listOfCat)

Possibility 3

def stock_list(stocklist, classes):
    if not stocklist or not classes:
        return ""
    return " - ".be part of(
        "({} : {})".format(
            class,
            sum(int(merchandise.cut up()[1]) for merchandise in stocklist if merchandise[0] == class))
        for class in classes)

Check instances to validate the answer

from resolution import stock_list
import check

@check.describe("Testing")
def _():
    @check.it("Assessments")
    def _():
        b = ["BBAR 150", "CDXE 515", "BKWR 250", "BTSQ 890", "DRTY 600"]
        c = ["A", "B", "C", "D"]
        check.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"]
        check.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"]
        check.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"]
        check.assert_equals(stock_list(b, c), "(B : 364) - (R : 225) - (D : 60) - (X : 0)")

        b = []
        c = ["B", "R", "D", "X"]
        check.assert_equals(stock_list(b, c), "")

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

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments