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:
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:
front = front2 = front3 = -1
rear = rear2 = rear3 = -1
queue_array=[]
queue_array2=[]
queue_array3=[]
element='='
x=y=0
def insert(item, i, j, t):
global front, rear, rear2, rear3, front2, front3
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)
rear3 = rear3 + 1
queue_array3.insert(rear3, j)
def delete():
global front, front2, front3
qa=queue_array[front]
aa=queue_array2[front2]
bb=queue_array3[front3]
front = front + 1
front2=front2 + 1
front3=front3 + 1
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()
array = [x.strip() for x in array]
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)):
for k in range(len(a[j])):
if(a[j][k]=='+' or a[j][k]=='-'):
mol=a[j][k]
t=0
insert(mol,j, k,t)
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