Did anyone else’s mind get blown learning about nesting loops? /u/PathRealistic6940 Python Education
Did anyone else’s mind get blown learning about nesting loops? /u/PathRealistic6940 Python Education
Edit: It is actually recursion, not nesting loops. Got the label wrong.
So was going through some challenges and got into backtracking. Long story short, I learned about nesting for loops, but not visually. Like this instead.
def find_zero_sum_subset(arr): arr_sub = [] def backtrack(start, current_subset): if sum(current_subset) == 0 and len(current_subset) > 0: arr_sub.append(current_subset) for i in range(start,len(arr)): backtrack(i+1, current_subset + [arr[i]]) return arr_sub return backtrack(0,[])
That’s nuts. It starts a loop in the middle of a loop, in the middle of a loop, n times. Now that i look back on it, it makes sense. Took me a while to wrap my head around this
submitted by /u/PathRealistic6940
[link] [comments]
r/learnpython Edit: It is actually recursion, not nesting loops. Got the label wrong. So was going through some challenges and got into backtracking. Long story short, I learned about nesting for loops, but not visually. Like this instead. def find_zero_sum_subset(arr): arr_sub = [] def backtrack(start, current_subset): if sum(current_subset) == 0 and len(current_subset) > 0: arr_sub.append(current_subset) for i in range(start,len(arr)): backtrack(i+1, current_subset + [arr[i]]) return arr_sub return backtrack(0,[]) That’s nuts. It starts a loop in the middle of a loop, in the middle of a loop, n times. Now that i look back on it, it makes sense. Took me a while to wrap my head around this submitted by /u/PathRealistic6940 [link] [comments]
Edit: It is actually recursion, not nesting loops. Got the label wrong.
So was going through some challenges and got into backtracking. Long story short, I learned about nesting for loops, but not visually. Like this instead.
def find_zero_sum_subset(arr): arr_sub = [] def backtrack(start, current_subset): if sum(current_subset) == 0 and len(current_subset) > 0: arr_sub.append(current_subset) for i in range(start,len(arr)): backtrack(i+1, current_subset + [arr[i]]) return arr_sub return backtrack(0,[])
That’s nuts. It starts a loop in the middle of a loop, in the middle of a loop, n times. Now that i look back on it, it makes sense. Took me a while to wrap my head around this
submitted by /u/PathRealistic6940
[link] [comments] Edit: It is actually recursion, not nesting loops. Got the label wrong. So was going through some challenges and got into backtracking. Long story short, I learned about nesting for loops, but not visually. Like this instead. def find_zero_sum_subset(arr): arr_sub = [] def backtrack(start, current_subset): if sum(current_subset) == 0 and len(current_subset) > 0: arr_sub.append(current_subset) for i in range(start,len(arr)): backtrack(i+1, current_subset + [arr[i]]) return arr_sub return backtrack(0,[]) That’s nuts. It starts a loop in the middle of a loop, in the middle of a loop, n times. Now that i look back on it, it makes sense. Took me a while to wrap my head around this submitted by /u/PathRealistic6940 [link] [comments]