ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 16 Oct 2010 05:42:24 +0200Help! I have a problem with lists!https://ask.sagemath.org/question/7727/help-i-have-a-problem-with-lists/Hello!
I am a real beginner in Sage, I have just started th use it. In fact this is my first programing language, so I have some difficulties with it... Now I needed a command, which determins all the k-element subset of [1,2,...,N]. Since I don't found a command like this, I decided to write a program... :-) And now I simple don't understand while my program doesn't work corrrectly! Could you explain me? It is a bit longish, so sorry for this.
def eve(A,N):
"""
the largest elem in the list A which can
be increased by 1, such that the resulting list contains only
different elements less than N. Originally
A should contain different elements in increasing order.
"""
ev = len(A)-1
while A[ev] == ev+N-len(A):
ev=ev-1
return ev
def rakovetkezo(A,N):
"""
Increases the eve(A,N)nt element by 1, and all the other elements
after this element are succesive natural numbers
"""
Q=A
ev=eve(Q,N)
x=Q[ev]
for i in range(ev,len(Q)):
Q[i]=x+i-ev+1
return Q
def reszhalmazok(N,k):
"""
It should (but it doesn't) return the k-element
subset of [1,2,...,N]
"""
X=range(k)
B=[range(k)]
ev=eve(X,N)
while ev != -1:
X=rakovetkezo(X,N)
B.append(X)
ev = eve(X,N)
return B
Now `reszhalmazok(5,3`) should return `[[0,1,2], [0,1,3],...]` but it returns something completely different, aaand I don't see why, and how could be improve it?
KatikaKatikaSat, 16 Oct 2010 05:42:24 +0200https://ask.sagemath.org/question/7727/