Решение на Първа задача - температура и химични елементи от Методи Димитров

Обратно към всички решения

Към профила на Методи Димитров

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 17 успешни тест(а)
  • 0 неуспешни тест(а)

Код

def convert_between_temperature_units(degrees, input_unit, output_unit)
multiplier = { 'C' => 1.8, 'F' => 1, 'K' => 1.8 }
additive = { 'C' => 32, 'F' => 0, 'K' => -459.67 }
degrees_fahrenheit = degrees * multiplier[input_unit] + additive[input_unit]
(degrees_fahrenheit - additive[output_unit]) / multiplier[output_unit]
end
def melting_point_of_substance(substance, unit)
melting_points = {
"water" => 0,
"ethanol" => -114,
"gold" => 1064,
"silver" => 961.8,
"copper" => 1085
}
convert_between_temperature_units melting_points[substance], 'C', unit
end
def boiling_point_of_substance(substance, unit)
boiling_points = {
"water" => 100,
"ethanol" => 78.37,
"gold" => 2700,
"silver" => 2162,
"copper" => 2567
}
convert_between_temperature_units boiling_points[substance], 'C', unit
end

Лог от изпълнението

.................

Finished in 0.008 seconds
17 examples, 0 failures

История (2 версии и 5 коментара)

Методи обнови решението на 13.10.2016 16:27 (преди над 7 години)

+def convert_between_temperature_units(degrees, input_unit, output_unit)
+ multiplier = { 'C' => 1.8, 'F' => 1, 'K' => 1.8 }
+ additive = { 'C' => 32, 'F' => 0, 'K' => -459.67 }
+ to_fahrenheit = ((degrees * multiplier[input_unit]) + additive[input_unit])

Тук най-външните скоби са напълно ненужни, а вътрешните са по-скоро 50/50, макар че и без тях резултатът ще е правилен, тъй като умножението е с по-висок приоритет. Има случаи, в които скобите са полезни с цел четимост, но по принцип е по-добре да си ги спестяваме там, където не правят кода по-ясен.

Името to_fahrenheit би могло да е по-описателно.. degrees_fahrenheit, може би?

Иначе добър подход с конвертирането на 2 стъпки. Интересно е че си избрал точно F като междинна мерна единица, но това е по-скоро въпрос на вкус. :)

+ (to_fahrenheit - additive[output_unit]) / multiplier[output_unit]
+end
+
+def melting_point_of_substance(substance)

Този метод трябва да приема като втори параметър и мерна единица, в която да върне резултата. В този му вид тестовете биха ти свалили всички точки, които даваме за него, въпреки че работи правилно за Целзий и е на 95% готов да работи и с други мерни единици. Успя ли да си пуснеш примерните тестове? Те биха уловили точно такъв тип грешки или недоглеждания. :)

+ melting_points = {
+ "water" => 0,
+ "ethanol" => -114,
+ "gold" => 1064,
+ "silver" => 961.8,
+ "copper" => 1085
+ }
+ melting_points[substance]
+end
+
+def boiling_point_of_substance(substance)
+ boiling_points = {
+ "water" => 100,
+ "ethanol" => 78.37,
+ "gold" => 2700,
+ "silver" => 2162,
+ "copper" => 2567
+ }
+ boiling_points[substance]
+end

Методи обнови решението на 17.10.2016 15:26 (преди над 7 години)

def convert_between_temperature_units(degrees, input_unit, output_unit)
- multiplier = { 'C' => 1.8, 'F' => 1, 'K' => 1.8 }
- additive = { 'C' => 32, 'F' => 0, 'K' => -459.67 }
- to_fahrenheit = ((degrees * multiplier[input_unit]) + additive[input_unit])
- (to_fahrenheit - additive[output_unit]) / multiplier[output_unit]
+ multiplier = { 'C' => 1.8, 'F' => 1, 'K' => 1.8 }
+ additive = { 'C' => 32, 'F' => 0, 'K' => -459.67 }
+ degrees_fahrenheit = degrees * multiplier[input_unit] + additive[input_unit]
+ (degrees_fahrenheit - additive[output_unit]) / multiplier[output_unit]
end
-def melting_point_of_substance(substance)
+def melting_point_of_substance(substance, unit)
melting_points = {
"water" => 0,
"ethanol" => -114,
"gold" => 1064,
"silver" => 961.8,
"copper" => 1085
}
- melting_points[substance]
+ convert_between_temperature_units melting_points[substance], 'C', unit
end
-def boiling_point_of_substance(substance)
+def boiling_point_of_substance(substance, unit)
boiling_points = {
"water" => 100,
"ethanol" => 78.37,
"gold" => 2700,
"silver" => 2162,
"copper" => 2567
}
- boiling_points[substance]
+ convert_between_temperature_units boiling_points[substance], 'C', unit
end