URL Parts
Programming Interview
Medium
8 views
Problem Description
One URL string like domain/path is provided (no spaces). Create URL class with domain() and path() methods. If no '/', path is EMPTY.
Input Format
One line url.
Output Format
Two lines: domain then path.
Sample Test Case
Input:
example.com/home/page
Output:
example.com
/home/page
Official Solution
import sys
s=sys.stdin.read().strip()
if not s: sys.exit(0)
class URL:
def __init__(self,u):
self.u=u
def domain(self):
i=self.u.find('/')
return self.u if i==-1 else self.u[:i]
def path(self):
i=self.u.find('/')
return 'EMPTY' if i==-1 else self.u[i:]
u=URL(s)
print(u.domain())
print(u.path())
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!