python - 如何在python中显示以下内容?
问题描述
问题陈述是:
编写一个函数
create_grid
,接收正方形的边数和正方形边上的星数,然后将正方形网格作为字符串返回。用换行符 (\n
) 分隔每一行。
它应该显示以下内容:
assert_equal(
create_grid(1, 1),
"""+ - +
| * |
+ - +
""")
assert_equal(
create_grid(2, 4),
"""+ - - - - + - - - - +
| * * * * | |
| * * * * | |
| * * * * | |
| * * * * | |
+ - - - - + - - - - +
| | * * * * |
| | * * * * |
| | * * * * |
| | * * * * |
+ - - - - + - - - - +
""")
assert_equal(
create_grid(4, 2),
"""+ - - + - - + - - + - - +
| * * | | * * | |
| * * | | * * | |
+ - - + - - + - - + - - +
| | * * | | * * |
| | * * | | * * |
+ - - + - - + - - + - - +
| * * | | * * | |
| * * | | * * | |
+ - - + - - + - - + - - +
| | * * | | * * |
| | * * | | * * |
+ - - + - - + - - + - - +
""")
我只能想出以下代码:
def create_grid(num_squares, num_stars):
# YOUR CODE HERE
#raise NotImplementedError()
#'+ - +\n
#| * |\n
#+ - +\n'
holder = ""
a = '+ ' + "- "*num_stars + "+\n"
b = '| ' + '* '*num_stars + '|\n'
for squaresdown in range(num_squares):
holder+=a
for row in range(num_stars):
holder+=b
holder+=a
print(holder)
create_grid(3,5)
显示网格
+ - - - - - +
| * * * * * |
| * * * * * |
| * * * * * |
| * * * * * |
| * * * * * |
+ - - - - - +
| * * * * * |
| * * * * * |
| * * * * * |
| * * * * * |
| * * * * * |
+ - - - - - +
| * * * * * |
| * * * * * |
| * * * * * |
| * * * * * |
| * * * * * |
+ - - - - - +
我如何实现一种方法:
- 忽略每隔一个盒子放星星
- 生成新列?
解决方案
考虑这种方法(更新):
def create_grid(num_squares, num_stars):
holder = ""
a = '+ ' + ("- " * num_stars + "+ " + "- " * num_stars + '+ ')*(
num_squares // 2) + ("- " * num_stars + "+ ") * (num_squares % 2)
b = ('| ' + '* ' * num_stars + '| ' + " " * num_stars)*(num_squares//2) \
+ ('| ' + '* ' * num_stars) * (num_squares % 2)
for column in range(num_squares):
holder += a + "\n"
for row in range(num_stars):
holder += (b + '|' if (column % 2) == 0 else '|' + b[::-1]) + "\n"
holder += a + "\n"
print(holder)
因此,一些关于它如何打印的说明性示例:
create_grid(1,1)
:
+ - +
| * |
+ - +
create_grid(1,2)
:
+ - - +
| * * |
| * * |
+ - - +
create_grid(1,3)
:
+ - - - +
| * * * |
| * * * |
| * * * |
+ - - - +
create_grid(2,1)
:
+ - + - +
| * | |
+ - + - +
| | * |
+ - + - +
create_grid(2,2)
:
+ - - + - - +
| * * | |
| * * | |
+ - - + - - +
| | * * |
| | * * |
+ - - + - - +
create_grid(2,3)
:
+ - - - + - - - +
| * * * | |
| * * * | |
| * * * | |
+ - - - + - - - +
| | * * * |
| | * * * |
| | * * * |
+ - - - + - - - +
create_grid(3,1)
:
+ - + - + - +
| * | | * |
+ - + - + - +
| * | | * |
+ - + - + - +
| * | | * |
+ - + - + - +
create_grid(3,2)
:
+ - - + - - + - - +
| * * | | * * |
| * * | | * * |
+ - - + - - + - - +
| * * | | * * |
| * * | | * * |
+ - - + - - + - - +
| * * | | * * |
| * * | | * * |
+ - - + - - + - - +
create_grid(3,3)
:
+ - - - + - - - + - - - +
| * * * | | * * * |
| * * * | | * * * |
| * * * | | * * * |
+ - - - + - - - + - - - +
| * * * | | * * * |
| * * * | | * * * |
| * * * | | * * * |
+ - - - + - - - + - - - +
| * * * | | * * * |
| * * * | | * * * |
| * * * | | * * * |
+ - - - + - - - + - - - +
推荐阅读
- json - 使用 swiftyJSON 的低速工作
- c# - 为什么编译器在使用这个 hack 时没有给出任何错误或警告?
- html - Complex grid with images
- c# - 从循环中调用时,C# async await 方法是否并行运行?
- aws-appsync - Exception in generateClasses when building AppSync project in Debug but not Release
- python - python type.cast 在 Python 中有什么作用?
- wordpress - 带有叶子类别的 Wordpress 存档永久链接
- javascript - One Click Deploy Solution for my webapp
- string - string[] 包括 EFCore2.1
- r - R中的自定义Keras损失函数,带有case_when函数