3.Write a program to implement Depth Limit Search
def depth_limit_search(array, depth_limit):
def dls_helper(arr, current_depth):
if current_depth > depth_limit:
return
for element in arr:
if isinstance(element, list):
print(f"At depth {current_depth}: Encountered nested list, diving deeper...")
dls_helper(element, current_depth + 1)
else:
print(f"At depth {current_depth}: Processing element: {element}")
# Start the depth-limited search with the initial depth of 0
dls_helper(array, 0)
# Example usage
nested_array = [1, [2, 3], [4, [5, 6]], 7, [8, [9, [10, 11]]]]
depth_limit = 2
depth_limit_search(nested_array, depth_limit)
Output:
At depth 0: Processing element: 1
At depth 0: Encountered nested list, diving deeper...
At depth 1: Processing element: 2
At depth 1: Processing element: 3
At depth 0: Encountered nested list, diving deeper...
At depth 1: Processing element: 4
At depth 1: Encountered nested list, diving deeper...
At depth 2: Processing element: 5
At depth 2: Processing element: 6
At depth 0: Processing element: 7
At depth 0: Encountered nested list, diving deeper...
At depth 1: Processing element: 8
At depth 1: Encountered nested list, diving deeper...
At depth 2: Processing element: 9
At depth 2: Encountered nested list, diving deeper..