打印复杂的菱形图案

2024-04-25 15:22:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试建立一个钻石模式的钻石模式。让我来给你解释一下。 目前我有一个函数,可以打印一个菱形图案,输入大小

def diamond(n,space=' '): 

    for i in range(n):
        print(space*(n-1-i) + "* "*(i+1))            
    for l in range(n-1,0,-1):   
        print(space*(n-l) + "* "*(l))

diamond(2)

我的输出将是2号钻石

 * 
* * 
 * 

现在我真正想要的是用这个钻石图案替换每颗星星。 期望的输出看起来像

enter image description here

应该能够在所有尺寸上做到这一点,其中该图案的每个星形都被整个图案替换

多谢各位


Tags: 函数infor尺寸def模式rangespace
3条回答

使用此递归函数,您可以在屏幕的任何位置打印复杂的菱形

import sys

def draw_diamond(x, y, diamond_size):
    # x and y represent the upper-left corner of the diamond to be drawn
    # diamond_size must be a power of 3
    if diamond_size == 1:
        sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (x, y, "*"))
        sys.stdout.flush()
        return
    subdiamond_size = diamond_size / 3
    draw_diamond(x + subdiamond_size, y, subdiamond_size)
    draw_diamond(x, y + subdiamond_size, subdiamond_size)
    draw_diamond(x + 2*subdiamond_size, y + subdiamond_size, subdiamond_size)
    draw_diamond(x + subdiamond_size, y + 2*subdiamond_size, subdiamond_size)

例如,draw_diamond(5, 100, 27)打印尺寸为27的菱形,其左上角距离终端顶部5个字符,距离终端左侧100个字符:

             *
            * *
             *
          *     *
         * *   * *
          *     *
             *
            * *
             *
    *                 *
   * *               * *
    *                 *
 *     *           *     *
* *   * *         * *   * *
 *     *           *     *
    *                 *
   * *               * *
    *                 *
             *
            * *
             *
          *     *
         * *   * *
          *     *
             *
            * *
             *

您可以使用diamond_size=9获得所需的输出。可能的尺寸为1、3、9、27、81、243等

可以通过diamond_size=3**2获得所需的输出:

    *
   * *
    *
 *     *
* *   * *
 *     *
    *
   * *
    *

您可以使用np.array作为网格,并将“*”与位置[i][j]放在一起。在这段代码中,制作空数组,然后检查*的位置,最后设置外部菱形位置。可能有更好的代码,但这仍然有效

import numpy as np

n = 2

k = (2*n - 1)

# make empty array, set
# a is used for single diamond. Not necessary.
a = np.array([[' ']*k for t in range(k)])
# set of * position in array
b = set()
c = np.array([[' ']*k**2 for t in range(k**2)])

# check positions in array of single diamond 
for i in range(n**2):
    for i in range(n):
        i_reverce = k - i -1
        for j in range(i+1):
            y = n-i-1+2*j
            a[i][y] = '*'
            a[i_reverce][y] = '*'
            b.add((i,y))
            b.add((i_reverce,y))

# print(a) # use if you want to check a

positions = list(b)

# positions of * in diamond of diamond
for pos1 in positions:
    for pos2 in positions:
        x = pos1[0] * k + pos2[0]
        y = pos1[1] * k + pos2[1]
        c[x][y] = '*'

for row in c:
    print(''.join(row))

对于n=2

    *    
   * *   
    *    
 *     * 
* *   * *
 *     * 
    *    
   * *   
    *    

对于n=3

            *            
           * *           
          * * *          
           * *           
            *            
       *         *       
      * *       * *      
     * * *     * * *     
      * *       * *      
       *         *       
  *         *         *  
 * *       * *       * * 
* * *     * * *     * * *
 * *       * *       * * 
  *         *         *  
       *         *       
      * *       * *      
     * * *     * * *     
      * *       * *      
       *         *       
            *            
           * *           
          * * *          
           * *           
            *         

光标前进后,不能转到上一行。相反,你可以用不同的方法来处理这个问题。如果您有一组要打印的单词,例如

words = ['Hello', 'World']

您可以按如下方式n次打印它们:

for word in words:
    print(word * n)

对于n = 2,这给出了

HelloHello
WorldWorld

相关问题 更多 >