Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. 1
  2. 2 1 2
  3. 3 2 1 2 3
  4.  
  5. p = eval(input("Enter the number of lines: "))
  6.  
  7. num = 0
  8. y = 1
  9.  
  10. while num < p:
  11. y = num
  12.  
  13. line = str(y+1) + " "
  14.  
  15. while y >= 1:
  16.  
  17. y -= 1
  18. line += str(y+1) + " "
  19.  
  20. while y < num:
  21.  
  22. line += str(y + 2) + " "
  23.  
  24. y +=1
  25.  
  26. print(format(line, "^80s"))
  27.  
  28. num +=1
  29.  
  30. # When n=3, returns [3, 2, 1, 2, 3]
  31. def row(n):
  32. return list(reversed(range(2, n+1))) + list(range(1, n+1))
  33.  
  34. for n in range(1, 5):
  35. print(row(n))
  36.  
  37. [1]
  38. [2, 1, 2]
  39. [3, 2, 1, 2, 3]
  40. [4, 3, 2, 1, 2, 3, 4]
  41.  
  42. def formatted_row(n, num_rows):
  43. padding = ' ' * (num_rows-n)
  44. return padding + ''.join(["%3s" % x for x in row(n)])
  45.  
  46. # When n=3, returns [3, 2, 1, 2, 3]
  47. def row(n):
  48. return list(reversed(range(2, n+1))) + list(range(1, n+1))
  49.  
  50. def formatted_row(n, num_rows):
  51. padding = ' ' * (num_rows-n)
  52. return padding + ''.join(["%3s" % x for x in row(n)])
  53.  
  54. num_lines = eval(input("Enter the number of lines: "))
  55. for n in range(1, num_lines+1):
  56. print(formatted_row(n, num_lines))
  57.  
  58. f(1, -n) f(1, -n+1) ... f(1, -1) f(1, 0) f(1, 1) ... f(1, n)
  59. f(2, -n) ... f(2, -1) f(2, 0) f(2, 1) ... f(2, n)
  60. ... ... ...
  61. f(n, -n) ... f(n, 0) ... f(n, n)
  62.  
  63. def f(line, place):
  64. # number is just an offset from center + 1
  65. number = abs(place) + 1
  66. # don't display numbers greater than line number
  67. return str(number) if number <= line else ' '
  68.  
  69. def pyramid(height):
  70. # same indexes for each line, so we'll reuse them
  71. places = range(-height+1, height)
  72. for line in range(1, height+1):
  73. print ' '.join(f(line, place) for place in places)
  74.  
  75. return str(number) if number <= line else ' ' * len(str(number))
  76.  
  77. def pyramid(height):
  78. FMT = '%3s'
  79. def row(r):
  80. right_half = [FMT % num for num in range(2, 2 + r)] +
  81. [FMT % ' ' for spc in range(2 + r, 1 + height)]
  82. return ''.join(list(reversed(right_half)) + # Mirrored right half
  83. [FMT % 1] + # Center
  84. right_half) # Right half
  85. return "n".join([row(r) for r in range(height)])
  86.  
  87. print(pyramid(p))
  88.  
  89. lines = input("enter the number of lines")
  90.  
  91. lines = int(lines)
  92.  
  93. count = 1
  94.  
  95.  
  96.  
  97. while count < lines+1:
  98.  
  99. padding = ' '*(lines-count)
  100.  
  101. digits = list(range(1,count+1))
  102.  
  103.  
  104. output = padding + str(digits[::-1]) + str(digits[1:])
  105. print(output)
  106. count+=1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement