1. 程式人生 > >入坑codewars第八天-Help the bookseller !

入坑codewars第八天-Help the bookseller !

今天只寫了一道題不容易呀,實在不懂輸出格式,於是想辦法湊出來的……

題目:

A bookseller has lots of books classified in 26 categories labeled A, B, ... Z. Each book has a code c of 3, 4, 5 or more capitals letters. The 1st letter of a code is the capital letter of the book category. In the bookseller's stocklist each code c is followed by a space and by a positive integer n (int n >= 0) which indicates the quantity of books of this code in stock.

For example an extract of one of the stocklists could 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 given a stocklist (e.g. : L) and a list of categories in capital letters e.g :

M = {"A", "B", "C", "W"}

or

M = ["A", "B", "C", "W"] or ...

and your task is to find all the books of L with codes belonging to each category of M and to sum their quantity according to each category.

For the lists L and M of example you have to return the string (in Haskell/Clojure a list of pairs):

(A : 20) - (B : 114) - (C : 50) - (W : 0)

where A, B, C, W are the categories, 20 is the sum of the unique book of category A, 114 the sum corresponding to "BKWRK" and "BTSQZ", 50 corresponding to "CDXEF" and 0 to category 'W' since there are no code beginning with W.

If L or M are empty return string is "" (Clojure should return an empty array instead).

題意就是:
輸入:
b = ["ABAR 200", "CDXE 500", "BKWR 250", "BTSQ 890", "DRTY 600"]
c = ["A", "B"]
輸入b代表書類別,比如ABAR是看首字母,首字母是A代表A類,後面跟著數字代表此類的藏書本數;
輸入c代表需要計算這些類的本數,比如這裡是A、B類因此在b中找A、B類的分別計數;

解題思路:

我的思路是:
(1)用字典儲存,類別+本數;比如A類作為鍵,本數作為它的值;
(2)然後就遍歷,如果是c中的鍵則它的值就加上b中字串後面的數;這裡的數用了正則表示式分割處理,具體可參考程式碼
(3)當都為零時則輸出空字串
(4)為了和輸出一樣我用了字串連線處理

程式碼如下:

import re
def stock_list(listOfArt, listOfCat):
    dict = {}
    string1 = ""
    for x in listOfCat:
        dict[x] = 0
    for x in listOfCat:
        for y in listOfArt:
            if y[0] == x:
                a = int(re.sub("\D","",y))
                dict[x]=dict[x]+a
        string1=string1+"("+x+" : "+str(dict[x])+")"+" - "
    if sum(dict.values()) == 0:
        return "";
    string1=string1[:-3]
    return string1

上幾個大神程式碼吧:

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

 

def stock_list(listOfArt, listOfCat):
    if (len(listOfArt) == 0) or (len(listOfCat) == 0):
        return ""
    result = ""
    for cat in listOfCat:
        total = 0
        for book in listOfArt:
            if (book[0] == cat[0]):
                total += int(book.split(" ")[1])
        if (len(result) != 0):
            result += " - "
        result += "(" + str(cat) + " : " + str(total) + ")"
    return result
def stock_list(stocklist, categories):
    if not stocklist or not categories:
        return ""
    return " - ".join(
        "({} : {})".format(
            category,
            sum(int(item.split()[1]) for item in stocklist if item[0] == category))
        for category in categories)