-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathprint_pyramid.rb
More file actions
53 lines (40 loc) · 1.38 KB
/
print_pyramid.rb
File metadata and controls
53 lines (40 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Method name: print_pyramid
# Input: a number n
# Returns: Nothing
# Prints: a pyramid consisting of "*" characters that is "n" characters tall
# at its tallest
#
# For example, print_pyramid(4) should print
#
# *
# **
# ***
# ****
# ***
# **
# *
# This is how we require the print-triangle.rb file in the current folder.
# We can now use the "print_triangle" and "print_line" methods we defined
# there -- as if we defined them here!
require_relative "./print_triangle"
def print_pyramid(height)
# This is your job. :)
# Suggestion: you can call print_triangle to print out the first, "upward"
# half of the pyramid. You'll have to write code to print out the second,
# "downward" half of the pyramid.
print_triangle(height)
height.downto(1) do |i| # or, equivalently, reverse the range from height to 1
print_line(i) # I called the print_line method to repeat the same process as in print_triangle.rb
end
end
if __FILE__ == $PROGRAM_NAME
# I'd advise putting some sanity checks here.
# How else will you be sure your code does what you think it does?
print_pyramid(1)
print "\n\n\n" # This is here to make the separation between pyramids clearer
print_pyramid(2)
print "\n\n\n" # This is here to make the separation between pyramids clearer
print_pyramid(3)
print "\n\n\n" # This is here to make the separation between pyramids clearer
print_pyramid(10)
end