title: Why do I need to use copy in recursion (climbing stairs problems)
body: <p>I was solving one of the problems from classic interview sets and I met a strange issue. The task is to by given <code>number of stairs n</code> and possible moves (steps) <code>range(1,k)</code> return all the possible combinations for steps. My solution works just if I use <code>copy.copy</code> successful steps combination before appending it to the <code>all-solutions-array</code>. Does anyone know the reason why ??</p>
  <pre><code>import copy
  def climbingStaircase(n, k, successful_steps, steps_made):
      print(n, k, successful_steps, steps_made)
  
      # if there n is less than 0 --&gt; return
      if n &lt; 0:
          print('entering here')
  
          return
      # if n  == 0 -&gt; we reached the top of the stairs
      if n == 0:
  
          successful_steps.append(copy.copy(steps_made))
  
          print('entering 0')
          print(n, k, successful_steps, steps_made)
          return
  
      for step in range(1,k+1):
  
  
          n -= step
          steps_made.append(step)
  
  
          climbingStaircase(n, k, successful_steps, steps_made) # 0, 2, [[1,1,1,1]] [1,1,1,1]
          n += steps_made.pop() #
          # steps_made.pop()
  
      return
  
  def countWays(n, k):
      successful_steps = []
      climbingStaircase(n, k,successful_steps, [])
      print(successful_steps)
  
  countWays(4,2)
  </code></pre>
  <p>For example for 4,2 with copy, I get the solution:</p>
  <pre><code>[[1, 1, 1, 1], [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2]]
  </code></pre>
  <p>If I do not use copy function, everything seems wrong, it seems as if by editing, adding and popping from <code>steps_made</code> I am still somehow influencing the <code>successful steps</code></p>