0

I'm trying to make a spiral matrix, but when I tried to make the numbers appear one by one, it only shows the lines one by one.

Please help!

import numpy as np
from time import sleep

n = int(input("Width : "))
k = int(input("Space : "))

a = np.zeros((n,n))
print(a/n)

i = 0 #i line
j = 0 #j column
it = -1 #it upper line
id = n #id downer line
jt = -1 #jt left column
jp = n #jp right column
x = k #x starter number

while j < jp:

    while j < jp:
        a[i][j] = x
        x += k
        j +=1
    it +=1
    i=it+1
    j=jp-1

    while i< id:
        a[i][j] = x
        x += k
        i +=1
    jp -=1
    j=jp-1
    i=id-1

    while j > jt:
        a[i][j] = x
        x += k
        j -=1
    id -=1
    i=id-1
    j=jt+1

    while i>it:
        a[i][j] = x
        x += k    
        i -=1
    jt +=1
    i=it+1
    j=jt+1

for x in a:
    print(x)
    sleep(0.1)

Here's an example:

Here's an example

Each number is suppose to appear one by one.

(I'm just putting this here so I can post this since I need to add more details)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

Not quite trivial.

I found a solution using an empty character array and cursor-manipulation. This should result in the desired output:

# replace your last "for x in a" loop by the following

def print_array(arr, block_size=4):
    """
    prints a 2-D numpy array in a nicer format
    """
    for a in arr:
        for elem in a:
            print(elem.rjust(block_size), end="")
        print(end="\n")

# empty matrix which gets filled with rising numbers
matrix_to_be_filled = np.chararray(a.shape, itemsize=3, unicode=True)

for _ in range(a.size):
    # find position of minimum
    row, col = np.unravel_index(a.argmin(), a.shape)
    # add minimum at correct position
    matrix_to_be_filled[row, col] = f'{a.min():.0f}'
    # write partially filled matrix
    print_array(matrix_to_be_filled)
    # delete old minimum in a-matrix
    a[row, col] = a.max()+1
    sleep(0.1)
    # bring cursor back up except for last element
    if _ < a.size-1:
        print('\033[F'*(matrix_to_be_filled.shape[0]+1))

If you are using pycharm, you need to edit the run/debug configuration and active "emulate terminal in output console" to make the special "move up" character work

enter image description here

Christian Karcher
  • 2,533
  • 1
  • 12
  • 17
  • Uh thx for answering, but i have ran into some problems.The problem is that i need to have only one array on the screen at a time. My teacher says this is needed.So if you dont mind, can you find a way to make it appear in only 1 array. – AntiDestroyer Feb 24 '22 at 09:42
  • Not sure what you mean by "array" - this code plots the full matrix containing a single element, then deletes it, replots it with 2 elements, etc. Please elaborate. – Christian Karcher Feb 24 '22 at 09:58
  • It means that all of that happen inside a single matrix, your method is printing the matrix with each time adding a element .I only want one appearing in the terminal. – AntiDestroyer Feb 24 '22 at 10:05
  • So is this possible? – AntiDestroyer Feb 24 '22 at 10:10
  • I am afraid that your terminal then is not supporting the special \033[F character. I am not aware of a different way to do that. Maybe look for a way to move the cursor upwards in your specific terminal. – Christian Karcher Feb 24 '22 at 14:01