Skip to content
Open

hw 1 #766

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/DmitryShkuratov/1/homework-1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class PascalsTriangle
def initialize
print "Enter the depth of the triangle:\n n="
@rows = gets.chomp.to_i
end

def to_pascal
temp = []
@rows.times do |row|
line = [1]
(0..row-1).each { |x| line << (line[x] * (row - x) / (x + 1)) }
temp << line
end
end
temp
end

def print_triangle
max = to_pascal.flatten.max.to_s.length
strings = to_pascal.map { |arr| arr.map { |int| int.to_s.center(max + 3)} }
strings.each do |line|
puts line.join.center(strings[-1].join.length)

Choose a reason for hiding this comment

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

Use 2 (not 0) spaces for indentation.

end

Choose a reason for hiding this comment

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

end at 23, 2 is not aligned with strings.each do |line| at 21, 4.

end

row = PascalsTriangle.new
row.print_triangle