I was coding the dfs solution for a problem.
When I write the code as below, on debugging, I find that whenever the code reaches at self.dfs_solution_rework, instead of recursing, it just continues execution, leading to wrong results.:
def dfs_solution_rework(self, end, start= 1, sorted=False, num= None):
if not num:
for i in range(1, 10):
self.dfs_solution_rework(end, start, num=i)
elif num <= end:
if num >= start:
yield num
last_digit = num % 10
if not (last_digit == 0 or last_digit == 9):
self.dfs_solution_rework(end, start, num=num * 10 + (last_digit - 1))
self.dfs_solution_rework(end, start, num=num * 10 + (last_digit + 1))
elif last_digit == 0:
self.dfs_solution_rework(end, start, num=num * 10 + 1)
else:
self.dfs_solution_rework(end, start, num=num * 10 + 8)
On the other hand, if I write the dfs with a util (helper) method, as follows, it works without any issues.
def dfs_solution(self, end, start= 1, sorted=False, num= None):
def dfs_util(end, start, num):
if num <= end:
if num >= start:
print(num)
last_digit = num % 10
if not (last_digit == 0 or last_digit == 9):
dfs_util(end, start, num=num * 10 + (last_digit - 1))
dfs_util(end, start, num=num * 10 + (last_digit + 1))
elif last_digit == 0:
dfs_util(end, start, num=num * 10 + 1)
else:
dfs_util(end, start, num=num * 10 + 8)
for i in range(1, 10):
dfs_util(end, start, num=i)
Any help as to why this behaviour might be happening ? I have debugged it in VS Code to understand but couldn't get any idea.
PS: Not a homework problem. :)
Thanks