-1

def babylonian(symbols):

    table=[["T",1], ["<",10],["\\",0]]
    returning=0
    for pair in table:

    forTrue=True

    while forTrue:
        if len(symbols)>=len(pair[0]):

            if symbols[0:len(pair[0])]==pair[0]:
                returning+=pair[1]
                symbols=symbols[len(pair[0]):]

            else:
                forTrue=False
        else:
            forTrue=False

return returning

你好,我该怎么做才能得到类似这样的输出:

print(babylonian(['TT', '<<']))
# should output [2,10]

print(babylonian(['<<T', 'TTT', '//', '<<<<TTT']))
# should output [21,3,0,43]

目前我只能输出表格中的数字,如果我尝试堆叠 ex。TT<<我得到的输出为 0。

4

1 回答 1

0

我试图让它变得简单,这是我的第一次尝试:

import collections

def babylonian(symbols):
    table=[{"T":1}, {"<":10},{"\\":0}]
    socre = []
    totle = 0
    ld = [collections.Counter(each) for each in symbols]
    for d in ld:
        for k,v in d.items():
            for t in table:
                totle += t.get(k) * v if t.get(k) else 0
        socre.append(totle)
        totle = 0
    return socre
print(babylonian(["<<T", "TTT","//", "<<<<TTT"]))
于 2018-09-23T07:36:26.960 回答