user3437711 user3437711 - 1 month ago 10
Python Question

python challenge qn for loop

EDIT: I SOLVED THE QUESTION BUT I WOULD APPRECIATE IF SOMEONE COULD JUST HELP ME MAKE MY CODE AS SHORT AS POSSIBLE (THE CHALLENGE WAS TO CODE IT WITH THE LEAST POSSIBLE CHARACTERS).

I was going through some python challenges online and I came across this question.

Click here to view the challenge question

I have been trying to solve it for quite a while but I am stuck in one place (heres my code):

c = ['H','H','H','H','H','H','H','I','H','H','H','H','F','H','H','H','H','H','H','H','H','H','H','H','H',]
c_copy = list(c)

def prt():
print("\n"+c[0]+"\t"+c[1]+"\t"+c[2]+"\t"+c[3]+"\t"+c[4]+"\n"+
c[5]+"\t"+c[6]+"\t"+c[7]+"\t"+c[8]+"\t"+c[9]+"\n"+
c[10]+"\t"+c[11]+"\t"+c[12]+"\t"+c[13]+"\t"+c[14]+"\n"+
c[15]+"\t"+c[16]+"\t"+c[17]+"\t"+c[18]+"\t"+c[19]+"\n"+
c[20]+"\t"+c[21]+"\t"+c[22]+"\t"+c[23]+"\t"+c[24])

generations = 3

while generations > 0:
for x, y in enumerate(c):

if(y == 'F'):
c_copy[x] = 'H'

if(y == 'I'):
c_copy[x] = 'F'

try:
if(c[x+1] == 'H'):
c_copy[x+1] = 'I'
if(c[x-1] == 'H'):
c_copy[x-1] = 'I'
if(c[x+5] == 'H'):
c_copy[x+5] = 'I'
if(c[x-5] == 'H'):
c_copy[x-5] = 'I'
except IndexError:
pass

c = list(c_copy)
generations = generations - 1

prt()


I know where my problem is but I cant seem to get around it. In the for loop, when i[7] == I, I change the value to its east to 'I', but when the for loop goes to i[8], it will change the value to its east again. That keeps happening until the value is not 'I'. how do I get around that? And also if someone can do the challenge in a simpler and better way, please put up your code.
Thanks

Answer

Try using lists to keep track of the cells that need to change under certain conditions. I have also used a multidimensional list to keep track of the field of flowers.

Code with annotations:

#sets up the grid and the number of generations
grid=[['H','H','H','H','H'],['H','H','I','H','H'],['H','H','F','H','H'],['H','H','H','H','H'],['H','H','H','H','H']]
gen=3

#loops for each generation
for i in range(gen):
    #creates lists for cells that need to be infected if possible
    #and cells that need to be replanted
    infect=[]
    plant=[]

    #loops through the grid cells
    for x in range(5):
        for y in range(5):

            if grid[x][y]=='F':
                #adds each fading cell to the list for replanting
                plant.append([x,y])

            if grid[x][y]=='I':
                #makes the cell fade
                grid[x][y]='F'
                #checks if each of its neighbours is a valid grid square
                #and if so adds it to the list to be infected if possible
                if x>0:
                    infect.append([x-1,y])
                if x<4:
                    infect.append([x+1,y])
                if y>0:
                    infect.append([x,y-1])
                if y<4:
                    infect.append([x,y+1])

    #infects healthy cells in the 'to be infected' list
    for x,y in infect:
        if grid[x][y]=='H':
            grid[x][y]='I'

    #replants cells in the replanting list
    for x,y in plant:
        grid[x][y]='H'

#nicely prints the grid
for a in grid:
    for b in a:
        print(b,end='')
    print()

Code without annotations:

grid=[['H','H','H','H','H'],['H','H','I','H','H'],['H','H','F','H','H'],['H','H','H','H','H'],['H','H','H','H','H']]
gen=3
for i in range(gen):
    infect=[]
    plant=[]
    for x in range(5):
        for y in range(5):
            if grid[x][y]=='F':
                plant.append([x,y])
            if grid[x][y]=='I':
                grid[x][y]='F'
                if x>0:
                    infect.append([x-1,y])
                if x<4:
                    infect.append([x+1,y])
                if y>0:
                    infect.append([x,y-1])
                if y<4:
                    infect.append([x,y+1])
    for x,y in infect:
        if grid[x][y]=='H':
            grid[x][y]='I'
    for x,y in plant:
        grid[x][y]='H'
for a in grid:
    for b in a:
        print(b,end='')
    print()
Comments