forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomain_extractor.py
More file actions
50 lines (36 loc) · 1.17 KB
/
domain_extractor.py
File metadata and controls
50 lines (36 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Domain Name Extractor
Given a URL as a string, parse out just the domain name and return it.
Uses only the .split() built-in function without regex or urlparse.
Reference: https://en.wikipedia.org/wiki/Domain_name
Complexity:
Time: O(n) where n is the length of the URL
Space: O(n)
"""
from __future__ import annotations
def domain_name_1(url: str) -> str:
"""Extract the domain name from a URL by splitting on protocol and dots.
Args:
url: The full URL string.
Returns:
The domain name extracted from the URL.
Examples:
>>> domain_name_1("https://github.com/SaadBenn")
'github'
"""
full_domain_name = url.split("//")[-1]
actual_domain = full_domain_name.split(".")
if len(actual_domain) > 2:
return actual_domain[1]
return actual_domain[0]
def domain_name_2(url: str) -> str:
"""Extract the domain name from a URL using chained splits.
Args:
url: The full URL string.
Returns:
The domain name extracted from the URL.
Examples:
>>> domain_name_2("http://google.com")
'google'
"""
return url.split("//")[-1].split("www.")[-1].split(".")[0]