[英]Print a 'X' pattern of '*'
I'm trying to print this pattern in Python:我正在尝试在 Python 中打印此模式:
* *
* *
* *
*
* *
* *
* *
And came up with this code:并想出了这个代码:
size = 7
inner_size = size - 3
space=0
for i in range(inner_size):
print (' ' * space + '*' + ' ' * inner_size + '*')
inner_size -=2
space +=1
print " *"
t_size=7
t_inner_size = 0
space=3
for i in range(inner_size):
print (' ' * space + '*' + ' ' * inner_size + '*')
inner_size +=2
space -=1
But, it prints this :但是,它打印了这个:
* *
* *
**
**
*
How to print the required pattern?如何打印所需的图案? Is it possible to do this in one loop?
是否可以在一个循环中完成此操作?
Here's one way to do it:这是一种方法:
def pattern(size):
for i in range(size):
print("".join("*" if j == i or size - 1 - j == i else " "
for j in range(size)))
Details: i
is the row index and j
is the column index (both zero-based).详细信息:
i
是行索引, j
是列索引(均从零开始)。 The j == i
condition generates a diagonal from upper-left to lower-right. j == i
条件生成从左上角到右下角的对角线。 The size - 1 - j == i
condition generates a diagonal from upper-right to lower-left. size - 1 - j == i
条件生成从右上角到左下角的对角线。 Together, they produce the X shape.它们一起产生了 X 形状。
When called with a size of 7:当调用大小为 7 时:
pattern(7)
It prints:它打印:
* *
* *
* *
*
* *
* *
* *
Note that it works with both even and odd sizes.请注意,它适用于偶数和奇数大小。 In the case of an even size, the resulting X will contain a 2x2 block of stars in the center.
在大小均匀的情况下,生成的 X 将在中心包含一个 2x2 的星星块。
Just another way of solving the problem:解决问题的另一种方法:
for i in range(5):
for j in range(5):
if ((i==j) or j==(5-i-1)):
print " *",
else:
print " ",
print ""
~ ~
I have used ideas from your existing code in the question and made a few tweaks to do it in a single loop with simple, crystal clear code:我在问题中使用了您现有代码中的想法,并使用简单、清晰的代码在单个循环中进行了一些调整:
def xprint(size):
i,j = 0,size - 1
while j >= 0 and i < size:
initial_spaces = ' '*min(i,j)
middle_spaces = ' '*(abs(i - j) - 1)
final_spaces = ' '*(size - 1 - max(i,j))
if j == i:
print initial_spaces + '*' + final_spaces
else:
print initial_spaces + '*' + middle_spaces + '*' + final_spaces
i += 1
j -= 1
xprint(7)
It prints out:它打印出:
* * * * * * * * * * * * *
A linear-time straightforward way of doing it.一种线性时间简单的方法。
You asked if it can be done with a ' single loop ' .您问是否可以通过“单循环”来完成。 So there ya go:
所以你去:
n = 3
for i in range(-n, n + 1):
print('{}*{}{}'.format(
(n + 1 - abs(i)) * ' ',
(2 * abs(i) - 1) * ' ',
'' if i == 0 else '*'))
Explanation:说明:
For any given line, we need 2 sets of spaces.对于任何给定的行,我们需要 2 组空格。 Spaces before the first
*
, and spaces after the first *
.第一个
*
之前的空格和第一个*
之后的空格。 This we are formatting our string as follows: {}*{}
我们将字符串格式化如下:
{}*{}
Now we add an extra placeholder, because every line has 2x '*', except for the middle line.现在我们添加一个额外的占位符,因为每一行都有 2x '*',除了中间的一行。 We're counting in a fashion similar to
-3 -2 -1 0 1 2 3
, there middle line has index 0
.我们以类似于
-3 -2 -1 0 1 2 3
的方式进行计数,中间线的索引为0
。 Thus now our string format has become {}*{}{}
.因此现在我们的字符串格式变成了
{}*{}{}
。
Now, space before the first *
need n + 1 - abs(i)
no.现在,第一个
*
之前的空间需要n + 1 - abs(i)
否。 of spaces.的空间。
and spaces after the first *
need n + 1 - abs(i)
no of spaces.第一个
*
之后的空格需要n + 1 - abs(i)
没有空格。
Output (n=3):输出(n=3):
* *
* *
* *
*
* *
* *
* *
Output (n=5):输出(n=5):
* *
* *
* *
* *
* *
*
* *
* *
* *
* *
* *
Although people are using the entire pattern size as input, I think it's more intuitive to use the recurring size as input.虽然人们使用整个图案尺寸作为输入,但我认为使用重复尺寸作为输入更直观。 So just change the value of
n
as desired.因此,只需根据需要更改
n
的值。
PS: This is not the most elegant, nor is it the most legible solution. PS:这不是最优雅的,也不是最清晰的解决方案。 But it works in a single loop.
但它在单个循环中工作。
You can also do it like this:你也可以这样做:
pattern_size = 7
for t in range(pattern_size):
pattern = list(" " * pattern_size)
pattern[t] = "*"
pattern[-(t+1)] = "*"
print("".join(pattern))
Output:输出:
* *
* *
* *
*
* *
* *
* *
Another code for the above pattern上述模式的另一个代码
i = 0
j = 6
for row in range(7):
for column in range(7):
if(column == i or column == j):
print("*",end="")
else:
print(" ",end="")
print("\n")
i += 1
j -= 1
Results in:结果:
* *
* *
* *
*
* *
* *
* *
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.