-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyramid.rb
More file actions
60 lines (53 loc) · 1.13 KB
/
pyramid.rb
File metadata and controls
60 lines (53 loc) · 1.13 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
57
58
59
60
class Pyramid
def initialize base='null'
if base.is_a? Array
@base = base.map do |num|
num.to_f
end
end
end
def get_input
puts 'Please enter a base a row:'
base = gets().chomp
base = base.split(',')
base = base.map do |num|
num.to_f
end
@base = base
end
def build_pyramid
if !@base then get_input end
size = @base.size - 1
pyramid = Array.new(size)
pyramid[0] = @base
size.times do |prev_index|
pyramid[prev_index + 1] = build_row pyramid[prev_index]
end
@pyramid = pyramid.reverse
end
def build_row prev_row
row = Array.new()
(prev_row.size - 1).times do |index|
row << prev_row[index] + prev_row[index + 1]
end
return row
end
def print_random_pyramid size=10, max_input=100
base = []
size.times do
base << rand(max_input)
end
@base = base
print_pyramid
end
def print_pyramid
if !@pyramid then build_pyramid end
for row in @pyramid do
print row
puts
end
end
end
pyramid = Pyramid.new [1,2,3,4,5,6,7,8]
pyramid.print_pyramid
# pyramid.print_random_pyramid 10