isaacpei / algorithm-interview

Weekly algorithm questions for interview
18 stars 0 forks source link

Q001_Rokic_solution #11

Open Rokicto opened 6 years ago

Rokicto commented 6 years ago

Question_ID: Q001

Language: Python

Time Cost: 27-mins

Time Complexity: O(n)

Solution & Thoughts

  1. No spaces in xml string, means no attributes will appear in tags.
  2. Line-breaker should appear once and only once before every '<' and after every '>'.
  3. Tags change indents when they appear.

My Code

#!/usr/bin/python
import sys
import re

def format_one_line_xml(xml: str) -> str:
    """One line xml has no space or line break.
    Return formatted xml with 4-space indents.
    """
    lines = []
    indent_count = 0
    xml = re.sub(r'(<[^>]*>)', r'\n\1\n', xml)

    for line in xml.split('\n'):
        if line:
            if line.startswith('</'):
                indent_count -= 1

            lines.append("{}{}".format(' ' * 4 * indent_count, line))

            if line.startswith('<') and not line.endswith('/>') and not line.startswith('</') \
                and not line.startswith('<!'):
                indent_count += 1

    return '\n'.join(lines)

def main():
    """Main function"""
    xml = sys.argv[1]
    print(format_one_line_xml(xml))

if __name__ == '__main__':
    main()

Other

Comments are welcome.

comesx4 commented 6 years ago

新的思路, nice

KIDJourney commented 6 years ago

不能假设输入里没有<, > 吧。

<a>123<b>456<c/>"1>2"</b></a>
Rokicto commented 6 years ago

@KIDJourney 有道理,我改进一下。 另,输入里面应该是没有tag以外的 < 的,参考wiki well-formed document