Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1
- 2 1 2
- 3 2 1 2 3
- p = eval(input("Enter the number of lines: "))
- num = 0
- y = 1
- while num < p:
- y = num
- line = str(y+1) + " "
- while y >= 1:
- y -= 1
- line += str(y+1) + " "
- while y < num:
- line += str(y + 2) + " "
- y +=1
- print(format(line, "^80s"))
- num +=1
- # When n=3, returns [3, 2, 1, 2, 3]
- def row(n):
- return list(reversed(range(2, n+1))) + list(range(1, n+1))
- for n in range(1, 5):
- print(row(n))
- [1]
- [2, 1, 2]
- [3, 2, 1, 2, 3]
- [4, 3, 2, 1, 2, 3, 4]
- def formatted_row(n, num_rows):
- padding = ' ' * (num_rows-n)
- return padding + ''.join(["%3s" % x for x in row(n)])
- # When n=3, returns [3, 2, 1, 2, 3]
- def row(n):
- return list(reversed(range(2, n+1))) + list(range(1, n+1))
- def formatted_row(n, num_rows):
- padding = ' ' * (num_rows-n)
- return padding + ''.join(["%3s" % x for x in row(n)])
- num_lines = eval(input("Enter the number of lines: "))
- for n in range(1, num_lines+1):
- print(formatted_row(n, num_lines))
- f(1, -n) f(1, -n+1) ... f(1, -1) f(1, 0) f(1, 1) ... f(1, n)
- f(2, -n) ... f(2, -1) f(2, 0) f(2, 1) ... f(2, n)
- ... ... ...
- f(n, -n) ... f(n, 0) ... f(n, n)
- def f(line, place):
- # number is just an offset from center + 1
- number = abs(place) + 1
- # don't display numbers greater than line number
- return str(number) if number <= line else ' '
- def pyramid(height):
- # same indexes for each line, so we'll reuse them
- places = range(-height+1, height)
- for line in range(1, height+1):
- print ' '.join(f(line, place) for place in places)
- return str(number) if number <= line else ' ' * len(str(number))
- def pyramid(height):
- FMT = '%3s'
- def row(r):
- right_half = [FMT % num for num in range(2, 2 + r)] +
- [FMT % ' ' for spc in range(2 + r, 1 + height)]
- return ''.join(list(reversed(right_half)) + # Mirrored right half
- [FMT % 1] + # Center
- right_half) # Right half
- return "n".join([row(r) for r in range(height)])
- print(pyramid(p))
- lines = input("enter the number of lines")
- lines = int(lines)
- count = 1
- while count < lines+1:
- padding = ' '*(lines-count)
- digits = list(range(1,count+1))
- output = padding + str(digits[::-1]) + str(digits[1:])
- print(output)
- count+=1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement