螺旋矩阵出现动画

发布于 2025-01-09 13:05:13 字数 1065 浏览 0 评论 0原文

我正在尝试制作一个螺旋矩阵,但是当我尝试让数字一一出现时,它只一一显示线条。

请帮忙!

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

每个数字都应该一一出现。

(我只是将其放在这里,以便我可以发布此内容,因为我需要添加更多详细信息)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熊抱啵儿 2025-01-16 13:05:13

不是很微不足道。

我找到了一个使用空字符数组和光标操作的解决方案。
这应该会产生所需的输出:

# 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))

如果您使用的是 pycharm,则需要编辑 运行/调试配置并激活“在输出控制台中模拟终端”以使特殊的“向上移动”字符起作用

在此处输入图像描述

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文