-
Notifications
You must be signed in to change notification settings - Fork 1
/
Creating_an_array_of_spiral_form_using_lists_in_Python.py
44 lines (38 loc) · 1.64 KB
/
Creating_an_array_of_spiral_form_using_lists_in_Python.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File: Creating_an_array_of_spiral_form_using_lists_in_Python.py
# Description: Graphic representation of program in Python
# Environment: PyCharm and Anaconda environment
#
# MIT License
# Copyright (c) 2018 Valentyn N Sichkar
# github.com/sichkar-valentyn
#
# Reference to:
# [1] Valentyn N Sichkar. Using lists create an array of spiral form in Python // GitHub platform [Electronic resource]. URL: https://github.com/sichkar-valentyn/Creating_an_array_of_spiral_form_using_lists_in_Python (date of access: XX.XX.XXXX)
# Creating an array of n by n size, filled with numbers from 1 to n ** 2 by spiral direction
n = int(input()) # Size of the array
a = [[0 for j in range(n)] for i in range(n)] # Creating an 0-filled 2-dimensional list
# Filling the 2-dimensional list with numbers in spiral direction
m = 1
c = n // 2 # The number of circules
for i in range(c): # Going through circules
for h1 in range(i, n - 1 - i): # Horizontal upper line of the spiral
a[i][h1] = m
m += 1
for v1 in range(i, n - 1 - i): # Vertical right line of the spiral
a[v1][n - 1 - i] = m
m += 1
for h2 in range(n - 1 - i, i, -1): # Horizontal bottom line of the spiral
a[n - 1 - i][h2] = m
m += 1
for v2 in range(n - 1 - i, i, -1): # Vertical left line of the spiral
a[v2][i] = m
m += 1
# Assigning the central value to the array if its size is odd number
if n % 2 != 0:
a[n // 2][n // 2] = m
# Showing the result in form of array
for i in range(n):
if i != 0 or i != n - 1:
print()
for j in range(n):
print(a[i][j], end=' ')