码迷,mamicode.com
首页 > Web开发 > 详细

HackerRank - "Detect HTML Attributes"

时间:2015-06-03 06:09:44      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

Really fun regex one.

import re

n = int(input())
txt = ‘‘
for _ in range(n):
    str = input()
    txt = txt + str

dict = {}
all = re.findall(<[^/][^<>]*>, txt)
for i in range(len(all)):
    segs = re.split( , all[i][1:-1])    
    if not segs[0] in dict:
        dict[segs[0]] = set([])
    if (len(segs) > 1):
        tags = re.findall("\s[a-z]+=", all[i][1:-1])
        for j in range(len(tags)):        
            dict[segs[0]].add(tags[j][1:-1])        

sortedKeys = list(dict.keys())
sortedKeys.sort()
for k in sortedKeys:
    print(k + :, end="")
    tags = list(dict[k])
    tags.sort()
    print (,.join(tags))

HackerRank - "Detect HTML Attributes"

标签:

原文地址:http://www.cnblogs.com/tonix/p/4548181.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!