Python古代数字到num

2024-06-28 19:41:16 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: falseoutputleniftableelsereturningprint
1条回答
网友
1楼 · 发布于 2024-06-28 19:41:16

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

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"]))

相关问题 更多 >