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>This answer should work</p>

<pre><code>class Flowers(object):
    def __init__(self, flowers_state):
        self.flowers_state = flowers_state

    def run_rule(self):
        """
        Rule1 -&gt; Infected area become faded the following year
        Rule2 -&gt; Faded aread becomes Healthy
        Rule3 -&gt; Infected area passes infections to North, east, west, south.
        """
        visited = []
        for row_index, row_flowers_state in enumerate(self.flowers_state):
            for flower_index, flower_state in enumerate(row_flowers_state):
                if (row_index, flower_index) not in visited and flower_state == 'I':
                    # Rule -&gt; 3
                    for x, y in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
                        try:
                            if (row_index+x, flower_index+y) not in visited and \
                                    self.flowers_state[row_index+x][flower_index+y] == 'H':
                                self.flowers_state[row_index+x][flower_index+y] = 'I'
                                visited.append((row_index+x, flower_index+y))
                        except IndexError:
                            pass

                    # Rule -&gt; 1
                    self.flowers_state[row_index][flower_index] = 'F'
                    visited.append((row_index, flower_index))

                elif (row_index, flower_index) not in visited and flower_state == 'F':
                    # Rule -&gt; 2
                    self.flowers_state[row_index][flower_index] = 'H'

                    visited.append((row_index, flower_index))

    def print_states(self):
        print "\n".join(["".join(flowers_state_row) for flowers_state_row in self.flowers_state])


def main():
    flowers_state = [
        ['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']
    ]
    f = Flowers(flowers_state)
    for _ in xrange(0, 3):
        f.run_rule()
        f.print_states()
        print "\n"

main()
</code></pre>

python for-loop