@anonymous/

PreemptiveQuaintRedundantcode

Python

No description

fork
loading
Files
  • main.py
main.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from itertools import cycle


def spiral(n):
    def fill_right(matrix):
        nonlocal x, y, current_n
        row = matrix[y]
        for _ in range(move_length):
            matrix[y][x] = current_n
            print(row)
            x += 1
            current_n += 1
        x -= 1
        y += 1

    def fill_down(matrix):
        nonlocal x, y, current_n
        for _ in range(move_length):
            matrix[y][x] = current_n
            print([row[x] for row in matrix])
            y += 1
            current_n += 1
        y -= 1
        x -= 1

    def fill_left(matrix):
        nonlocal x, y, current_n
        row = matrix[y]
        for _ in range(move_length):
            matrix[y][x] = current_n
            print(row)
            x -= 1
            current_n += 1
        x += 1
        y -= 1

    def fill_top(matrix):
        nonlocal x, y, current_n
        for _ in range(move_length):
            matrix[y][x] = current_n
            print([row[x] for row in matrix])
            y -= 1
            current_n += 1
        y += 1
        x += 1

    fill_to = {
        "right": fill_right,
        "down": fill_down,
        "left": fill_left,
        "top": fill_top
    }
    directions = cycle(['right', 'down', 'left', 'top'])
    matrix = [[None for i in range(n)] for i in range(n)]
    x = y = 0
    counter = current_n = 1
    move_length = n

    while move_length > 0:
        direction = next(directions)
        print('filling ', direction)
        fill_to[direction](matrix)
        print(y, x)
        counter += 1
        if counter > 1:
            counter = 0
            move_length -= 1

    column_width = len(str(current_n - 1))
    for row in matrix:
        print(' '.join([f'{i:^{column_width}}' for i in row]), end='\n\n')
spiral(3)
spiral(6)
spiral(10)