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

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

Към профила на Мартин Христов

Резултати

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

Код

CELSIUS_COEFFICIENTS = {
"C" => {
"initial_value" => 0,
"delta" => 1,
"formula_to_celsius" => -> (degrees) { degrees },
"celsius_to_unit" => -> (degrees) { degrees }
},
"F" => {
"initial_value" => 32,
"delta" => 1.8,
"celsius_to_unit" => -> (degrees) { degrees * 1.8 + 32 },
"formula_to_celsius" => -> (degrees) { (degrees - 32) / 1.8 }
},
"K" => {
"initial_value" => 273.15,
"delta" => 1,
"celsius_to_unit" => -> (celsius) { celsius + 273.15 },
"formula_to_celsius" => -> (degrees) { degrees - 273.15 }
}
}
SUBSTANCE_MELTING_POINTS = {
"water" => 0,
"ethanol" => -114,
"gold" => 1064,
"silver" => 961.8,
"copper" => 1085
}
SUBSTANCE_BOILING_POINTS = {
"water" => 100,
"ethanol" => 78.37,
"gold" => 2700,
"silver" => 2162,
"copper" => 2567
}
def convert_between_temperature_units(degrees, convert_from_unit, convert_to_unit)
celsius = CELSIUS_COEFFICIENTS[convert_from_unit]["formula_to_celsius"].call(degrees)
CELSIUS_COEFFICIENTS[convert_to_unit]["celsius_to_unit"].call(celsius)
end
def melting_point_of_substance(substance, unit)
convert_between_temperature_units(SUBSTANCE_MELTING_POINTS[substance], "C", unit)
end
def boiling_point_of_substance(substance, unit)
convert_between_temperature_units(SUBSTANCE_BOILING_POINTS[substance], "C", unit)
end

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

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

Finished in 0.00773 seconds
17 examples, 0 failures

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

Мартин обнови решението на 10.10.2016 23:56 (преди над 7 години)

+CELSIUM_METRIX = {
+ C: 0,
+ F: 32.8,
+ K: 273.15,
+}
+
+FARENHEIT_METRIX = {
+ F: 0,
+ C: -16.22,
+ K: 254.928
+}
+
+KELVIN_METRIX = {
+ K: 0,
+ C: -271.15,
+ F: -456.87
+}
+
+LIQUIDS_MELT_TEMP = {
+ water: 0,
+ ethanol: -114,
+ gold: 1064,
+ silver: 961.8,
+ copper: 1085
+}
+
+LIQUIDS_BOIL_TEMP = {
+ water: 100,
+ ethanol: 78.37,
+ gold: 2700,
+ silver: 2162,
+ copper: 2567
+}
+
+def convert_between_temperature_units(degree, convert_from, convert_to)
+ case convert_from
+ when "C"
+ degree + CELSIUM_METRIX[convert_to.to_sym].to_f
+ when "F"
+ degree + FARENHEIT_METRIX[convert_to.to_sym].to_f
+ else
+ degree + KELVIN_METRIX[convert_to.to_sym].to_f
+ end
+end
+
+def melting_point_of_substance(liquid, unit)
+ convert_between_temperature_units(LIQUIDS_MELT_TEMP[liquid.to_sym], "C", unit)
+end
+
+def boiling_point_of_substance(liquid, unit)
+ convert_between_temperature_units(LIQUIDS_BOIL_TEMP[liquid.to_sym], "C", unit)
+end

Мартин обнови решението на 11.10.2016 13:47 (преди над 7 години)

-CELSIUM_METRIX = {
- C: 0,
- F: 32.8,
- K: 273.15,
+CELSIUS_METRIX = {
+ C: {
+ INITIAL_VALUE: 0,
+ DELTA: 1
+ },
+ F: {
+ INITIAL_VALUE: 32,
+ DELTA: 1.8
+ },
+ K: {
+ INITIAL_VALUE: 273.15,
+ DELTA: 1
+ }
}
-FARENHEIT_METRIX = {
- F: 0,
- C: -16.22,
- K: 254.928
-}
-
-KELVIN_METRIX = {
- K: 0,
- C: -271.15,
- F: -456.87
-}
-
LIQUIDS_MELT_TEMP = {

Супер е, че си използвал хеш за това. Само, че името на константата не ми харесва:

  • TEMP. Избягвай подобни съкращения. Не би ти попречило да напишеш LIQUID_MELT_TEMPERATURES.
  • Забележи, че на горното не сложих LIQUID в множествено число :)
  • Златото, среброто и медта не са течности - защо пише liquids?

liquids melt temp vs liquid melt temperatures vs liquid melting temperatures vs liquid melting points vs substance melting points

Кое от горните ти звучи най-добре?

water: 0,
ethanol: -114,
gold: 1064,
silver: 961.8,
copper: 1085
}
LIQUIDS_BOIL_TEMP = {
water: 100,
ethanol: 78.37,
gold: 2700,
silver: 2162,
copper: 2567
}
def convert_between_temperature_units(degree, convert_from, convert_to)
case convert_from
when "C"
- degree + CELSIUM_METRIX[convert_to.to_sym].to_f
+ convert_to_celsius(degree, convert_to)
when "F"
- degree + FARENHEIT_METRIX[convert_to.to_sym].to_f
+ celsius = (degree - 32) / 1.8
+ convert_to_celsius(celsius, "C")
else
- degree + KELVIN_METRIX[convert_to.to_sym].to_f
+ celsius = degree - 273.15
+ convert_to_celsius(celsius, "C")
end
+end
+
+def convert_to_celsius(degree, convert_from)
+ metrix = CELSIUS_METRIX[convert_from.to_sym]

