使用字母打印 rangoli 图案的 Python 程序
假设我们有一个数字n。我们必须创建nxn大小的字母rangoli。n必须在1和26之间,当n为26时,它将从a开始到z结束。
所以,如果输入像5,那么输出将是
--------e-------- ------e-d-e------ ----e-d-c-d-e---- --e-d-c-b-c-d-e-- e-d-c-b-a-b-c-d-e --e-d-c-b-c-d-e-- ----e-d-c-d-e---- ------e-d-e------ --------e--------
示例
让我们看下面的实现来更好地理解
def solve(n): for i in range(n-1,-1,-1): for j in range(i): print(end="--") for j in range(n-1,i,-1): print(chr(j+97),end="-") for j in range(i,n): if j != n-1: print(chr(j+97),end="-") else: print(chr(j+97),end="") for j in range(2*i): print(end="-") print() for i in range(1,n): for j in range(i): print(end="--") for j in range(n-1,i,-1): print(chr(j+97),end="-") for j in range(i,n): if j != n-1: print(chr(j+97),end="-") else: print(chr(j+97),end="") for j in range(2*i): print(end="-") print() n = 8 solve(n)
输入
8输出结果
--------------h-------------- ------------h-g-h------------ ----------h-g-f-g-h---------- --------h-g-f-e-f-g-h-------- ------h-g-f-e-d-e-f-g-h------ ----h-g-f-e-d-c-d-e-f-g-h---- --h-g-f-e-d-c-b-c-d-e-f-g-h-- h-g-f-e-d-c-b-a-b-c-d-e-f-g-h --h-g-f-e-d-c-b-c-d-e-f-g-h-- ----h-g-f-e-d-c-d-e-f-g-h---- ------h-g-f-e-d-e-f-g-h------ --------h-g-f-e-f-g-h-------- ----------h-g-f-g-h---------- ------------h-g-h------------ --------------h--------------