I commented out and located where the weird bug starts occurring in the code. It's supposed to be a sliding window problem, and as I slide the window it's supposed to add the window list inside the results list. But for some reason as I update the window list it seems to also be updating the results list??? but it updates at a point when I don't even mention the results list?
def find_subarrays(arr, target):
result = []
start = 0
window = []
product = 1
print("target", target)
for end in range(len(arr)):
print("check Result", result)
print("arr[end]", arr[end])
window.append(arr[end]) # Somehow the variables window and result are connected
print("check Result 2", result, "hmm",window)
print("firstR ", result)
result.append(window)
print("secondR ", result)
# print("result", result)
return result
find_subarrays([2, 5, 3, 10], 30)
Re. the suggested duplicate:
I am not doing an multiplication on the result list and I removed a good chunk what they actual code is supposed to do, in order to simplify and focus on the root of the problem.
also with each loop the window list is change because it is adding each element of the input arr and at the end of each loop it adds it to the result array. The output I'm getting at the end is the the same array multiple time? but the change happens at a weird spot where I don't even modify the result array.
CodePudding user response:
You are using window variable to append in result list. That is causing this snyc in result. You can use below snippet to solve this issue.
result.append([*window])