Ето този to_sym е малко изкуствен. Защо ти е да правиш хеша със символи за ключове и да конвертираш постоянно, вместо да го направиш директно със стрингове? Няма да се налага преобразуване и е по-очевидно, че методите приемат стрингове.

+ degree * metrix[:DELTA] + metrix[:INITIAL_VALUE].to_f
end
def melting_point_of_substance(liquid, unit)
convert_between_temperature_units(LIQUIDS_MELT_TEMP[liquid.to_sym], "C", unit)
end
def boiling_point_of_substance(liquid, unit)
convert_between_temperature_units(LIQUIDS_BOIL_TEMP[liquid.to_sym], "C", unit)
end

Мартин обнови решението на 14.10.2016 11:30 (преди над 7 години)

-CELSIUS_METRIX = {
- C: {
- INITIAL_VALUE: 0,
- DELTA: 1
+CELSIUS_COEFFICIENTS = {
+ "C" => {
+ "initial_value" => 0,
+ "delta" => 1,
+ "formula_to_celsius" => -> (degrees) { degrees },
+ "celsius_to_unit" => -> (degrees) { degrees }
},
- F: {
- INITIAL_VALUE: 32,
- DELTA: 1.8
+ "F" => {
+ "initial_value" => 32,
+ "delta" => 1.8,
+ "celsius_to_unit" => -> (degrees) { degrees * 1.8 + 32 },
+ "formula_to_celsius" => -> (degrees) { (degrees - 32) / 1.8 }
},
- K: {
- INITIAL_VALUE: 273.15,
- DELTA: 1
+ "K" => {
+ "initial_value" => 273.15,
+ "delta" => 1,
+ "celsius_to_unit" => -> (celsius) { celsius + 273.15 },
+ "formula_to_celsius" => -> (degrees) { degrees - 273.15 }
}
}
-LIQUIDS_MELT_TEMP = {
- water: 0,
- ethanol: -114,
- gold: 1064,
- silver: 961.8,
- copper: 1085
+SUBSTANCE_MELTING_POINTS = {
+ "water" => 0,
+ "ethanol" => -114,
+ "gold" => 1064,
+ "silver" => 961.8,
+ "copper" => 1085
}
-LIQUIDS_BOIL_TEMP = {
- water: 100,
- ethanol: 78.37,
- gold: 2700,
- silver: 2162,
- copper: 2567
+SUBSTANCE_BOILING_POINTS = {
+ "water" => 100,
+ "ethanol" => 78.37,
+ "gold" => 2700,
+ "silver" => 2162,
+ "copper" => 2567
}
-def convert_between_temperature_units(degree, convert_from, convert_to)
- case convert_from
- when "C"
- convert_to_celsius(degree, convert_to)
- when "F"
- celsius = (degree - 32) / 1.8
- convert_to_celsius(celsius, "C")
- else
- celsius = degree - 273.15
- convert_to_celsius(celsius, "C")
- end
+def convert_between_temperature_units(degrees, convert_from_unit, convert_to_unit)
+ celsius = CELSIUS_COEFFICIENTS[convert_from_unit]["formula_to_celsius"].call(degrees)
+ CELSIUS_COEFFICIENTS[convert_to_unit]["celsius_to_unit"].call(celsius)
end
-def convert_to_celsius(degree, convert_from)
- metrix = CELSIUS_METRIX[convert_from.to_sym]
- degree * metrix[:DELTA] + metrix[:INITIAL_VALUE].to_f
+def melting_point_of_substance(substance, unit)
+ convert_between_temperature_units(SUBSTANCE_MELTING_POINTS[substance], "C", unit)
end
-def melting_point_of_substance(liquid, unit)
- convert_between_temperature_units(LIQUIDS_MELT_TEMP[liquid.to_sym], "C", unit)
-end
-
-def boiling_point_of_substance(liquid, unit)
- convert_between_temperature_units(LIQUIDS_BOIL_TEMP[liquid.to_sym], "C", unit)
+def boiling_point_of_substance(substance, unit)
+ convert_between_temperature_units(SUBSTANCE_BOILING_POINTS[substance], "C", unit)
end