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

homework 1 #771

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
50 changes: 50 additions & 0 deletions 2018/Serg-Lapata/hw-1/task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
def triangle(height, start_value)
rows = []

height.times do |row_number|
current_value = start_value
row = [current_value]
k = 1
row_number.times do
Copy link
Contributor

Choose a reason for hiding this comment

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

а почему бы аналог k переменной не передать в блок? Ведь times это может и тогда этот вот k = 1 можно будет убрать

current_value = current_value * (row_number - k + 1) / k
row << current_value
k += 1
end
rows << row
end

print_triangle(rows)
end

def check_integer(int)
int.positive? ? int : raise('Got invalid number')

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

def start_value
int = ENV['BASE_NUMBER']

unless int
puts 'Enter start value'
int = gets
end

check_integer(int.to_i)
end

def print_triangle(rows)
width_element = rows.last.max.to_s.length + 1

Choose a reason for hiding this comment

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

Inconsistent indentation detected.

width_last_row = width_element * rows.last.length

Choose a reason for hiding this comment

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

Inconsistent indentation detected.


rows.each.with_index do |row, row_number|

Choose a reason for hiding this comment

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

Inconsistent indentation detected.

row_data = row.map do |element|
element.to_s.center(width_element)
end.join.center(width_last_row)

puts "#{row_number}: #{row_data}"
end
end

puts 'Enter height triangle'
height_triangle = gets.to_i
check_integer(height_triangle)
triangle(height_triangle, start_value)