gpt-4o-2024-08-06· openai
WildChat-4.8M
Problem Statement: Write a Program to print the following pattern Ente 1 N=5 01 02 03 04…
- User
Problem Statement:
Write a Program to print the following pattern
Ente
1
N=5
01 02 03 04 05
16 17 18 19 06
15 24 25 20 07
14 23 22 21 08
13 12 11 10 09
Input Format:
Get integer as a input
Output Format:
Display the pattern
- Assistant
To create the pattern described in the problem statement, we need to fill a matrix in a spiral order. Below is a Python program that achieves this.
def spiral_matrix(n): matrix = [[0] * n for _ in range(n)] num = 1 top, left = 0, 0 bottom, right = n - 1, n - 1 while top <= bottom and left <= right: # Traverse from left to right for i in range(left, right + 1): matrix[top][i] = num num += 1 top += 1 # Traverse from top to bottom for i in range(top, bottom + 1): matrix[i][right] = num num += 1 right -= 1 # Traverse from right to left if top <= bottom: for i in range(right, left - 1, -1): matrix[bottom][i] = num num += 1 bottom -= 1 # Traverse from bottom to top if left <= right: for i in range(bottom, top - 1, -1): matrix[i][left] = num num += 1 left += 1 for row in matrix: print(" ".join(f"{x:02d}" for x in row)) # Get the input from the user N = int(input("Enter the value of N: ")) # Display the pattern spiral_matrix(N)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.