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 #761

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
34 changes: 34 additions & 0 deletions 2018/IvanKarpechanka/1/hw.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def fact(n)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Используй более дружелюбные переменные, чтобы можно было читать код легко, а не гадать что такое n, k или j

(1..n).inject(1, :*)
end

def binom(n, k)
fact(n) / (fact(k) * fact(n - k))
end

def pascals_row(row, first_el)
(0..row).map { |line| first_el * binom(row, line) }
end

puts 'Введите глубину дерева:'
three_len = gets.chomp.to_i
puts 'Введите базовый номер:'
first_el = gets.chomp.to_i

# to output a number with zeros in the beginning if its

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

# length is less than the length of the maximum element
number_max_len = pascals_row(three_len, first_el).max.to_s.length
console_len = 160
gaps = (' ' * number_max_len)
help_str = "\/#{gaps}\\#{gaps}" # Specifying a Tree Display Template
(0..three_len).each do |i|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

three_len.times do |tree_level|

str = '' # to create an output line on the console of each line of the tree
pascals_row(i, first_el).each do |elem|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent indentation detected.

str += elem.to_s.rjust(number_max_len, '0') + (gaps + ' ')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 (not 1) spaces for indentation.
Tab detected.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 (not 1) spaces for indentation.
Tab detected.

end
help1 = "#{' ' * three_len.to_s.length}"
puts help1 + (help_str * i).chomp(gaps).center(console_len)

help2 = "#{i.to_s.rjust(three_len.to_s.length, '0')}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer to_s over string interpolation.

puts help2 + str.chomp(gaps + ' ').center(console_len, ',')
end