python challenge qn for loop
<p>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).</p>

<p>I was going through some python challenges online and I came across this question.</p>

<p><a href="https://i.stack.imgur.com/wGw4F.png" rel="nofollow">Click here to view the challenge question</a></p>

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

<pre><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 &gt; 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()
</code></pre>

<p>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</p>

<p>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.</p>

<p>Code with annotations:</p>

<pre><code>#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&gt;0:
                    infect.append([x-1,y])
                if x&lt;4:
                    infect.append([x+1,y])
                if y&gt;0:
                    infect.append([x,y-1])
                if y&lt;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></pre>

<p>Code without annotations:</p>

<pre><code>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&gt;0:
                    infect.append([x-1,y])
                if x&lt;4:
                    infect.append([x+1,y])
                if y&gt;0:
                    infect.append([x,y-1])
                if y&lt;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()
</code></pre>

python for-loop