英文:
A function that prints an xo square
问题
我有一个学校项目。我有点困难。我想在Python中实现一个2D列表,然后使用while循环来更新变量并索引列表以重新分配索引为x。详细信息如下
进入main.py编写一个名为
xo
的函数,该函数接受一个整数大小作为参数,并打印一个大小为size的字符方阵,其中所有字符都是“o”,除了在方阵的角上绘制了“x”字符的图案。
在第一行,第一个和最后一个字符是“x”;
在第二行,第二个和倒数第二个字符是“x”;以此类推。
例如,调用
xo(5)
应该产生以下输出:
xooox
oxoxo
ooxoo
oxoxo
xooox
我的代码到目前为止是
def xo(num):
square = []
section = ["o" * num]
for i in range(1, num + 1):
square.append(section)
for row in square:
print("".join(row))
xo(5)
它垂直打印了:
ooooo
ooooo
ooooo
ooooo
ooooo
英文:
I have a school proejct. I'm having a bit of trouble. I was thinking of implementing a 2D lists in python and then using a while loop to update the variables and index the lists to reassign the index to an x. The details are
> Go to main.py write a function called xo
that accepts an integer size as a parameter and prints a square of size by size characters,
where all characters are "o" except that an “x” pattern of "x" characters has been drawn from the corners of the square.
On the first line, the first and last characters are "x";
on the second line, the second and second-from-last characters are "x"; and so on.
> For example, the call of xo(5) should produce the following outputs:
xooox
oxoxo
ooxoo
oxoxo
xooox
my code so far was
def xo(num):
square=[]
section=["o"*num]
for i in range(1,num+1):
square.append(section)
for row in square:
print("".join(row))
xo(5)
and it prints
ooooo
ooooo
ooooo
ooooo
ooooo
vertically
答案1
得分: 3
这个练习可以看作是一个数学问题,然后再尝试将其转化为代码。我们可以从打印每个方块为“o”的代码开始(就像你已经做过的那样),但如果每次打印一个“o”时,我们有一个特定的网格坐标(x
和 y
)可以使用,那么下一步会更容易:
def xo(n):
for y in range(n):
for x in range(n):
print("o", end="")
print()
xo(5)
这将得到输出:
ooooo
ooooo
ooooo
ooooo
ooooo
我们知道我们希望其中一些“o”变成“x”,所以让我们看看当我们希望发生这种情况时,代码中的x
和y
值会是什么样子:
y x
0: 0, 4
1: 1, 3
2: 2, 2
3: 1, 3
4: 0, 4
这就是数学问题 - 我们如何找到一种数学方式来表达x
和y
之间的关系?
我们可以看到,在每种情况下,我们希望在x == y
或x == 4 - y
时更改我们打印的内容。记住在这种情况下n == 5
,我们可以改变我们的代码如下:
def xo(n):
for y in range(n):
for x in range(n):
print("x" if x in (y, n - 1 - y) else "o", end="")
print()
对于xo(5)
,输出如下:
xooox
oxoxo
ooxoo
oxoxo
xooox
由于我们的代码是用n
来表示,而不是在那里硬编码数字4
作为一个魔术数,我们也可以运行,例如,xo(20)
,得到:
xoooooooooooooooooox
oxooooooooooooooooxo
ooxooooooooooooooxoo
oooxooooooooooooxooo
ooooxooooooooooxoooo
oooooxooooooooxooooo
ooooooxooooooxoooooo
oooooooxooooxooooooo
ooooooooxooxoooooooo
oooooooooxxooooooooo
oooooooooxxooooooooo
ooooooooxooxoooooooo
oooooooxooooxooooooo
ooooooxooooooxoooooo
oooooxooooooooxooooo
ooooxooooooooooxoooo
oooxooooooooooooxooo
ooxooooooooooooooxoo
oxooooooooooooooooxo
xoooooooooooooooooox
英文:
It helps to look at this type of exercise as a math problem before trying to turn it into code. We can start with code that prints every square as an "o" (as you've already done), but it'll be easier to take it to the next step if each time we print an "o" we have a particular coordinate on the grid (x
and y
) to work with:
def xo(n):
for y in range(n):
for x in range(n):
print("o", end="")
print()
xo(5)
This gets us the output:
ooooo
ooooo
ooooo
ooooo
ooooo
We know we want some of those o
s to be x
s, so let's look at what the x
and y
values in our code will look like when we want that to happen:
y x
0: 0, 4
1: 1, 3
2: 2, 2
3: 1, 3
4: 0, 4
This is the math problem -- how do we come up with a mathematical way to express that relationship between x
and y
?
We can see that in every case we want to change what we print when x == y
, or x == 4 - y
. Remembering that in this case n == 5
, we might change our code to do:
def xo(n):
for y in range(n):
for x in range(n):
print("x" if x in (y, n - 1 - y) else "o", end="")
print()
which for xo(5)
gets us:
xooox
oxoxo
ooxoo
oxoxo
xooox
and since we wrote our code in terms of n
instead of hardcoding 4
in there as a magic number, we can also run, say, xo(20)
and get:
xoooooooooooooooooox
oxooooooooooooooooxo
ooxooooooooooooooxoo
oooxooooooooooooxooo
ooooxooooooooooxoooo
oooooxooooooooxooooo
ooooooxooooooxoooooo
oooooooxooooxooooooo
ooooooooxooxoooooooo
oooooooooxxooooooooo
oooooooooxxooooooooo
ooooooooxooxoooooooo
oooooooxooooxooooooo
ooooooxooooooxoooooo
oooooxooooooooxooooo
ooooxooooooooooxoooo
oooxooooooooooooxooo
ooxooooooooooooooxoo
oxooooooooooooooooxo
xoooooooooooooooooox
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论