def compute_alignment_matrix(seq_x, seq_y, scoring_matrix, global_flag):
    """
    Takes as input two sequences seq_x and seq_y
    whose elements share a common alphabet with
    the scoring matrix scoring_matrix. The function
    computes and returns the alignment matrix for 
    seq_x and seq_y as described in the Homework.
    If global_flag is True, each entry of the alignment
    matrix is computed using the method described in Question 8 of the Homework.
    If global_flag is False, each entry is computed 
    using the method described in Question 12 of the Homework.
    """
    rows = len(seq_x) + 1
    cols = len(seq_y) + 1
    s_matrix = [[_ for _ in range(cols)] for _ in range(rows)]

    s_matrix[0][0] = 0     
        
    if global_flag:         
        for row in range(1, rows):
            s_matrix[row][0] = s_matrix[row - 1][0] + scoring_matrix[seq_x[row - 1]]['-']
        for col in range(1, cols):
            s_matrix[0][col] = s_matrix[0][col - 1] + scoring_matrix['-'][seq_y[col - 1]]
        
        for row in range(1, rows):
            for col in range(1, cols):
                s_matrix[row][col] = max(s_matrix[row - 1][col - 1]
                 + scoring_matrix[seq_x[row - 1]][seq_y[col - 1]],
                 
                max(s_matrix[row - 1][col]
                + scoring_matrix[seq_x[row - 1]]['-'],
                                       
                s_matrix[row][col - 1]
                + scoring_matrix['-'][seq_y[col - 1]]))    
    else:     
        for row in range(1, rows):
            s_matrix[row][0] = max(0, s_matrix[row - 1][0] + 
                                    scoring_matrix[seq_x[row - 1]]['-'])
        for col in range(1, cols):
            s_matrix[0][col] = max(0, s_matrix[0][col - 1] + 
                                    scoring_matrix['-'][seq_y[col - 1]])
                   
        for row in range(1, rows):
            for col in range(1, cols):
                s_matrix[row][col] = max(s_matrix[row - 1][col - 1]
                 + scoring_matrix[seq_x[row - 1]][seq_y[col - 1]],
                 
                max(s_matrix[row - 1][col]
                + scoring_matrix[seq_x[row - 1]]['-'],
                                       
                s_matrix[row][col - 1]
                + scoring_matrix['-'][seq_y[col - 1]]))
                
                if s_matrix[row][col] < 0:
                    s_matrix[row][col] = 0
                
    return s_matrix