jmooremcc

test30.py

Feb 12th, 2022
1,461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. SPACE = '   '
  2.  
  3. def subsets(nums):
  4.     res = []
  5.    
  6.     def dfs(temp, idx):
  7.         print(f"{idx * SPACE}entering dfs({temp},{idx})")
  8.         res.append(temp[:])
  9.         print(f"{idx * SPACE}res:{res}")
  10.        
  11.         print(f"{idx * SPACE}entering loop")              
  12.         for i in range(idx, len(nums)):
  13.             temp.append(nums[i])
  14.             print(f"{i * SPACE}temp:{temp} i:{i} before recursive call")
  15.             dfs(temp,i +1)
  16.             print(f"{i * SPACE}temp:{temp} i:{i} before backtrack")
  17.             temp.pop()
  18.             print(f"{i * SPACE}temp:{temp} i:{i} after backtrack")
  19.        
  20.         print(f"{idx * SPACE}exiting loop")
  21.        
  22.     dfs([], 0)
  23.     print("exiting dfs")
  24.    
  25.     return res
  26.  
  27. print(f"\nsubsets: {subsets([1,2,3])}")
Advertisement
Add Comment
Please, Sign In to add comment