Creating a spiral array in python?

Creating a spiral array in python?

Creating a spiral array in Python involves filling a 2D array in a spiral pattern, where the values increase from the center outward. Here's an example of how you can create a spiral array using NumPy:

import numpy as np def create_spiral_array(n): spiral = np.zeros((n, n), dtype=int) num = 1 row_start, row_end = 0, n - 1 col_start, col_end = 0, n - 1 while num <= n * n: for i in range(col_start, col_end + 1): spiral[row_start, i] = num num += 1 row_start += 1 for i in range(row_start, row_end + 1): spiral[i, col_end] = num num += 1 col_end -= 1 for i in range(col_end, col_start - 1, -1): spiral[row_end, i] = num num += 1 row_end -= 1 for i in range(row_end, row_start - 1, -1): spiral[i, col_start] = num num += 1 col_start += 1 return spiral # Define the size of the spiral array size = 5 # Create the spiral array spiral_array = create_spiral_array(size) # Print the spiral array print(spiral_array) 

In this example, the create_spiral_array() function generates a spiral array of size n x n. The array is filled in a clockwise spiral pattern, starting from the center and moving outward.

The code uses four loops to fill the array along the top, right, bottom, and left sides, incrementing the num value as it goes. The row_start, row_end, col_start, and col_end variables control the current position of each side of the spiral. The loops continue until num reaches n * n, filling the entire array.

You can change the size variable to create spiral arrays of different sizes.

Examples

  1. Creating a spiral array in Python using nested loops:

    • Description: Implement a simple approach using nested loops to generate a spiral array in Python.
    def generate_spiral_array(n): spiral = [[0] * n for _ in range(n)] direction = [(0, 1), (1, 0), (0, -1), (-1, 0)] num = 1 row, col, d = 0, 0, 0 for _ in range(n * n): spiral[row][col] = num num += 1 next_row, next_col = row + direction[d][0], col + direction[d][1] if 0 <= next_row < n and 0 <= next_col < n and spiral[next_row][next_col] == 0: row, col = next_row, next_col else: d = (d + 1) % 4 row, col = row + direction[d][0], col + direction[d][1] return spiral # Example usage n = 5 spiral_array = generate_spiral_array(n) for row in spiral_array: print(row) 
  2. Generating a spiral array in Python using recursion:

    • Description: Explore a recursive approach to create a spiral array in Python, making use of recursive function calls.
    def generate_spiral_array_recursive(n, start_row=0, start_col=0, num=1): if n == 0: return [] if n == 1: return [[num]] spiral = [[0] * n for _ in range(n)] for col in range(start_col, start_col + n): spiral[start_row][col] = num num += 1 for row in range(start_row + 1, start_row + n): spiral[row][start_col + n - 1] = num num += 1 for col in range(start_col + n - 2, start_col - 1, -1): spiral[start_row + n - 1][col] = num num += 1 for row in range(start_row + n - 2, start_row, -1): spiral[row][start_col] = num num += 1 inner_spiral = generate_spiral_array_recursive(n - 2, start_row + 1, start_col + 1, num) for i in range(1, n - 1): for j in range(1, n - 1): spiral[start_row + i][start_col + j] = inner_spiral[i - 1][j - 1] if inner_spiral else 0 return spiral # Example usage n = 5 spiral_array = generate_spiral_array_recursive(n) for row in spiral_array: print(row) 
  3. Creating a clockwise spiral array in Python:

    • Description: Learn how to generate a clockwise spiral array in Python, where the numbers increase in a clockwise direction.
    def generate_clockwise_spiral_array(n): spiral = [[0] * n for _ in range(n)] row, col = 0, 0 num = 1 while num <= n * n: for r, c in [(0, 1), (1, 0), (0, -1), (-1, 0)]: while 0 <= row + r < n and 0 <= col + c < n and spiral[row + r][col + c] == 0: spiral[row][col] = num num += 1 row, col = row + r, col + c row, col = row - 1, col + 1 return spiral # Example usage n = 5 spiral_array = generate_clockwise_spiral_array(n) for row in spiral_array: print(row) 
  4. Generating a square spiral array in Python:

    • Description: Implement a square spiral array in Python where the dimensions of the array are the same (i.e., it's a square).
    def generate_square_spiral_array(n): spiral = [[0] * n for _ in range(n)] row, col = 0, 0 num = 1 for layer in range(n // 2): for i in range(layer, n - layer - 1): spiral[layer][i] = num num += 1 for i in range(layer, n - layer - 1): spiral[i][n - layer - 1] = num num += 1 for i in range(n - layer - 1, layer, -1): spiral[n - layer - 1][i] = num num += 1 for i in range(n - layer - 1, layer, -1): spiral[i][layer] = num num += 1 if n % 2 == 1: spiral[n // 2][n // 2] = num return spiral # Example usage n = 5 spiral_array = generate_square_spiral_array(n) for row in spiral_array: print(row) 
  5. Creating an outward spiral array in Python:

    • Description: Implement an outward spiral array in Python where the numbers increase outward from the center.
    def generate_outward_spiral_array(n): spiral = [[0] * n for _ in range(n)] row, col = n // 2, n // 2 num = 1 layer = 1 spiral[row][col] = num num += 1 while layer <= n // 2: for i in range(2 * layer - 1): col += 1 spiral[row][col] = num num += 1 for i in range(2 * layer): row -= 1 spiral[row][col] = num num += 1 for i in range(2 * layer): col -= 1 spiral[row][col] = num num += 1 for i in range(2 * layer): row += 1 spiral[row][col] = num num += 1 layer += 1 return spiral # Example usage n = 5 spiral_array = generate_outward_spiral_array(n) for row in spiral_array: print(row) 

More Tags

dbscan inno-setup sql-server-2014-express spiral intel-mkl iconbutton compatibility android-studio rest-client startup

More Python Questions

More Math Calculators

More Trees & Forestry Calculators

More Cat Calculators

More Other animals Calculators