python challenge qn for loop

时间:2016-10-16 05:07:21

标签: python for-loop

编辑:我解决了这个问题,但是如果有人能够帮助我尽快做出我的代码,那么我会感到欣慰(挑战是用最不可能的特征来代码)。

我正在网上遇到一些python挑战,我遇到了这个问题。

Click here to view the challenge question

我一直试图解决它已经有一段时间了,但我被困在一个地方(以下是我的代码):

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()

我知道我的问题在哪里,但我似乎无法绕过它。在for循环中,当i [7] == I时,我将其值更改为“I”,但是当for循环转到i [8]时,它将再次将值更改为其东。这种情况一直持续到价值不是'我'。我怎么解决这个问题?如果有人能够以更简单,更好的方式应对挑战,请提出您的代码。 感谢

4 个答案:

答案 0 :(得分:0)

尝试使用列表来跟踪在特定条件下需要更改的单元格。我还使用了一个多维列表来跟踪花的领域。

带注释的代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map" style="height: 400px; width: 500px"></div>

没有注释的代码:

#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()

答案 1 :(得分:0)

这是另一种解决方案:

import sys
# use a string to hold the flowerbed
bed = 'HHHHH' 'HHIHH' 'HHFHH' 'HHHHH' 'HHHHH'

# a function to write out a flowerbed
pr = lambda b: sys.stdout.write('\n'.join(b[x:x+5] for x in range(0, 25, 5))+'\n\n')

# make a list of the cells to check for infection, for each bed position
ckl = [ list(cc for cc in (5*(r-1)+c, 5*r+c-1, 5*r+c, 5*r+c+1, 5*(r+1)+c)
             if 0 <= cc < 25 and (cc // 5 ==r or cc % 5 == c))
        for r in range(5)
        for c in range(5) ]

pr(bed)    # print the initial configuration
for gen in range(5):
    bed = ''.join([ tx.get(c, 'I' if bed[i]=='H' and any([bed[n]=='I' for n in ckl[i]])
                                  else 'H')
                    for i,c in enumerate(bed) ])
    pr(bed)

答案 2 :(得分:0)

这个答案应该有效

class Flowers(object):
    def __init__(self, flowers_state):
        self.flowers_state = flowers_state

    def run_rule(self):
        """
        Rule1 -> Infected area become faded the following year
        Rule2 -> Faded aread becomes Healthy
        Rule3 -> 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 -> 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 -> 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 -> 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()

答案 3 :(得分:0)

基于Numpy的解决方案(在函数curr中的2D numpy数组nextupdate中,0,1和2对应于“健康”,“感染”和“褪色”分别地):

import numpy as np
def update(curr, next):
    next[curr==1] = 2 # infected flowers become faded
    next[curr==2] = 0 # faded flowers become healthy 

    iw0 = np.argwhere(curr==0) # indices of elements of `curr` that are 0 (healthy)
    next[tuple(iw0.T)] = 0

    def fix_bad_indices(ia, shape):
        """ ia : an array of array-indices, some of which may be bad (exceed array dimensions)
            shape: an array's shape based on which any bad index in `ia` will be fixed 

            (Note that the data in `ia` may get modified in this function)
        """
        assert ia.ndim == 2 and len(shape) == ia.shape[1]
        for axis, size in enumerate(shape):
            for bad, new in [(-1, 0), (size, size-1)]:
                kk = ia[:, axis]
                kk[bad == kk] = new
        return ia

    senw = [(1, 0), (0, 1), (-1, 0), (0, -1)] # south, east, north, west

    # an array of neighbour indices of all healthy flowers
    nb_idxs = np.stack([fix_bad_indices(iw0 + shift, curr.shape) for shift in senw], axis=1)

    # elements of `curr` at neighbour-indices
    nb_elems = curr[tuple(nb_idxs.reshape(-1, 2).T)]
    nb_elems = nb_elems.reshape(*nb_idxs.shape[:-1])

    nb_elems[nb_elems==2] = 0
    next[tuple(iw0.T)] = np.any(nb_elems, axis=1)


def run(grid):
    curr = grid
    next = curr.copy()
    while 1:
        update(curr, next)
        yield next
        next, curr = curr, next


def main(grid, ngens):
    dct = {'H':0, 'I':1, 'F':2}
    rdct = dict(zip(dct.values(), dct.keys())) 

    def to_string(array):
        return '\n'.join(''.join(rdct[x] for x in row) for row in array)

    def to_array(string):
        return np.array([[dct[x] for x in row] for row in string.splitlines()])

    # grid = np.mod(np.arange(20).reshape(5, 4), 3)
    grid = to_array(grid)

    print(to_string(grid))
    print()
    for i, grid in zip(range(ngens), run(grid)): 
        print(to_string(grid))
        print()
    return to_string(grid)

if __name__ == '__main__':
    grid="""HHHHH 
            HHIHH 
            HHFHH 
            HHHHH 
            HHHHH""".replace(' ','')
    main(grid, 3)