Ask Your Question

Revision history [back]

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?

Katika

click to hide/show revision 2
Fixed some formatting issues.

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 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

order.
    """         
    ev = len(A)-1
    while A[ev]==ev+N-len(A):
    ev=ev-1

A[ev] == ev+N-len(A):
        ev=ev-1    
    return ev

ev def rakovetkezo(A,N): # rakovetkezo(A,N): """ Increases the eve(A,N)nt element by 1, and all the other elements after this element are succesive natural numbers

numbers
    """
    Q=A
 ev=eve(Q,N)
x=Q[ev]

    x=Q[ev]  
    for i in range(ev,len(Q)):
     Q[i]=x+i-ev+1
  return Q

Q def reszhalmazok(N,k): #It reszhalmazok(N,k): """ It should (but it doesn't) return the k-element subset of [1,2,...,N]

[1,2,...,N]
    """
    X=range(k)
  B=[range(k)]
  ev=eve(X,N)
  while ev!=-1:

ev != -1:
        X=rakovetkezo(X,N)
     B.append(X)

    ev=eve(X,N)

        ev = eve(X,N)
    return B

Now reszhalmazok(5,3) reszhalmazok(5,3) should return [[0,1,2], [0,1,3],...] [0,1,3],...] but it returns something completely different, aaand I don't see why, and how could be improve it?

Katika

click to hide/show revision 3
Added some more tags

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?

Katika