I wrote a Python solution based @Rady solution: https://stackoverflow.com/a/63219124/14726555
I also added code to draw diagonals of any size so it is easier to see why this solution works and how it is useful for generating diagonals.
def diag_iter(row, col, n):
for i in range(n):
if i != row:
dist = abs(i - row)
if 0 <= col - dist < n:
yield (i, col - dist)
if col + dist < n:
yield (i, col + dist)
def print_diag(row, col, n):
board = [['_'] * n for _ in range(n)]
board[row][col] = 'X'
for diag_row, diag_col in diag_iter(row, col, n):
board[diag_row][diag_col] = "X"
print(f"Board of size {n} with diagional starting at ({row}, {col})")
for row in board:
print(row)
print_diag(1, 1, 5)
print_diag(2, 2, 5)
print_diag(3, 4, 5)
print_diag(0, 3, 5)
"""
Board of size 5 with diagional starting at (1, 1)
['X', '_', 'X', '_', '_']
['_', 'X', '_', '_', '_']
['X', '_', 'X', '_', '_']
['_', '_', '_', 'X', '_']
['_', '_', '_', '_', 'X']
Board of size 5 with diagional starting at (2, 2)
['X', '_', '_', '_', 'X']
['_', 'X', '_', 'X', '_']
['_', '_', 'X', '_', '_']
['_', 'X', '_', 'X', '_']
['X', '_', '_', '_', 'X']
Board of size 5 with diagional starting at (3, 4)
['_', 'X', '_', '_', '_']
['_', '_', 'X', '_', '_']
['_', '_', '_', 'X', '_']
['_', '_', '_', '_', 'X']
['_', '_', '_', 'X', '_']
Board of size 5 with diagional starting at (0, 3)
['_', '_', '_', 'X', '_']
['_', '_', 'X', '_', 'X']
['_', 'X', '_', '_', '_']
['X', '_', '_', '_', '_']
['_', '_', '_', '_', '_']
"""