Question: a. Create a nested hierarchy of dictionaries that represent locations on a map (address, city, state/region, country) that follows these criteria: < i. There
a. Create a nested hierarchy of dictionaries that represent locations on a map (address, city, state/region, country) that follows these criteria: < i. There needs to be at least 3 levels of hierarchy/nesting. < ii. Each level in the hierarchy needs to have at least 3 locations (i.e. 1. country with 3-regions, each of those regions with 3 cities, etc.). So you'll have a structure consisting of 1 level 1 dictionary, 3 level 2 dictionaries, 9 level 3 dictionaries, and so on. < iii. Each dictionary representing a level needs to have the following key/value pairs: < 1.name: The name of the location (i.e. city name for a city location). < 2.location Type: The type of location (i.e. city, state, province, etc.) 3.population: The number of people at the location. < 4. children: A list to hold the nested locations. This should be an empty list for the lowest level dictionaries. b. Create a function at the top of your code that has the following features: < i. This function will perform a deep copy of a location, so name it accordingly. < ii. It must have an appropriate docstring. < iii. This function will accept a parameter for the location dictionary to copy. < iv. Create a new variable named cloned Location and set it to an empty dictionary. < v. Set the cloned Location's location Type, name, and population keys to the parameter's corresponding value (i.e. copy the values from the original to the clone). < vi. Loop through the parameter's children and for each iteration of the loop: < 1.Recursively call this same function, but pass in the current child as the argument. Store the return value of the recursive function call to a variable named cloned Child. < 2. Check if the cloned Location is missing its children key and set it to an empty list if so. < 3.Append the clonedChild to the cloned Location's children list. < v vii. Return the cloned Location variable.
Step by Step Solution
There are 3 Steps involved in it
Heres a Python code example that creates a nested hierarchy of dictionaries representing locations on a map and includes a function for deep copying a ... View full answer
Get step-by-step solutions from verified subject matter experts
