Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code I wrote is supposed to read a matrix I give as an input from a txt file via the command line. I execute it like this:python3 doomday_py.py file.txt in visualcode. When I read the input matrix , for example,

+..X..X...
....XX...-

I save it into an array. The first line is a[0] and the second a[1] in this example. Element a[0][1] is '.' , in this example. Then with the function flood I wrote I make changes into this list. If lets say a[0][1]=='.' I want to change it into a[0][1]=='+' , according to the element I remove each time from the queue. I wrote the flood function.

when I run the code I get :

Traceback (most recent call last):
File "doomday_py.py", line 118, in <module>
File "doomday_py.py", line 118, in <module>
synteleia = flood(element, x, y, col_per_line, lines)
File "doomday_py.py", line 62, in flood
a[x+1][y]=element
TypeError: 'str' object does not support item assignment

If I make such a change:

Python
if x-1>=0:
    if a[x-1][y]=='.':
       var=a[x-1][y]
       var=element
       insert(element, x-1, y, 0)

    elif (a[x-1][y]!='X' and a[x-1][y]!=element):
        a[x-1][y]='*'
        synteleia = 1

I no longer get this error but the program doesnt terminate. Can u please explain me why I get such behavior?

Also, in this program I wrote, I see that I can change elements inside a list without getting error
<pre lang="Python">


What I have tried:

Python
front = front2 = front3 = -1
rear = rear2 = rear3 = -1
queue_array=[]
queue_array2=[]
queue_array3=[]
element='='
x=y=0
#qa=bb=aa=0
#item=i=j=t=0

def insert(item, i, j, t):
    global front, rear, rear2, rear3, front2, front3 #insert element into the queue
    if front == -1:
        front = 0
        front2 = 0
        front3 = 0
    rear = rear + 1
    queue_array.insert(rear, item)
    rear2 = rear2 + 1
    queue_array2.insert(rear2, i)  # x coordinate
    rear3 = rear3 + 1
    queue_array3.insert(rear3, j)  #y coordinate
    
def delete():
    global front, front2, front3
    qa=queue_array[front]    #the element to be deleted from the queue + or - 
    aa=queue_array2[front2]
    bb=queue_array3[front3]
    front = front + 1 # x coordinate of deleted element
    front2=front2 + 1 # y coordinate of del elem
    front3=front3 + 1
    #print(qa)
    #print(aa)
    #print(bb)
    return(qa, aa, bb)

def flood(element, x, y, col_per_line, lines):
    synteleia = 0
    if x-1>=0:
        if a[x-1][y]=='.':
           a[x-1][y]=element
           insert(element, x-1, y, 0)
        
        elif (a[x-1][y]!='X' and a[x-1][y]!=element):
            a[x-1][y]='*'
            synteleia = 1
        
    
    
    if(y-1>=0):
        if (a[x][y-1]=='.'):
            a[x][y-1]=element
            insert(element, x, y-1, 0)
        
        elif (a[x][y-1]!='X' and a[x][y-1]!=element):
            a[x][y-1]='*'
            synteleia = 1
        
    
    if(x+1 < lines):
        if (a[x+1][y]=='.'):
            a[x+1][y]=element
            insert(element, x+1, y, 0)
        
        elif (a[x+1][y]!='X' and a[x+1][y]!=element):
            a[x+1][y]='*'
            synteleia = 1
        
    
    
    if(y+1 < col_per_line):
        if (a[x][y+1]=='.'):
            a[x][y+1]=element
            insert(element, x, y+1, 0)
        
        elif (a[x][y+1]!='X' and a[x][y+1]!=element):
            a[x][y+1]='*'
            synteleia = 1
        
    
    return synteleia

  
import sys
with open(sys.argv[1], 'r') as f:                  
    array = f.readlines()                  #reads the file's lines
    array = [x.strip() for x in array]     #ignores the newline char
#print (content)

a=array
lines=len(a)
for j in range(len(a)):                             
    for k in range(len(a[j])):                        
                col_per_line=len(a[j])

for j in range(len(a)):         #the list consists of 2 sublists, for these 2 sublists
    for k in range(len(a[j])):      #for every elemenent in the sublist, loops in the "2D" list
         if(a[j][k]=='+' or a[j][k]=='-'):    # add + or - ,only them, into the queue
                mol=a[j][k]
                #print(mol)
                #print(j, k)
                t=0
                insert(mol,j, k,t)
                
   # print()

synteleia, timef = 0 , 0
while (synteleia==0 and front<rear):
    size = rear - front + 1
    while(size > 0):
        t=0
        qa,aa,bb = delete()
        element=qa
        print(element)
        x=aa
        y=bb
        print(x,y)
        synteleia = flood(element, x, y, col_per_line, lines)
        size=size-1
        
    timef=timef+1

#print(queue_array)
Posted
Comments
Richard MacCutchan 17-May-18 15:11pm    
Which is line 62, which is line 118?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900