forked from codeunion/ruby-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_triangle.rb
More file actions
56 lines (45 loc) · 1.42 KB
/
print_triangle.rb
File metadata and controls
56 lines (45 loc) · 1.42 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
54
55
56
# Method name: print_triangle
# Input: a number n
# Returns: Nothing
# Prints: a right triangle consisting of "*" characters that is "n" characters tall
#
# For example, print_triangle(4) should print
#
# *
# **
# ***
# ****
# The print_line method is here to help you.
# Conceptually, it prints out a row of "count" *'s. Run it yourself to
# see how it works. Experiment with different inputs.
def print_line(count)
(1..count).each do |i|
print "*" # This prints a single "*"
end
print "\n" # This forces the output to the next like, like hitting "return" on the keyboard
end
# Print a triangle. Defaults to ascending (smallest on top). Can be set to descending.
def print_triangle(height, order="asc")
if order == "asc" || order != "desc"
(1..height).each do |i|
print_line(i)
end
elsif order == "desc"
i = height
while i > 0
print_line(i)
i -= 1
end
end
end
# There are no rumble strips this time. It's up to you to decide whether
# this is working as intended or not.
if __FILE__ == $0
print_triangle(1, "asc")
print "\n\n\n" # This is here just to make the separation between triangles clearer
print_triangle(2, "asc")
print "\n\n\n" # This is here just to make the separation between triangles clearer
print_triangle(3, "asc")
print "\n\n\n" # This is here just to make the separation between triangles clearer
print_triangle(10, "asc")
end