All pastes #1663077 Raw Edit

Miscellany

public python v1 · immutable
#1663077 ·published 2009-11-09 07:33 UTC
rendered paste body
def load_grid(file_name):    '''Read in a grid from a file.  For testing purposes only.'''    fin = open(file_name, 'r')    grid = []    for line in fin:        row = []        line_strip = line.strip()        for ch in line_strip:            row.append(ch)        grid.append(row)    fin.close()    return griddef draw_grid(grid):    '''Draw the grid to the terminal.  For testing purposes only.'''    for i in range(len(grid)):        row = ''        for j in range(len(grid[i])):            row += grid[i][j]        print rowdef remove_blob(grid, row, col):    '''Remove the entire blob at position (row, col).'''    if grid[row][col] == ".":        return    if grid[row][col] == "#":        grid[row][col] = "."        next_col = col + 1        next_row = row + 1        row_before = row - 1        col_before = col - 1        if next_col < len(grid[row]):            remove_blob(grid, row, next_col)        if next_row < len(grid):            remove_blob(grid, next_row, col)        if col_before > -1:            remove_blob(grid, row, col_before)        if row_before > -1:            remove_blob(grid, row_before, col)if __name__ == '__main__':    grid = load_grid('./grid.in')    draw_grid(grid)    print    remove_blob(grid, 0, 1)    draw_grid(grid)    print    remove_blob(grid, 2, 4)    draw_grid(grid)    print    remove_blob(grid, 0, 4)    draw_grid(grid)    print    remove_blob(grid, 3, 3)    draw_grid(grid)    print