human
I am working on the file ".ipynb_checkpoints/csp-checkpoint.ipynb".
The first task is:
NQueens Visualization
Just like the Graph Coloring Problem we will start with defining a few helper functions to help us visualize the assignments as they evolve over time. The make_plot_board_step_function behaves similar to the make_update_step_function introduced earlier. It initializes a chess board in the form of a 2D grid with alternating 0s and 1s. This is used by plot_board_step function which draws the board using matplotlib and adds queens to it. This function also calls the label_queen_conflicts which modifies the grid placing 3 in positions in a position where there is a conflict.
Can you write Python code for it?
assistant
#Here is the Python code you requested.

def label_queen_conflicts(assingment,grid):
    ''' Mark grid with queens that are under conflict. '''
    for col, row in assingment.items(): # check each queen for conflict
        row_conflicts = {temp_col:temp_row for temp_col,temp_row in assingment.items() 
                         if temp_row == row and temp_col != col}
        up_conflicts = {temp_col:temp_row for temp_col,temp_row in assingment.items() 
                        if temp_row+temp_col == row+col and temp_col != col}
        down_conflicts = {temp_col:temp_row for temp_col,temp_row in assingment.items() 
                          if temp_row-temp_col == row-col and temp_col != col}
        
        # Now marking the grid.
        for col, row in row_conflicts.items():
                grid[col][row] = 3
        for col, row in up_conflicts.items():
                grid[col][row] = 3
        for col, row in down_conflicts.items():
                grid[col][row] = 3

    return grid

def make_plot_board_step_function(instru_csp):
    '''ipywidgets interactive function supports
       single parameter as input. This function
       creates and return such a function by taking
       in input other parameters.
    '''
    n = len(instru_csp.variables)
    
    
    def plot_board_step(iteration):
        ''' Add Queens to the Board.'''
        data = instru_csp.assingment_history[iteration]
        
        grid = [[(col+row+1)%2 for col in range(n)] for row in range(n)]
        grid = label_queen_conflicts(data, grid) # Update grid with conflict labels.
        
        # color map of fixed colors
        cmap = matplotlib.colors.ListedColormap(['white','lightsteelblue','red'])
        bounds=[0,1,2,3] # 0 for white 1 for black 2 onwards for conflict labels (red).
        norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N)
        
        fig = plt.imshow(grid, interpolation='nearest', cmap = cmap,norm=norm)

        plt.axis('off')
        fig.axes.get_xaxis().set_visible(False)
        fig.axes.get_yaxis().set_visible(False)

        #...