Non decreasing sequences
<p>I am trying to write a simple python script that will find all the non-decreasing sequences made up of positive integers summing to 7. My code does not seem to work as it's supposed to no matter what I try. Here's what I have</p>

<pre><code>components = [1,2,3,4,5,6,7]

ans = []

def sumSeq(seq):
    sumA = 0
    for i in seq:
        sumA += i
    return sumA

def findSeq(seq):
    for x in components:
        if (x &lt; seq[-1]):
            continue

        newSeq = seq
        newSeq.append(x)
        sumA = sumSeq(newSeq)

        if (sumA &gt; 7):
            continue

        if (sumA == 7):
            ans.append(newSeq)

        findSeq(newSeq)

findSeq([0])

print ans
</code></pre>

<p>When you do the following assignment:</p>

<pre><code>newSeq = seq
</code></pre>

<p>you actually bind a different name (newSeq) to the same object (seq), you do not create a new object with similar values. So as you change the contents of newSeq, you change the contents of seq, too, since they are both aliases of the same object stored in memory. 
As the Python documentation says:</p>

<blockquote>
  <p>Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other.</p>
</blockquote>

<p>So you have to change this line of code with:</p>

<pre><code>newSeq = seq.copy()
</code></pre>

python sequence