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

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

Към профила на Петър Нетовски

Резултати

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

Код

MELTING_POINTS_IN_CELSIUS = {
"water" => 0,
"ethanol" => -114,
"gold" => 1_064,
"silver" => 961.8,
"copper" => 1_085
}
BOILING_POINTS_IN_CELSIUS = {
"water" => 100,
"ethanol" => 78.37,
"gold" => 2_700,
"silver" => 2_162,
"copper" => 2_567
}
def convert_between_temperature_units(degrees, base_unit, result_unit)
return degrees if base_unit == result_unit
units = "#{base_unit}#{result_unit}"
case units
when "CK" then degrees + 273.15
when "KC" then degrees - 273.15
when "CF" then degrees * 9.0 / 5 + 32
when "FC" then (degrees - 32) * 5.0 / 9
else degrees_in_celsius = convert_between_temperature_units(degrees, base_unit, "C")
convert_between_temperature_units(degrees_in_celsius, "C", result_unit)
end
end
def melting_point_of_substance(substance, unit)
degrees_in_celsius = MELTING_POINTS_IN_CELSIUS[substance]
convert_between_temperature_units(degrees_in_celsius, "C", unit)
end
def boiling_point_of_substance(substance, unit)
degrees_in_celsius = BOILING_POINTS_IN_CELSIUS[substance]
convert_between_temperature_units(degrees_in_celsius, "C", unit)
end

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

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

Finished in 0.00891 seconds
17 examples, 0 failures

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

Петър обнови решението на 12.10.2016 17:56 (преди около 8 години)

+def convert_between_temperature_units(degrees, base_unit, result_unit)
+ units = "#{base_unit}#{result_unit}"
+
+ case units
+ when "CK" then degrees + 273.15
+ when "CF" then degrees * 9.0 / 5 + 32
+ when "KC" then degrees - 273.15
+ when "KF" then degrees * 9.0 / 5 - 459.67
+ when "FC" then (degrees - 32) * 5.0 / 9
+ when "FK" then (degrees + 495.67) * 5.0 / 9
+ end
+end
+
+def melting_point_of_substance(substance, unit)
+ melting_points_in_celsius = {
+ "water" => 0,
+ "ethanol" => -114,
+ "gold" => 1_064,
+ "silver" => 961.8,
+ "copper" => 1_085
+ }
+
+ if (unit <=> "C") == 0
+ melting_points_in_celsius[substance]
+ else
+ degrees_in_celsius = melting_points_in_celsius[substance]
+ convert_between_temperature_units(degrees_in_celsius, "C", unit)
+ end
+end
+
+def boiling_point_of_substance(substance, unit)
+ boiling_points_in_celsius = {
+ "water" => 100,
+ "ethanol" => 78.37,
+ "gold" => 2_700,
+ "silver" => 2_162,
+ "copper" => 2_567
+ }
+
+ if (unit <=> "C") == 0
+ boiling_points_in_celsius[substance]
+ else
+ degrees_in_celsius = boiling_points_in_celsius[substance]
+ convert_between_temperature_units(degrees_in_celsius, "C", unit)
+ end
+end

Петър обнови решението на 12.10.2016 18:07 (преди около 8 години)

def convert_between_temperature_units(degrees, base_unit, result_unit)
+ return degrees if (base_unit <=> result_unit) == 0
+
units = "#{base_unit}#{result_unit}"
case units
when "CK" then degrees + 273.15
when "CF" then degrees * 9.0 / 5 + 32
when "KC" then degrees - 273.15
when "KF" then degrees * 9.0 / 5 - 459.67
when "FC" then (degrees - 32) * 5.0 / 9
- when "FK" then (degrees + 495.67) * 5.0 / 9
+ when "FK" then (degrees + 459.67) * 5.0 / 9
end
end
def melting_point_of_substance(substance, unit)
melting_points_in_celsius = {
"water" => 0,
"ethanol" => -114,
"gold" => 1_064,
"silver" => 961.8,
"copper" => 1_085
}
if (unit <=> "C") == 0
melting_points_in_celsius[substance]
else
degrees_in_celsius = melting_points_in_celsius[substance]
convert_between_temperature_units(degrees_in_celsius, "C", unit)
end
end
def boiling_point_of_substance(substance, unit)
boiling_points_in_celsius = {
"water" => 100,
"ethanol" => 78.37,
"gold" => 2_700,
"silver" => 2_162,
"copper" => 2_567
}
if (unit <=> "C") == 0
boiling_points_in_celsius[substance]
else
degrees_in_celsius = boiling_points_in_celsius[substance]
convert_between_temperature_units(degrees_in_celsius, "C", unit)
end
end

