def legal(puzzle, i, j, k, diagonal=False):
    """
    Check if k can be placed at the jth column of ith row.

    Parameters:

        puzzle (nested list): sudoku grid

        i (int): row to be checked

        j (int): column to be checked

        k (int): number to be placed in puzzle[i][j]

        diagonal (bool): True if diagonal sudoku, False otherwise (default: False)

    Returns:

        bool: True if k can be placed in puzzle[i][j], False otherwise.
    """
    for a in range(9):
        if a != j and puzzle[i][a] == k:
            return False
        if a != i and puzzle[a][j] == k:
            return False
    for a in range((i//3)*3, (i//3)*3+3):
        for b in range((j//3)*3, (j//3)*3+3):
            if puzzle[a][b] == k:
                return False
    if diagonal:
        diag_left = [0]*10
        diag_right = [0]*10
        temp = puzzle[i][j]
        puzzle[i][j] = k
        for a in range(9):
            diag_left[puzzle[a][a]] += 1
            diag_left[puzzle[a][8-a]] += 1
        puzzle[i][j] = temp
        if not all([diag_left[a] <= 1 for a in range(1, 10)]):
            return False
        if not all([diag_right[a] <= 1 for a in range(1, 10)]):
            return False
    return True