Ask Your Question
0

A problem in outputting a list

asked 2024-06-22 16:50:27 +0100

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 ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2024-06-22 17:25:01 +0100

Emmanuel Charpentier gravatar image

updated 2024-06-22 17:26:22 +0100

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,

edit flag offensive delete link more

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: 2024-06-22 16:50:27 +0100

Seen: 120 times

Last updated: Jun 22