lundi 4 novembre 2019

Why is the if statement in the function definition being skipped?

In the function definition for enqueue(rb, x), there is an if statement with an increment, and a size increment after the if statement. When I run the code, neither of the variables increase.

I have tried changing the main() function where the range starts at N and ends at 1 with a -1 step, which changes the first element of the list to 1, but still does not increment, so it keeps changing the first element,

def create(capacity):
    buff = stdarray.create1D(capacity, None)
    count = 0
    for i in buff:
        if i != None:
            count += 1
    size = count
    first = 0
    for i in range(len(buff)):
        if buff[i] != None and first == 0:
            first += i
    last = 0
    for i in range(len(buff)):
        if buff[i] == None and buff[i - 1] != None and buff[i +1] == None:
            last += i 
            break
    rb = [buff, size, first, last]
    return rb

def enqueue(rb, x):
    last = rb[3]
    size = rb[1]
    rb[0][last] = x
    if last + 1 == len(rb[0]):
        last = 0
    else:
        last += 1
    size += 1



def _main():
    N = int(sys.argv[1])
    rb = create(N)
    for i in range(1, N + 1):
        enqueue(rb, i)
    stdio.writeln(rb)

if __name__ == '__main__':
    _main()

If the input is 5, the output should be [[1, 2, 3, 2, 5], 5, 0, 0]. I am getting [[5, None, None, None, None], 0, 0, 0] instead.

Aucun commentaire:

Enregistrer un commentaire