easy +10 pts

Protocol definition

Parse a simple wire protocol and return a structured dictionary.

You are implementing a decoder for a simple text-based protocol. Each message consists of one or more fields separated by ';'. Each field is a key-value pair where key and value are separated by '='. Keys are alphanumeric (letters and digits) and values may contain any characters except ';' and '='. For example: 'name=Alice;role=admin;id=42'. Write a function `parse_protocol(s: str) -> dict` that takes a string `s` and returns a dictionary with the parsed fields. Rules: - If `s` is an empty string, return an empty dictionary `{}`. - If `s` ends with a semicolon, ignore that trailing semicolon. - Keys must be unique; if a duplicate key appears, the last value wins. - If a field is malformed (does not contain exactly one '=' or the key is empty), skip that field entirely. Implement the function exactly as specified.

Constraints

- `s` is a string of length between 0 and 1000. - Keys consist of alphanumeric characters (a-z, A-Z, 0-9) and are case-sensitive. - Values are non-empty and may contain any printable ASCII except ';' and '='. - Number of fields is between 0 and 50. Your solution should run in O(n) time, where n is the length of the string.

Example

>>> parse_protocol('name=Alice;role=admin;id=42')
{'name': 'Alice', 'role': 'admin', 'id': '42'}
>>> parse_protocol('')
{}
>>> parse_protocol('a=1;b=2;')
{'a': '1', 'b': '2'}
>>> parse_protocol('x=1;x=2')
{'x': '2'}
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Split the string by ';' and handle the trailing semicolon case.
For each part, check that exactly one '=' exists and the key is not empty.
Use a dictionary and assign by key, so later duplicates overwrite earlier ones.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.