Reputation: 33
I am trying to write a function,that prints out rectangle in a terminal. My function accepts a single input parameter N and output a string with an ASCII art. This what the output should look like:
N = 2 N = 6
######## ####################
# # # #
# ** # # #
# ** # # #
# # # ****** #
######## # ****** #
# ****** #
N = 4 # ****** #
############## # ****** #
# # # ****** #
# # # #
# **** # # #
# **** # # #
# **** # ####################
# **** #
# #
# #
##############
This is what i have so far :
def flag(N):
if N % 2 == 0:
border_j = (N * 3) + 2
border_i = (N * 2) + 2
for i in range(border_i):
for j in range(border_j):
if i in [0, border_i - 1] or j in [0, border_j - 1]:
print('#', end='')
elif j == N + 1 and i == 1 + N / 2:
print('*', end='')
else:
print(' ', end='')
print()
else:
raise AssertionError
flag(2)
Output:
########
# #
# * #
# #
# #
########
And after this i've got a little confused. What should i do next?
Upvotes: 1
Views: 1922
Reputation: 11
A year late, but maybe this will be useful to someone else.
Code:
def rectangle(n) -> None:
x = n*3 + 2
y = n*2 + 2
[
print(''.join(i))
for i in
(
'#'*x
if i in (0,y-1)
else
(
f'#{" "*n}{"*"*n}{" "*n}#'
if i >= (n+2)/2 and i <= (3*n)/2
else
f'#{" "*(x-2)}#'
)
for i in range(y)
)
]
rectangle(0)
rectangle(1)
rectangle(2)
rectangle(4)
rectangle(6)
Output:
##
##
#####
# #
# #
#####
########
# #
# ** #
# ** #
# #
########
##############
# #
# #
# **** #
# **** #
# **** #
# **** #
# #
# #
##############
####################
# #
# #
# #
# ****** #
# ****** #
# ****** #
# ****** #
# ****** #
# ****** #
# #
# #
# #
####################
Upvotes: 1
Reputation: 41
you can do this with quite a bit fewer lines of code by breaking it down into which types of lines you want to print, and printing them in the right order. I propose the following code, it will work for any positive even number and the printout will be good up to the size of the window you're using :)
def flag(N):
if ((N%2 != 0) or (N <= 0)) :
return print("Error: Requires N%2 == 0 and N positive")
pound_sign = '#'*(3*N+2)
blank_line = "#"+(' '*(3*N))+'#'
grid_line = "#"+(' '*N)+('*'*N)+(' '*N)+"#"
print('{}{}{}{}{}'.format(pound_sign+'\n',\
(blank_line+'\n')*int(N/2),\
(grid_line+'\n')*N,\
(blank_line+'\n')*int(N/2),\
pound_sign))
Upvotes: 0