首页 > Web开发 > 详细

HackerRank - "Detect HTML Attributes"

时间:2015-06-03 06:08:01      阅读:307      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!