A tiny DNS zone is given as two dicts:
cnamesmaps an alias to another name:"www.shop.com" -> "shop.com".addressesmaps a name to its IP address:"shop.com" -> "10.0.0.7".
No name appears in both dicts.
Implement resolve(name, cnames, addresses) -> str: follow aliases starting from name until you reach a name that has an address, and return that address.
- If a name on the way has neither an address nor an alias, raise
KeyError. - If the chain comes back to a name it already visited, raise
ValueError(a CNAME loop; without detection you'd spin forever).
cnames = {"www.shop.com": "shop.com", "shop.com": "lb.shop.net", "a": "b", "b": "a"}
addresses = {"lb.shop.net": "10.0.0.7"}
resolve("www.shop.com", cnames, addresses) # "10.0.0.7"
resolve("lb.shop.net", cnames, addresses) # "10.0.0.7" (no alias to follow)
resolve("nowhere.org", cnames, addresses) # KeyError
resolve("a", cnames, addresses) # ValueError: a -> b -> a
Don't modify the dicts you're given.
Show hint
walk the chain in a loop with a seen set: return as soon as the current name is in addresses, raise if it's in seen, otherwise add it and move to cnames[name].