John Lekberg


JUNTO Practice - "Hackerrank, "Detect HTML Links""

Discussed on 2019-06-11.

Hackerrank, "Detect HTML Links"


Solution - John

from itertools import islice, chain, tee
from sys import stdin
import re

tag_re = re.compile(
    r"<a[ ] .*? </a>", re.VERBOSE
)
href_re = re.compile(
    r'(?<= href=") [^"]+', re.VERBOSE
)
name_re = re.compile(
    r"(?<= >) [^<]+ (?= <)", re.VERBOSE
)

N = int(next(stdin))
lines = islice(stdin, N)
a_tags = chain.from_iterable(
    map(tag_re.findall, lines)
)
a_tags1, a_tags2 = tee(a_tags)
hrefs = map(href_re.search, a_tags1)
names = map(name_re.search, a_tags2)

for href, name in zip(hrefs, names):
    href = href.group(0).strip()
    name = (
        name.group(0).strip()
        if name
        else ""
    )
    print(href, name, sep=",")