Python古代数字到数字

时间:2018-09-23 00:48:39

标签: python numeral-system

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。

1 个答案:

答案 0 :(得分: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"]))