Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions 2018/KuikoIhar/1/pas_tri.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def pas_tri(h, n)
l = [n]
[l] + (1..h).map do
l = [n] + l[1..-1].map.with_index { |x, i| x + l[i] } + [n]
end
end

def pad(s, n)
l = n - s.size
' ' * (l / 2) + s + ' ' * (l - l / 2)
end

def lines(rows)
n = rows[-1].max.to_s.size
rows.map { |row| row.map { |x| pad(x.to_s, n) }.join(' ') }
end

def center(line)
n = line[-1].size
line.map { |s| pad(s, n) }
end

puts 'Введите глубину дерева: '
h = gets.chomp.to_i
puts 'Введите базовый номер: '
n = gets.chomp.to_i
puts center lines pas_tri(h, n)