习题17:过滤注释

2017-02-22  本文已影响0人  今年说话算话
def solution(string,markers):
    #your code here
    def get_lines(string):
        l = []
        k = 0
        while True:
            t = string.find('\n', k)
            if t >= 0:
                l.append(string[k:t+1])
                k = t + 1
            else:
                l.append(string[k:])
                break
        return l

    lines = get_lines(string)
    r = ''

    for line in lines:
        flag = False
        t = ''
        for e in line:
            if e in markers:
                flag = True
            elif e == '\n':
                t = t.rstrip()
                flag = False
            if not flag:
                t += e
        r += t     
    return r.rstrip()
上一篇 下一篇

猜你喜欢

热点阅读