python - Non decreasing sequences -


i trying write simple python script find non-decreasing sequences made of positive integers summing 7. code not seem work it's supposed no matter try. here's have

components = [1,2,3,4,5,6,7]  ans = []  def sumseq(seq):     suma = 0     in seq:         suma +=     return suma  def findseq(seq):     x in components:         if (x < seq[-1]):             continue          newseq = seq         newseq.append(x)         suma = sumseq(newseq)          if (suma > 7):             continue          if (suma == 7):             ans.append(newseq)          findseq(newseq)  findseq([0])  print ans 

when following assignment:

newseq = seq 

you bind different name (newseq) same object (seq), not create new object similar values. change contents of newseq, change contents of seq, too, since both aliases of same object stored in memory. python documentation says:

assignment statements in python not copy objects, create bindings between target , object. collections mutable or contain mutable items, copy needed 1 can change 1 copy without changing other.

so have change line of code with:

newseq = seq.copy() 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -