Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Use Python country) that follows these criteria: i. There needs to be at least 3 levels of hierarchyesting. ii. Each level in the hierarchy needs
Use Python
country) that follows these criteria: i. There needs to be at least 3 levels of hierarchyesting. 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 dietionary 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. locationType: 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 clonedLocation and set it to an empty dictionary. v. Set the clonedLocation's locationType, 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 clonedChild. 2. Check if the clonedLocation is missing its children key and set it to an empty list if so. 3. Append the clonedChild to the clonedLocation's children list. vii. Return the clonedLocation variable. c. In your main code section, below where you've defined your example locations list (i.e. 5a), call your function from 5b, pass in your top level location, and store the return value in a new variable. d. Print the original and cloned locations Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started