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

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

Към профила на София Петрова

Резултати

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

Код

UNITS = {
'C' => 32.0,
'K' => -459.67
}
MELTING_POINTS_IN_CELSIUS = {
'water' => 0.0,
'ethanol' => -114.0,
'gold' => 1064.0,
'silver' => 961.8,
'copper' => 1085.0
}
BOILING_POINTS_IN_CELSIUS = {
'water' => 100.0,
'ethanol' => 78.37,
'gold' => 2700.0,
'silver' => 2162.0,
'copper' => 2567.0
}
def convert_between_temperature_units(temperature, original_unit, new_unit)
return temperature if original_unit == new_unit
temperature_in_fahrenheit =
original_unit == 'F' ? temperature : temperature * 9.0 / 5.0 + UNITS[original_unit]
return temperature_in_fahrenheit if new_unit == 'F'
(temperature_in_fahrenheit - UNITS[new_unit]) * 5.0 / 9.0
end
def melting_point_of_substance(substance, unit)
convert_between_temperature_units(MELTING_POINTS_IN_CELSIUS[substance], 'C', unit)
end
def boiling_point_of_substance(substance, unit)
convert_between_temperature_units(BOILING_POINTS_IN_CELSIUS[substance], 'C', unit)
end

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

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

Finished in 0.00783 seconds
17 examples, 0 failures

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

София обнови решението на 15.10.2016 01:51 (преди около 8 години)

+def convert_between_temperature_units(original_degrees, original_unit, new_unit)
+ units = {
+ 'F' => original_degrees - original_degrees * 9.0 / 5.0,

Йохо, вие в четвърти ранг не можете ли просто да принесете в жертва една червена панда и кода да се оправи сам? :)

Не виждам голяма разлика между двете версии.

Помисли как да го разцепиш този метод на две. Така ще си решиш проблемите. :) В тези два метода може да имаш по един if или case.

+ 'C' => 32.0,
+ 'K' => -459.67
+ }
+
+ return original_degrees if original_unit == new_unit
+ temperature_in_fahrenheit = original_degrees * 9.0 / 5.0 + units[original_unit]
+ return temperature_in_fahrenheit if new_unit == 'F'
+
+ (temperature_in_fahrenheit - units[new_unit]) * 5.0 / 9.0
+end
+
+def melting_point_of_substance(substance, unit)
+ melting_points_in_celsius = {
+ 'water' => 0.0,
+ 'ethanol' => -114.0,
+ 'gold' => 1064.0,
+ 'silver' => 961.8,
+ 'copper' => 1085
+ }
+
+ convert_between_temperature_units(melting_points_in_celsius[substance], 'C', unit)
+end
+
+def boiling_point_of_substance(substance, unit)
+ boiling_points_in_celsius = {
+ 'water' => 100.0,
+ 'ethanol' => 78.37,
+ 'gold' => 2700.0,
+ 'silver' => 2162.0,
+ 'copper' => 2567
+ }
+
+ convert_between_temperature_units(boiling_points_in_celsius[substance], 'C', unit)
+end

София обнови решението на 15.10.2016 02:01 (преди около 8 години)

-def convert_between_temperature_units(original_degrees, original_unit, new_unit)
- units = {
- 'F' => original_degrees - original_degrees * 9.0 / 5.0,
- 'C' => 32.0,
- 'K' => -459.67
- }
+UNITS = {
+ 'C' => 32.0,
+ 'K' => -459.67
+}
+MELTING_POINTS_IN_CELSIUS = {
+ 'water' => 0.0,
+ 'ethanol' => -114.0,
+ 'gold' => 1064.0,
+ 'silver' => 961.8,
+ 'copper' => 1085
+}
+
+BOILING_POINTS_IN_CELSIUS = {
+ 'water' => 100.0,
+ 'ethanol' => 78.37,
+ 'gold' => 2700.0,
+ 'silver' => 2162.0,
+ 'copper' => 2567
+}
+
+def convert_between_temperature_units(original_degrees, original_unit, new_unit)
return original_degrees if original_unit == new_unit
- temperature_in_fahrenheit = original_degrees * 9.0 / 5.0 + units[original_unit]
+
+ temperature_in_fahrenheit = original_unit == 'F' ? original_degrees : original_degrees * 9.0 / 5.0 + UNITS[original_unit]
+
return temperature_in_fahrenheit if new_unit == 'F'
- (temperature_in_fahrenheit - units[new_unit]) * 5.0 / 9.0
+ (temperature_in_fahrenheit - UNITS[new_unit]) * 5.0 / 9.0
end
def melting_point_of_substance(substance, unit)
- melting_points_in_celsius = {
- 'water' => 0.0,
- 'ethanol' => -114.0,
- 'gold' => 1064.0,
- 'silver' => 961.8,
- 'copper' => 1085
- }
-
- convert_between_temperature_units(melting_points_in_celsius[substance], 'C', unit)
+ convert_between_temperature_units(MELTING_POINTS_IN_CELSIUS[substance], 'C', unit)
end
def boiling_point_of_substance(substance, unit)
- boiling_points_in_celsius = {
- 'water' => 100.0,
- 'ethanol' => 78.37,
- 'gold' => 2700.0,
- 'silver' => 2162.0,
- 'copper' => 2567
- }
-
- convert_between_temperature_units(boiling_points_in_celsius[substance], 'C', unit)
+ convert_between_temperature_units(BOILING_POINTS_IN_CELSIUS[substance], 'C', unit)
end

София обнови решението на 15.10.2016 02:27 (преди около 8 години)

UNITS = {
'C' => 32.0,
'K' => -459.67
}
MELTING_POINTS_IN_CELSIUS = {
'water' => 0.0,
'ethanol' => -114.0,
'gold' => 1064.0,
'silver' => 961.8,
- 'copper' => 1085
+ 'copper' => 1085.0
}
BOILING_POINTS_IN_CELSIUS = {
'water' => 100.0,
'ethanol' => 78.37,
'gold' => 2700.0,
'silver' => 2162.0,
- 'copper' => 2567
+ 'copper' => 2567.0
}
-def convert_between_temperature_units(original_degrees, original_unit, new_unit)
- return original_degrees if original_unit == new_unit
-
- temperature_in_fahrenheit = original_unit == 'F' ? original_degrees : original_degrees * 9.0 / 5.0 + UNITS[original_unit]
-
+def convert_between_temperature_units(temperature, original_unit, new_unit)
+ return temperature if original_unit == new_unit
+ temperature_in_fahrenheit =
+ original_unit == 'F' ? temperature : temperature * 9.0 / 5.0 + UNITS[original_unit]
return temperature_in_fahrenheit if new_unit == 'F'
(temperature_in_fahrenheit - UNITS[new_unit]) * 5.0 / 9.0
end
def melting_point_of_substance(substance, unit)
convert_between_temperature_units(MELTING_POINTS_IN_CELSIUS[substance], 'C', unit)
end
def boiling_point_of_substance(substance, unit)
convert_between_temperature_units(BOILING_POINTS_IN_CELSIUS[substance], 'C', unit)
end