isaacpei / algorithm-interview

Weekly algorithm questions for interview
18 stars 0 forks source link

Q001_Goat_solution #8

Open yagggi opened 6 years ago

yagggi commented 6 years ago

Question_ID: Q001

Language: Python2.7

Time Cost: 10-mins

Time Complexity: O(n)

Solution

  1. 正则匹配标签
  2. 对于非结束标签,深度加1,结束标签则深度减1
  3. 递归扫描整个字符串

My Code

# coding: utf-8
import re
import sys

def parse(xml_str, depth=0, pattern=None):

    if not pattern:
        pattern = re.compile('(<\/\w>|<\w>|<\w\/>)')
    res = pattern.search(xml_str)

    if not res:
        return
    start = res.start()
    end = res.end()
    tag = res.groups()[0]
    if start != 0:
        sys.stdout.write(' ' * 4 * depth + xml_str[:start] + '\n')

    sys.stdout.write(' ' * 4 * depth + tag + '\n')

    if '/' not in tag:
        depth += 1
    else:
        depth -= 1
    sub_str = xml_str[end:]

    parse(sub_str, depth=depth, pattern=pattern)

if __name__ == '__main__':
    parse('<a>123<b>456<c/></b></a>')

Other

isaacpei commented 6 years ago

囧, 首先把我觉得正则的pattern不太合适, 如果里面有数字咋整捏 然后把, 这个代码还是有问题的... 先回去改改吧