easy +8 pts

Parse URL components

Break a URL into scheme, netloc, path, params, query, and fragment cleanly.

Write a function `parse_url(url: str) -> dict` that parses a URL string and returns a dictionary with keys `'scheme'`, `'netloc'`, `'path'`, `'params'`, `'query'`, and `'fragment'`. For each component, if it is not present in the input, use the empty string `''` as the value. The parsing rules are: 1. **Fragment**: If there is a `#`, everything after the first `#` becomes the `fragment`; otherwise `''`. Remove the fragment from the rest before further parsing. 2. **Query**: If there is a `?` after removing the fragment, everything after the first `?` becomes the `query`; otherwise `''`. Remove the query from the rest. 3. **Scheme**: If the remaining string contains `://`, then the part before the first `://` is the `scheme`, and the part after is the rest. Otherwise `scheme` is `''` and the whole string is the rest. 4. **Netloc**: If there is a `/` after the scheme (or at the very start if no scheme), the part from the beginning up to (but not including) the first `/` is the `netloc`. If there is no `/`, the `netloc` is the entire remaining string. 5. **Path, params**: After removing the netloc, if the remaining starts with `/`, take that as the path. Inside the path, if there is a `;`, the part before the first `;` is the `path` and the part after (up to the end) is the `params`; otherwise `path` is the whole remaining string and `params` is `''`. If the remaining after netloc is empty, then both `path` and `params` are `''`. The function should handle URLs with or without scheme, query, fragment, and params. The returned dictionary should have the keys in this exact order: `scheme`, `netloc`, `path`, `params`, `query`, `fragment`. **Examples**: - `parse_url('https://example.com/path?q=1#frag')` → `{'scheme': 'https', 'netloc': 'example.com', 'path': '/path', 'params': '', 'query': 'q=1', 'fragment': 'frag'}` - `parse_url('example.com/path;param?q=1')` → `{'scheme': '', 'netloc': 'example.com', 'path': '/path', 'params': 'param', 'query': 'q=1', 'fragment': ''}` - `parse_url('/just/path')` → `{'scheme': '', 'netloc': '', 'path': '/just/path', 'params': '', 'query': '', 'fragment': ''}` - `parse_url('https://example.com')` → `{'scheme': 'https', 'netloc': 'example.com', 'path': '', 'params': '', 'query': '', 'fragment': ''}`

Constraints

Input `url` is a non-empty string of length at most 200. It may contain letters, digits, ':', '/', '?', '#', ';', '.', and other typical URL characters. No spaces. Time complexity: O(n), space O(n).

Example

>>> parse_url('https://example.com/path?q=1#frag')
{'scheme': 'https', 'netloc': 'example.com', 'path': '/path', 'params': '', 'query': 'q=1', 'fragment': 'frag'}
>>> parse_url('example.com/path;param?q=1')
{'scheme': '', 'netloc': 'example.com', 'path': '/path', 'params': 'param', 'query': 'q=1', 'fragment': ''}
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Process the string step by step: strip fragment first, then query, then scheme/netloc/path.
Use the split method with maxsplit=1 to separate components.
Remember that when there is no scheme, the netloc is everything before the first '/' or the whole string if no slash.
Check edge cases like 'example.com' (no slash) and '/just/path' (no scheme).
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.