Processing math: 100%
Ask Your Question
0

A problem in outputting a list

asked 0 years ago

hamouda gravatar image

I want to output a list depending on two parameters m and t, My attempt is

def L(m,t):
List=[]
dim=0
for k in srange(m+1):
    for j in srange(0,3):
        for i in srange(1,m-k+1):
            List.extend([(k,i,j)])
            dim=dim+1
for j in srange(floor(t)+1):
    for k in srange(floor(m/t)*j,m+1):
        for i in srange(0,1):
            List.extend([(k,i,j)]) # Indentation fixed here
            dim=dim+1
    return List

When we call len(L(3,1)) i.e.m=3 and t=1; sage outputs 22, this is not logical for me because I implemented again without using the function the length of the list (also for m=3 and t=1) becomes 23 as the following code

dim=0
List=[]
for k in srange(4):
    for j in srange(0,3):
        for i in srange(1,4-k):
            List.extend([(k,i,j)])
            dim=dim+1
for j in srange(2):
    for k in srange(3*j,4):
        for i in srange(0,1):
            List.extend([(k,i,j)]) 
            dim=dim+1

So where is the problem to solve it ?

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
0

answered 0 years ago

Emmanuel Charpentier gravatar image

updated 0 years ago

Hard to say : your cut'n'paste has probably mangled your code (indentations and spaces are semantic in Python).

Running :

# Original code does *not* run (bad use of indentation).
# *GUESSING* indentation
def L(m,t):
    List=[]
    dim=0
    for k in srange(m+1):
        for j in srange(0,3):
            for i in srange(1,m-k+1):
                List.extend([(k,i,j)])
                dim=dim+1
    for j in srange(floor(t)+1):
        for k in srange(floor(m/t)*j,m+1):
            for i in srange(0,1):
                List.extend([(k,i,j)])
                dim=dim+1
    return List

gives :

sage: len(L(3,1))
23

which seems to be what you expected...

HTH,

Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 0 years ago

Seen: 170 times

Last updated: Jun 22 '24