You've successfully subscribed to The Poor Coder | Hackerrank Solutions
Great! Next, complete checkout for full access to The Poor Coder | Hackerrank Solutions
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
HackerRank - Detect the Domain Name Solution

HackerRank - Detect the Domain Name Solution

Using this solution you can extract all the urls from a html page

Beeze Aal
Beeze Aal

You will be provided with a chunk of HTML markup. Your task is to identify the unique domain names from the links or Urls which are present in the markup fragment.

For example, if the link is present in the markup, you should detect the domain: In case there are second level or higher level domains present in the markup, all of them need to be treated as unique. For instance if the links,, are present in the markup then [,,] should all be identified as unique domains present in the markup. Prefixes like "www." and "ww2.", if present, should be scrubbed out from the domain name.

Input Format

An Integer N. This is equal to the number of lines in the HTML Fragment which follows. A chunk of HTML Markup with embedded links, the length of which is N lines.

Output Format

One line, containing the list of detected domains, separated by semi-colons, in lexicographical order. Do not leave any leading or trailing spaces either at the ends of the line, or before and after the individual domain names.

Sample Input

<div class="reflist" style="list-style-type: decimal;">
<ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b>^ ["Train (noun)"]( <i>(definition – Compact OED)</i>. Oxford University Press<span class="reference-accessdate">. Retrieved 2008-03-18</span>.</span><span title="ctx_ver=Z39.88-2004&" class="Z3988"><span style="display:none;"> </span></span></span></li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b>^</b></span> <span class="reference-text"><span class="citation book">Atchison, Topeka and Santa Fe Railway (1948). <i>Rules: Operating Department</i>. p. 7.</span><span title="ctx_ver=Z39.88-2004&" class="Z3988"><span style="display:none;"> </span></span></span></li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b>^ [Hydrogen trains](</span></li>
<li id="cite_note-4"><span class="mw-cite-backlink"><b>^ [Vehicle Projects Inc. Fuel cell locomotive](</span></li>
<li id="cite_note-5"><span class="mw-cite-backlink"><b>^</b></span> <span class="reference-text"><span class="citation book">Central Japan Railway (2006). <i>Central Japan Railway Data Book 2006</i>. p. 16.</span><span title="ctx_ver=Z39.88-2004&" class="Z3988"><span style="display:none;"> </span></span></span></li>
<li id="cite_note-6"><span class="mw-cite-backlink"><b>^ ["Overview Of the existing Mumbai Suburban Railway"]( _Official webpage of Mumbai Railway Vikas Corporation_. Archived from [the original]( on 2008-06-20<span class="reference-accessdate">. Retrieved 2008-12-11</span>.</span><span title="ctx_ver=Z39.88-2004&" class="Z3988"><span style="display:none;"> </span></span></span></li>

Sample Output;;;;

Solution in Python

import sys, re

#read user input
text =
#pattern for matching both http and https 
regex = r"https?://[.\w-]*\.*[.\w-]+\.[a-z0-9]+"
#grab all domains
domains = re.findall(regex, text)
#remove http:// or https:// from the beginning of each domain
domains = [re.sub(r"\bhttps?://", "", domain) for domain in domains]
#remove www. from the beginning of each domain
domains = [re.sub(r"\bwww.", "", domain) for domain in domains]
#remove non unique domains
domains = set(domains)
#convert from set to list
domains = list(domains)
#sort in alphabetical order