Петър обнови решението на 12.10.2016 18:55 (преди около 8 години)

def convert_between_temperature_units(degrees, base_unit, result_unit)
return degrees if (base_unit <=> result_unit) == 0
units = "#{base_unit}#{result_unit}"
case units
when "CK" then degrees + 273.15
when "CF" then degrees * 9.0 / 5 + 32
when "KC" then degrees - 273.15
when "KF" then degrees * 9.0 / 5 - 459.67
when "FC" then (degrees - 32) * 5.0 / 9
when "FK" then (degrees + 459.67) * 5.0 / 9
end
end
def melting_point_of_substance(substance, unit)
melting_points_in_celsius = {
- "water" => 0,
- "ethanol" => -114,
- "gold" => 1_064,
- "silver" => 961.8,
- "copper" => 1_085
+ "water" => 0, "ethanol" => -114, "gold" => 1_064,
+ "silver" => 961.8, "copper" => 1_085
}
if (unit <=> "C") == 0
melting_points_in_celsius[substance]
else
degrees_in_celsius = melting_points_in_celsius[substance]
convert_between_temperature_units(degrees_in_celsius, "C", unit)
end
end
def boiling_point_of_substance(substance, unit)
boiling_points_in_celsius = {
- "water" => 100,
- "ethanol" => 78.37,
- "gold" => 2_700,
- "silver" => 2_162,
- "copper" => 2_567
+ "water" => 100, "ethanol" => 78.37, "gold" => 2_700,
+ "silver" => 2_162, "copper" => 2_567
}
if (unit <=> "C") == 0
boiling_points_in_celsius[substance]
else
degrees_in_celsius = boiling_points_in_celsius[substance]
convert_between_temperature_units(degrees_in_celsius, "C", unit)
end
end

Петър обнови решението на 16.10.2016 16:24 (преди около 8 години)

+MELTING_POINTS_IN_CELSIUS = {
+ "water" => 0,
+ "ethanol" => -114,
+ "gold" => 1_064,
+ "silver" => 961.8,
+ "copper" => 1_085
+}
+
+BOILING_POINTS_IN_CELSIUS = {
+ "water" => 100,
+ "ethanol" => 78.37,
+ "gold" => 2_700,
+ "silver" => 2_162,
+ "copper" => 2_567
+}
+
def convert_between_temperature_units(degrees, base_unit, result_unit)
- return degrees if (base_unit <=> result_unit) == 0
+ return degrees if base_unit == result_unit
units = "#{base_unit}#{result_unit}"
case units
when "CK" then degrees + 273.15
- when "CF" then degrees * 9.0 / 5 + 32
when "KC" then degrees - 273.15
- when "KF" then degrees * 9.0 / 5 - 459.67
+ when "CF" then degrees * 9.0 / 5 + 32
when "FC" then (degrees - 32) * 5.0 / 9
- when "FK" then (degrees + 459.67) * 5.0 / 9
+ else degrees_in_celsius = convert_between_temperature_units(degrees, base_unit, "C")
+ convert_between_temperature_units(degrees_in_celsius, "C", result_unit)
end
end
def melting_point_of_substance(substance, unit)
- melting_points_in_celsius = {
- "water" => 0, "ethanol" => -114, "gold" => 1_064,
- "silver" => 961.8, "copper" => 1_085
- }
-
- if (unit <=> "C") == 0
- melting_points_in_celsius[substance]
- else
- degrees_in_celsius = melting_points_in_celsius[substance]
- convert_between_temperature_units(degrees_in_celsius, "C", unit)
- end
+ degrees_in_celsius = MELTING_POINTS_IN_CELSIUS[substance]
+ convert_between_temperature_units(degrees_in_celsius, "C", unit)
end
def boiling_point_of_substance(substance, unit)
- boiling_points_in_celsius = {
- "water" => 100, "ethanol" => 78.37, "gold" => 2_700,
- "silver" => 2_162, "copper" => 2_567
- }
-
- if (unit <=> "C") == 0
- boiling_points_in_celsius[substance]
- else
- degrees_in_celsius = boiling_points_in_celsius[substance]
- convert_between_temperature_units(degrees_in_celsius, "C", unit)
- end
+ degrees_in_celsius = BOILING_POINTS_IN_CELSIUS[substance]
+ convert_between_temperature_units(degrees_in_celsius, "C", unit)
end