Add lesson-1 comments#1
Conversation
| # вернет 4, т.е. квадратный корень из 16. | ||
|
|
||
| puts "Enter a b c (with delimiter ','):" | ||
| sides = gets.chomp.split(',').map {|i| Float(i) } |
There was a problem hiding this comment.
Обычно используют to_f.
Ещё можно применить сокращённую форму вызова метода в итераторе map:
sides = gets.chomp.split(',').map(&:to_f)There was a problem hiding this comment.
Второй момент, я догадываюсь почему, но sides здесь совсем не sides, а coefficients.
| sides = gets.chomp.split(',').map {|i| Float(i) } | ||
| a = sides[0] | ||
| b = sides[1] | ||
| c = sides[2] |
There was a problem hiding this comment.
Можно воспользоваться параллельным присваиванием:
a, b, c = sidesА можно сразу работать через массив sides, чтобы не плодить лишних локальных переменных.
| a = sides[0] | ||
| b = sides[1] | ||
| c = sides[2] | ||
| if (d=b**2-4*a*c) == 0 |
There was a problem hiding this comment.
В данном случае присваивание лучше вынести за пределы if.
| elsif d<0 | ||
| puts "no roots of equation" | ||
| elsif d>0 | ||
| puts "x1 = #{(-b+Math.sqrt(d))/(2*a)}\nx2 = #{(-b-Math.sqrt(d))/(2*a)}" |
There was a problem hiding this comment.
Здесь стоит разделить логику вывода и вычислений. Иначе трудно читать, поддерживать и ориентироваться.
| puts 'triangle is rectangular' | ||
| end | ||
| else | ||
| puts "something went wrong" |
There was a problem hiding this comment.
Почему что-то пошло не так? Треугольник может существовать, просто не быть прямоугольным.
| b = sides[1] | ||
| c = sides[2] | ||
| case sides.max | ||
| when a |
There was a problem hiding this comment.
Подумай как можно это отрефакторить чтобы не считать три раза теорему Пифагора.
| else | ||
| puts "something went wrong" | ||
| end | ||
| if [a, c].include? b |
There was a problem hiding this comment.
[3, 5].include?(3) - равнобедренный, но не равносторонний.
| puts 'triangle is equilateral and isosceles' | ||
| elsif a == b || a == c || b == c | ||
| puts 'triangle is isosceles' | ||
| end No newline at end of file |
There was a problem hiding this comment.
Обычно пишут в таком формате:
if <condition>
<code>
else
<code>
end| end | ||
| if [a, c].include? b | ||
| puts 'triangle is equilateral and isosceles' | ||
| elsif a == b || a == c || b == c |
There was a problem hiding this comment.
Подумай как ещё можно определить это условие.
| a = gets.chomp.to_f | ||
| puts 'Enter h' | ||
| h = gets.chomp.to_f | ||
| puts "S = #{0.5*a*h}" No newline at end of file |
There was a problem hiding this comment.
Стоит вычислять площадь заранее и присваивать результат локальной переменной, затем её здесь выводить чтобы разделить логику вычисления и вывода.
| puts 'Fine, now i want to know your height' | ||
| height = gets.chomp.to_i | ||
| best_weight = height - 110 | ||
| if best_weight <= 0 |
There was a problem hiding this comment.
По-моему, должно быть наоборот )
| if best_weight <= 0 | ||
| puts "Ok, #{name}, yor best weight is #{best_weight}" | ||
| else puts "Your weight is good" | ||
| end No newline at end of file |
There was a problem hiding this comment.
Как правило, условные выражения форматируют так:
if <condition>
<code>
else
<code>
end
No description provided.