Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement hw 1 #773

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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)