#108 · Parsing · Intermediate · 85 XP · about 18 min
The part of a web address after the ? carries parameters, like q=python+books&page=2. Write parse_query(query) that turns such a string into a dictionary from keys to values:
&. Each pair is split at its first = into a key and a value, and a pair with no = gets the value ''. Empty pairs (from && or a trailing &) are skipped.+ stands for a space, and % followed by two hex digits (in either case) is the character with that code: %20 is a space, %2B is + and %3D is =. Turn the + signs into spaces before decoding the % codes, so that %2B ends up as a real plus.parse_query("q=python+books&page=2") # returns {'q': 'python books', 'page': '2'} parse_query("tag=a&tag=b&sort=new&tag=c") # returns {'tag': ['a', 'b', 'c'], 'sort': 'new'} parse_query("debug&name=Ada%20Lovelace&sum=1%2B1%3D2") # returns {'debug': '', 'name': 'Ada Lovelace', 'sum': '1+1=2'}
query has 0 to 10,000 characters and does not include the ?.% is followed by two hex digits between 00 and 7F (a plain ASCII character).Ready?
Accept the challenge to start writing. A small timer counts the time this page is in view.
Output appears here.