[Tutor] buggy bug in my program

Kermit Rose kermit at polaris.net
Sun Jun 11 08:28:58 CEST 2006


# def insertw(j1,k,w1,w2,jar,limit):
#    trace = 1
#    if k == 0:
#        jar[k][0] = k
#        jar[k][1] = w1
#        jar[k][2] = w2
#        return
#    if j1 > k:
#        jar[j1][0] = k
#        jar[j1][1] = w1
#        jar[j1][2] = w2
#        return
#        
#    for j2 in range(j1+1,k+1):
#        j3 = k + j1 - j2 
#        if trace > 0:
#            print " insertw:  move jar[",j3,"] up one"," j1 = ",j1," k = "
k," w1 = ",w1," w2 = ",w2
#        f = jar[j3]
#        jar[j3+1] = f
#        if trace > 0:
#            print " insertw: jar[",j3+1," is now ",jar[j3+1]
#
#        
#    jar[j1][0] = k
#    jar[j1][1] = w1
#    jar[j1][2] = w2
#
#    if trace > 0:
#        for j in range(k+1):
#            print " insertw: jar[",j,"] = ",jar[j]
#    return
#

 
debug trace shows the following puzzling behavior.
 
 
fermat2: before insertw:  jar[ 0 ] =  [0, 2, 4]
 fermat2: before insertw:  jar[ 1 ] =  [1, 4, 16]
 fermat2: before insertw:  jar[ 2 ] =  [-1, -1, -1]
 
 
I show the array jar before going into insert.
 
remember the heading of  insertw is
 
# def insertw(j1,k,w1,w2,jar,limit):
 
 

 insertw:  move jar[ 1 ] up one  j1 =  1  k =  2  w1 =  16  w2 =  13

This shows that insert made one shift, and sifted jar[1] to jar[2].
j1 = 1 means that insertw was supposed to insert new value into jar[1]
 
 insertw: jar[ 2]  is now  [1, 4, 16]

I print out jar[2] to show that insertw really did shift jar[1] to jar[2].
 
 
 
 
 insertw: jar[ 0 ] =  [0, 2, 4]
 insertw: jar[ 1 ] =  [2, 16, 13]
 insertw: jar[ 2 ] =  [2, 16, 13]
 
Now, outside the loop,  
I  set jar[j1] to the new values.
 
And I print the resulting array, still within the routine insertw.
 
jar[1] has been set to the new values.
 
BUT, and this is the problem,
 
jar[2] has been also set to the new values.
 
WHY???????????
 
 
 
 
 
 
       



More information about the Tutor mailing list