讨论/面试考题/请教一下大佬这道题怎么做。/
请教一下大佬这道题怎么做。

1.png

展开讨论
辉辉发起于 2020-03-02
最近编辑于 2020-03-02

华为笔试第二题

我用的方法是纯状态判断。生生写了30分钟,因为样例盲盒,最后一个实例太难过。。

最后测出最后一个实例应该是类似于:

aas,"fasd""",""

所以要对双引号包裹空字符串好好判断。

import sys

while True:
    a = sys.stdin.readline().strip()
    if not a:
        break
    if a == "\"\"":
        print(1,'--')
        continue
    section = []

    ded = False
    temp = ""
    i = 0
    good = True
    while i < len(a):
        if a[i] == ',':
            if ded:
                temp += ','
            else:
                section.append(temp)
                temp = ''
            i+=1
            continue
        if a[i] == '"':
            if ded:
                if i+1<len(a) and a[i+1] == '"':
                    i+=1
                    temp +='"'
                elif i+1 == len(a):
                    ded = False
                elif i+1 < len(a) and a[i+1] == ',':
                    ded = False
                else:
                    good = False
                    break
            else:
                if i != 0 and a[i-1] != ',':
                    good = False
                    break
                ded = True
            i+=1
            continue
        temp += a[i]
        i+=1

    if ded:
        good = False
    if good:
        if temp != '':
            section.append(temp)
        if a[-1] == ',':
            section.append('')
        print(len(section))
        for i in section:
            if i == '':
                print("--")
            else:
                print(i)
    else:
        print("ERROR")

展开全部 3 讨论