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

Add homework-1 #770

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
43 changes: 43 additions & 0 deletions 2018/alexeymoissenko/hm-1/Pascal-tree.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Triangle
def initialize(base_number, deep)
@base_number = base_number
@deep = deep
end

def fact(any_value)
(1..any_value).reduce(:*)
end

def binomial(ln_s, num_in_ln)
return @base_number if ln_s - num_in_ln <= 0 || num_in_ln <= 0
fact(ln_s) / (fact(num_in_ln) * fact(ln_s - num_in_ln)) * @base_number
end

def curr_str(curr_size)
(0..curr_size).map { |e| binomial(curr_size, e) }
end

def show
max_row = curr_str(@deep)
max_el_size = max_row.max.to_s.size
width = [`tput cols`.to_i, 100].max
@deep.times do |i|
string = curr_str(i).map do |el|
el.to_s.center(max_el_size)
end.join(' ').center(width)
puts "#{i + 1}: #{string}"
end
end
end

puts 'Введите начальное значение:'

base_number = gets.chomp.to_i

puts 'Введите глубину треугольника:'

deep = gets.chomp.to_i

triangle = Triangle.new(base_number, deep)

triangle.show