Skip to content

Update 21.py #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 18 additions & 52 deletions src/ch03/21.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,19 @@
n = int(input("> "))

max_height = 4 + 3 * n # 3 (foot) + n (wall) + 2 * n + 1 (roof)

for row in range(max_height, 0, -1):
row_output = ""
for house in list(range(1, n + 1)) + list(range(n - 1, 0, -1)):
house_height = 4 + 3 * house
house_width = 1 + house * 4

# Margin between houses
if row_output:
row_output += " "

# Above the house
if row > house_height:
row_output += " " * house_width
continue

# Roof
if row == house_height:
row_output += " " * house * 2 + "*" + " " * house * 2
continue
elif row > 3 + house:
roof_margin = row - 4 - house
roof_width = house_width - 2 * roof_margin - 2
row_output += "{0}/{1}\\{0}".format(" " * roof_margin, "-" * roof_width)
continue

# Wall
if row > 3:
wall_padding = house
door_width = house_width - 2 - 2 * wall_padding

# Door
door = ""
if door_width < 2 or row > 3 + house:
door = " " * door_width
elif row == 3 + house:
door = " {} ".format("-" * (door_width - 2))
else:
door = "|{}|".format(" " * (door_width - 2))

row_output += "|{0}{1}{0}|".format(" " * wall_padding, door)
continue

# Foot
if row == 3:
row_output += "|{}|".format("=" * (house_width - 2))
n=int(input("> "))
for i in range(3*n+1,-3,-1):
for j in range(2*n-1):
h=n-abs(n-j-1)
if i==-1 or i==-2:
print("II"+" "*(4*h-3)+"II",end=" ")
elif i==0:
print("|"+"="*(4*h-1)+"|",end=" ")
elif i<h:
print("|"+" "*h,"|"+" "*(2*h-3)+"|" if h>1 else " "," "*h+"|",sep="",end=" ")
elif i==h:
print("|"+" "*h," "+"_"*(2*h-3)+" " if h>1 else " "," "*h+"|",sep="",end=" ")
elif i<3*h+1:
print(" "*(i-h-1)+"/"+"-"*(6*h-2*i+1)+"\\"+" "*(i-h-1),end=" ")
elif i==3*h+1:
print(" "*2*h+"*"+" "*2*h,end=" ")
else:
row_output += "II{}II".format(" " * (house_width - 4))

print(row_output)
print(" "*(4*h+1),end=" ")
print()