Home > database >  how to position an ascii isosceles pyramid
how to position an ascii isosceles pyramid

Time:10-04

this question have been answered thank you

CodePudding user response:

Where a is the height of the triangle and b is the height of the rectangle:

rh = int(input('Enter b: '))    # Rectangle's height.
th = int(input('Enter a: '))    # Triangle's height.
rw = rh * 2 - 1                 # Rectangle's width.
print()
# Limit size of triangle (rectangle >= triangle).
if th > rh:
    th = rh
# Generate the rectangle as a matrix of chars.
rectangle = [['b'] * rw, ['b'] * rw]
for _ in range(rh - 2):
    rectangle.insert(1, (['b']   ([' '] * (rw - 2))   ['b']))
# Calculate gaps between rectangle and triangle.
gap1 = int((rh - th) / 2)       # Empty lines over the triangle.
gap2 = gap1                     # Empty lines below the triangle.
if (rh - th) & 1:
    gap2 = gap1   1
# Insert the triangle into the rectangle.
width = 1                       # Current width of the triangle.
for row in range(gap1, len(rectangle) - gap2):
    start = int(rw / 2) - (row - gap1)
    for i in range(start, start   width):
        rectangle[row][i] = 'a'
    width  = 2
# Print the output matrix.
for row in rectangle:
    print(''.join(row))

Note that the size of the triangle is limited to the size of the rectangle! so, for an input like b=12 and a=8 (like in your image), this is the output:

bbbbbbbbbbbbbbbbbbbbbbb
b                     b
b          a          b
b         aaa         b
b        aaaaa        b
b       aaaaaaa       b
b      aaaaaaaaa      b
b     aaaaaaaaaaa     b
b    aaaaaaaaaaaaa    b
b   aaaaaaaaaaaaaaa   b
b                     b
bbbbbbbbbbbbbbbbbbbbbbb
  • Related