IntermediateStrings25 points
Dissect the tracking URLs
A marketing analyst in Toronto receives hundreds of campaign URLs such as HTTPS://Shop.Example.COM:8080/Men/Shoes?color=Red&size=42&color=blue#reviews and needs them split into readable parts.
- Everything after the first
#is a fragment and is ignored (even a?inside it). - The host is the text between
://and the first/or?, in lower case, with any:portremoved. - The path starts at the first
/after the host and ends before the?; keep its case; if there is none the path is/. - The query is the text after the first
?. It is split on&; empty parts are skipped; each part is split at its first=into key and value (no=means an empty value). Keys and values are percent-decoded and+becomes a space (urllib.parse.unquote_plusdoes exactly this).
Task: line 1 is N; then N URLs, each containing ://. For each URL print host=..., then path=..., then one line per distinct key, sorted by key, as key=v1|v2 where the values of repeated keys are joined with | in order of appearance. After each URL print a line with ---.
Sample tests
Input
2 HTTPS://Shop.Example.COM:8080/Men/Shoes?color=Red&size=42&color=blue#reviews http://example.org
Expected output
host=shop.example.com path=/Men/Shoes color=Red|blue size=42 --- host=example.org path=/ ---
Input
1 https://news.site.pk/search?q=data+analyst%20jobs&city=Lahore&q=sql
Expected output
host=news.site.pk path=/search city=Lahore q=data analyst jobs|sql ---
Your answer is also checked against 2 hidden tests.
Python 3
25 pts
Loading editor...
Log in or create a free account to run your code, submit and earn points.
Run your code to see the output here. Ctrl + Enter also runs it.
