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

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

Към профила на Божидар Михайлов

Резултати

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

Код

def convert_between_temperature_units(src_degs, src_unit, dest_unit)
converters = [
-> (c_degs) { c_degs * 9.0 / 5 + 32 },
-> (f_degs) { (f_degs + 459.67) * 5.0 / 9 },
-> (k_degs) { k_degs - 273.15 }
]
from_index = 'CFK'.index(src_unit)
to_index = ('CFK' * 2).index(dest_unit, from_index)
converter_chain = (converters * 2)[from_index...to_index]
converter_chain.reduce(src_degs) { |acc, f| f.call(acc) }
end
def get_degs_and_convert_units(degree_source, substance, unit)
degrees = degree_source[substance]
convert_between_temperature_units(degrees, 'C', unit)
end
def melting_point_of_substance(substance, unit)
melting_points_in_c = {
'water' => 0, 'ethanol' => -114, 'gold' => 1064,
'silver' => 961.8, 'copper' => 1085
}
get_degs_and_convert_units(melting_points_in_c, substance, unit)
end
def boiling_point_of_substance(substance, unit)
boling_points_in_c = {
'water' => 100, 'ethanol' => 78.37, 'gold' => 2700,
'silver' => 2162, 'copper' => 2567
}
get_degs_and_convert_units(boling_points_in_c, substance, unit)
end

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

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

Finished in 0.00861 seconds
17 examples, 0 failures

История (1 версия и 0 коментара)

Божидар обнови решението на 16.10.2016 22:46 (преди над 7 години)

+def convert_between_temperature_units(src_degs, src_unit, dest_unit)
+ converters = [
+ -> (c_degs) { c_degs * 9.0 / 5 + 32 },
+ -> (f_degs) { (f_degs + 459.67) * 5.0 / 9 },
+ -> (k_degs) { k_degs - 273.15 }
+ ]
+ from_index = 'CFK'.index(src_unit)
+ to_index = ('CFK' * 2).index(dest_unit, from_index)
+ converter_chain = (converters * 2)[from_index...to_index]
+ converter_chain.reduce(src_degs) { |acc, f| f.call(acc) }
+end
+
+def get_degs_and_convert_units(degree_source, substance, unit)
+ degrees = degree_source[substance]
+ convert_between_temperature_units(degrees, 'C', unit)
+end
+
+def melting_point_of_substance(substance, unit)
+ melting_points_in_c = {
+ 'water' => 0, 'ethanol' => -114, 'gold' => 1064,
+ 'silver' => 961.8, 'copper' => 1085
+ }
+ get_degs_and_convert_units(melting_points_in_c, substance, unit)
+end
+
+def boiling_point_of_substance(substance, unit)
+ boling_points_in_c = {
+ 'water' => 100, 'ethanol' => 78.37, 'gold' => 2700,
+ 'silver' => 2162, 'copper' => 2567
+ }
+ get_degs_and_convert_units(boling_points_in_c, substance, unit)
+end