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

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

Към профила на Александър Илиев

Резултати

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

Код

TEMPERATURES = {
'water_melting_point': 0,
'water_boiling_point': 100,
'ethanol_melting_point': -114,
'ethanol_boiling_point': 78.37,
'gold_melting_point': 1064,
'gold_boiling_point': 2700,
'silver_melting_point': 961.8,
'silver_boiling_point': 2162,
'copper_melting_point': 1085,
'copper_boiling_point': 2567,
}
MELTING_POINT = '_melting_point'
BOILING_POINT = '_boiling_point'
def convert_between_temperature_units(deg, from, to)
case from
when 'C'
convert_celsius(deg, to)
when 'K'
convert_kelvin(deg, to)
when 'F'
convert_fahrenheit(deg, to)
end
end
def convert_celsius(deg, to)
case to
when 'K'
deg + 273.15
when 'F'
(deg * 1.8) + 32
else
deg
end
end
def convert_fahrenheit(deg, to)
case to
when 'K'
(deg + 459.67) / 1.8
when 'C'
(deg - 32) / 1.8
else
deg
end
end
def convert_kelvin(deg, to)
case to
when 'C'
deg - 273.15
when 'F'
(deg * 1.8) - 459.67
else
deg
end
end
def melting_point_of_substance(substance, deg_type)
operating_temp = TEMPERATURES[(substance + MELTING_POINT).to_sym]
convert_between_temperature_units(operating_temp, 'C', deg_type)
end
def boiling_point_of_substance(substance, deg_type)
operating_temp = TEMPERATURES[(substance + BOILING_POINT).to_sym]
convert_between_temperature_units(operating_temp, 'C', deg_type)
end

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

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

Finished in 0.00773 seconds
17 examples, 0 failures

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

Александър обнови решението на 16.10.2016 11:43 (преди над 7 години)

+WATER_CHANGING_POINTS = [0, 100]
+ETHANOL_CHANGING_POINTS = [-114, 78.37]
+GOLD_CHANGING_POINTS = [1064, 2700]
+SILVER_CHANGING_POINTS = [961.8, 2162]
+COPPER_CHANGING_POINTS = [1085, 2567]
+
+MELTING_POINT = 0
+BOILING_POINT = 1
+
+def convert_between_temperature_units(deg, from, to)
+ case from
+ when 'C'
+ convert_celsius(deg, to)
+ when 'K'
+ convert_kelvin(deg, to)
+ when 'F'
+ convert_fahrenheit(deg, to)
+ end
+end
+
+def convert_celsius(deg, to)
+ case to
+ when 'K'
+ deg + 273.15
+ when 'F'
+ (deg * 1.8) + 32
+ else
+ deg
+ end
+end
+
+def convert_fahrenheit(deg, to)
+ case to
+ when 'K'
+ (deg + 459.67) / 1.8
+ when 'C'
+ (deg - 32) / 1.8
+ else
+ deg
+ end
+end
+
+def convert_kelvin(deg, to)
+ case to
+ when 'C'
+ deg - 273.15
+ when 'F'
+ (deg * 1.8) - 459.67
+ else
+ deg
+ end
+end
+
+def melting_point_of_substance(substance, deg_type)
+ case substance
+ when 'water'
+ operating_temp = WATER_CHANGING_POINTS[MELTING_POINT]
+ when 'ethanol'
+ operating_temp = ETHANOL_CHANGING_POINTS[MELTING_POINT]
+ when 'gold'
+ operating_temp = GOLD_CHANGING_POINTS[MELTING_POINT]
+ when 'silver'
+ operating_temp = SILVER_CHANGING_POINTS[MELTING_POINT]
+ when 'copper'
+ operating_temp = COPPER_CHANGING_POINTS[MELTING_POINT]
+ end
+ convert_between_temperature_units(operating_temp, 'C', deg_type)
+end
+
+def boiling_point_of_substance(substance, deg_type)
+ case substance
+ when 'water'
+ operating_temp = WATER_CHANGING_POINTS[BOILING_POINT]
+ when 'ethanol'
+ operating_temp = ETHANOL_CHANGING_POINTS[BOILING_POINT]
+ when 'gold'
+ operating_temp = GOLD_CHANGING_POINTS[BOILING_POINT]
+ when 'silver'
+ operating_temp = SILVER_CHANGING_POINTS[BOILING_POINT]
+ when 'copper'
+ operating_temp = COPPER_CHANGING_POINTS[BOILING_POINT]
+ end
+ convert_between_temperature_units(operating_temp, 'C', deg_type)
+end

