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

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

Към профила на Иван Станков

Резултати

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

Код

MELTING_POINT = {
'water' => 0,
'ethanol' => -114,
'gold' => 1064,
'silver' => 961.8,
'copper' => 1085
}
BOILING_POINT = {
'water' => 100,
'ethanol' => 78.37,
'gold' => 2700,
'silver' => 2162,
'copper' => 2567
}
def convert_between_temperature_units(degree, initial_unit, target_unit)
case initial_unit
when 'C'
convert_from_c(degree, target_unit)
when 'K'
convert_from_k(degree, target_unit)
when 'F'
convert_from_f(degree, target_unit)
end
end
def melting_point_of_substance(substance, unit)
convert_between_temperature_units(MELTING_POINT[substance], 'C', unit)
end
def boiling_point_of_substance(substance, unit)
convert_between_temperature_units(BOILING_POINT[substance], 'C', unit)
end
def convert_from_c(degree, target_unit)
case target_unit
when 'K'
degree + 273.15
when 'F'
(degree * 9.0 / 5.0) + 32
when 'C'
degree
end
def convert_from_k(degree, target_unit)
case target_unit
when 'K'
degree
when 'F'
(degree - 273.15) * 9.0 / 5.0 + 32
when 'C'
degree - 273.15
end
def convert_from_f(degree, target_unit)
case target_unit
when 'K'
(degree - 32) * 5.0 / 9.0 + 273.15
when 'F'
degree
when 'C'
(degree - 32) * 5.0 / 9.0
end

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

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

Finished in 0.00778 seconds
17 examples, 0 failures

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

Иван обнови решението на 16.10.2016 10:54 (преди над 7 години)

+MELTING_POINT = {
+ 'water' => 0,
+ 'ethanol' => -114,
+ 'gold' => 1064,
+ 'silver' => 961.8,
+ 'copper' => 1085
+}
+
+BOILING_POINT = {
+ 'water' => 100,
+ 'ethanol' => 78.37,
+ 'gold' => 2700,
+ 'silver' => 2162,
+ 'copper' => 2567
+}
+
+def convert_between_temperature_units(degree, initial_unit, target_unit)
+ case initial_unit
+ when 'C'
+ convert_from_c(degree, target_unit)
+ when 'K'
+ convert_from_k(degree, target_unit)
+ when 'F'
+ convert_from_f(degree, target_unit)
+ end
+end
+
+def melting_point_of_substance(substance, unit)
+ convert_between_temperature_units(MELTING_POINT[substance], 'C', unit)
+end
+
+def boiling_point_of_substance(substance, unit)
+ convert_between_temperature_units(BOILING_POINT[substance], 'C', unit)
+end
+
+def convert_from_c(degree, target_unit)
+ if target_unit == 'K'
+ degree + 273.15
+ elsif target_unit == 'F'
+ (degree * 9.0 / 5.0) + 32
+ else
+ degree
+end
+
+def convert_from_k(degree, target_unit)
+ if target_unit == 'K'
+ degree
+ elsif target_unit == 'F'
+ (degree - 273.15) * 9.0 / 5.0 + 32
+ else
+ degree - 273.15
+ end
+
+def convert_from_f(degree, target_unit)
+ if target_unit == 'K'
+ (degree - 32) * 5.0 / 9.0 + 273.15
+ elsif target_unit == 'F'
+ degree
+ else
+ (degree - 32) * 5.0 / 9.0
+ end

Иван обнови решението на 17.10.2016 13:04 (преди над 7 години)

MELTING_POINT = {
'water' => 0,
'ethanol' => -114,
'gold' => 1064,
'silver' => 961.8,
'copper' => 1085
}
BOILING_POINT = {
'water' => 100,
'ethanol' => 78.37,
'gold' => 2700,
'silver' => 2162,
'copper' => 2567
}
def convert_between_temperature_units(degree, initial_unit, target_unit)
case initial_unit
when 'C'
convert_from_c(degree, target_unit)
when 'K'
convert_from_k(degree, target_unit)
when 'F'
convert_from_f(degree, target_unit)
end
end
def melting_point_of_substance(substance, unit)
convert_between_temperature_units(MELTING_POINT[substance], 'C', unit)
end
def boiling_point_of_substance(substance, unit)
convert_between_temperature_units(BOILING_POINT[substance], 'C', unit)
end
def convert_from_c(degree, target_unit)
- if target_unit == 'K'
+ case target_unit
+ when 'K'
degree + 273.15
- elsif target_unit == 'F'
+ when 'F'
(degree * 9.0 / 5.0) + 32
- else
+ when 'C'
degree
end
end
def convert_from_k(degree, target_unit)
- if target_unit == 'K'
+ case target_unit
+ when 'K'
degree
- elsif target_unit == 'F'
+ when 'F'
(degree - 273.15) * 9.0 / 5.0 + 32
- else
+ when 'C'
degree - 273.15
end
end
def convert_from_f(degree, target_unit)
- if target_unit == 'K'
+ case target_unit
+ when 'K'
(degree - 32) * 5.0 / 9.0 + 273.15
- elsif target_unit == 'F'
+ when 'F'
degree
- else
+ when 'C'
(degree - 32) * 5.0 / 9.0
end
end