Александър обнови решението на 16.10.2016 22:30 (преди над 7 години)

-WATER_CHANGING_POINTS = [0, 100]
-ETHANOL_CHANGING_POINTS = [-114, 78.37]
-GOLD_CHANGING_POINTS = [1064, 2700]
-SILVER_CHANGING_POINTS = [961.8, 2162]
-COPPER_CHANGING_POINTS = [1085, 2567]
+TEMPERATURES = {
+ 'water_melting_point': 0,
+ 'water_boiling_point': 100,
+ 'ethanol_melting_point': -114,
+ 'ethanol_boiling_point': 78.37,
+ 'gold_melting_point': 1064,
+ 'gold_boiling_point': 2700,
+ 'silver_melting_point': 961.8,
+ 'silver_boiling_point': 2162,
+ 'copper_melting_point': 1085,
+ 'copper_boiling_point': 2567,
+}
-MELTING_POINT = 0
-BOILING_POINT = 1
+MELTING_POINT = '_melting_point'
+BOILING_POINT = '_boiling_point'
def convert_between_temperature_units(deg, from, to)
case from
when 'C'
convert_celsius(deg, to)
when 'K'
convert_kelvin(deg, to)
when 'F'
convert_fahrenheit(deg, to)
end
end
def convert_celsius(deg, to)
case to
when 'K'
deg + 273.15
when 'F'
(deg * 1.8) + 32
else
deg
end
end
def convert_fahrenheit(deg, to)
case to
when 'K'
(deg + 459.67) / 1.8
when 'C'
(deg - 32) / 1.8
else
deg
end
end
def convert_kelvin(deg, to)
case to
when 'C'
deg - 273.15
when 'F'
(deg * 1.8) - 459.67
else
deg
end
end
def melting_point_of_substance(substance, deg_type)
- case substance
- when 'water'
- operating_temp = WATER_CHANGING_POINTS[MELTING_POINT]
- when 'ethanol'
- operating_temp = ETHANOL_CHANGING_POINTS[MELTING_POINT]
- when 'gold'
- operating_temp = GOLD_CHANGING_POINTS[MELTING_POINT]
- when 'silver'
- operating_temp = SILVER_CHANGING_POINTS[MELTING_POINT]
- when 'copper'
- operating_temp = COPPER_CHANGING_POINTS[MELTING_POINT]
- end
+ operating_temp = TEMPERATURES[(substance + MELTING_POINT).to_sym]
convert_between_temperature_units(operating_temp, 'C', deg_type)
end
def boiling_point_of_substance(substance, deg_type)
- case substance
- when 'water'
- operating_temp = WATER_CHANGING_POINTS[BOILING_POINT]
- when 'ethanol'
- operating_temp = ETHANOL_CHANGING_POINTS[BOILING_POINT]
- when 'gold'
- operating_temp = GOLD_CHANGING_POINTS[BOILING_POINT]
- when 'silver'
- operating_temp = SILVER_CHANGING_POINTS[BOILING_POINT]
- when 'copper'
- operating_temp = COPPER_CHANGING_POINTS[BOILING_POINT]
- end
+ operating_temp = TEMPERATURES[(substance + BOILING_POINT).to_sym]
convert_between_temperature_units(operating_temp, 'C', deg_type)
end

Благодаря за коментара, надявам се съм поправил това, което си имал в предвид. Между другото, rubocop ми отчита за грешка това, че Hash-ът е дефиниран с : вместо с =>, а видях в презентацията, че предпочитате първото.