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

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

Към профила на Кузман Белев

Резултати

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

Код

CONVERTER = {
'K' => {
'C' => -> (t) { t + 273.15 },
'F' => -> (t) { (t + 459.67) * 5 / 9.0 }
},
'F' => {
'C' => -> (t) { t * 9 / 5.0 + 32 },
'K' => -> (t) { t * 9 / 5.0 - 459.67 },
},
'C' => {
'K' => -> (t) { t - 273.15 },
'F' => -> (t) { (t - 32) * 5 / 9.0 }
}
}
MELTING_POINT = {
'water' => 0,
'ethanol' => -114,
'gold' => 1_064,
'silver' => 961.8,
'copper' => 1_085
}
BOILING_POINT = {
'water' => 100,
'ethanol' => 78.37,
'gold' => 2_700,
'silver' => 2_162,
'copper' => 2_567
}
def convert_between_temperature_units(degrees, input_units, output_units)
return degrees if input_units == output_units
CONVERTER[output_units][input_units].call(degrees)
end

Интересен подход с ламбдите. Все пак би могъл да си спестиш няколко от тях ако конвертираш градусите на 2 стъпки, ползвайки междинна мерна единица. От една страна, така ще правим по 2 конвертирания, но от друга, спестяваме изреждане на няколко реципрочни формули.

def melting_point_of_substance(substance, output_units)
convert_between_temperature_units(MELTING_POINT[substance], 'C', output_units)
end
def boiling_point_of_substance(substance, output_units)
convert_between_temperature_units(BOILING_POINT[substance], 'C', output_units)
end

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

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

Finished in 0.00896 seconds
17 examples, 0 failures

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

Кузман обнови решението на 14.10.2016 17:41 (преди над 7 години)

+CONVERTER = {
+ 'K' => {
+ 'C' => -> (t) { t + 273.15 },
+ 'K' => -> (t) { t },
+ 'F' => -> (t) { (t + 459.67) * 5 / 9.0 }
+ },
+
+ 'F' => {
+ 'C' => -> (t) { t * 9 / 5.0 + 32 },
+ 'K' => -> (t) { t * 9 / 5.0 - 459.67 },
+ 'F' => -> (t) { t }
+ },
+
+ 'C' => {
+ 'C' => -> (t) { t },
+ 'K' => -> (t) { t - 273.15 },
+ 'F' => -> (t) { (t - 32) * 5 / 9.0 }
+ }
+}
+
+MELTING_POINT = {
+ 'water' => 0,
+ 'ethanol' => -114,
+ 'gold' => 1_064,
+ 'silver' => 961.8,
+ 'copper' => 1_085
+}
+
+BOILING_POINT = {
+ 'water' => 100,
+ 'ethanol' => 78.37,
+ 'gold' => 2_700,
+ 'silver' => 2_162,
+ 'copper' => 2_567
+}
+
+def convert_between_temperature_units(degrees, input, output)
+ return degrees if input == output
+ CONVERTER[output][input].call(degrees)
+end

Интересен подход с ламбдите. Все пак би могъл да си спестиш няколко от тях ако конвертираш градусите на 2 стъпки, ползвайки междинна мерна единица. От една страна, така ще правим по 2 конвертирания, но от друга, спестяваме изреждане на няколко реципрочни формули.

+
+def melting_point_of_substance(substance, output)
+ convert_between_temperature_units(MELTING_POINT[substance], 'C', output)
+end
+
+def boiling_point_of_substance(substance, output)
+ convert_between_temperature_units(BOILING_POINT[substance], 'C', output)
+end

Кузман обнови решението на 16.10.2016 08:04 (преди над 7 години)

CONVERTER = {
'K' => {
'C' => -> (t) { t + 273.15 },
- 'K' => -> (t) { t },
'F' => -> (t) { (t + 459.67) * 5 / 9.0 }
},
-
'F' => {
'C' => -> (t) { t * 9 / 5.0 + 32 },
'K' => -> (t) { t * 9 / 5.0 - 459.67 },
- 'F' => -> (t) { t }
},
-
'C' => {
- 'C' => -> (t) { t },
'K' => -> (t) { t - 273.15 },
'F' => -> (t) { (t - 32) * 5 / 9.0 }
}
}
-
MELTING_POINT = {
'water' => 0,
'ethanol' => -114,
'gold' => 1_064,
'silver' => 961.8,
'copper' => 1_085
}
-
BOILING_POINT = {
'water' => 100,
'ethanol' => 78.37,
'gold' => 2_700,
'silver' => 2_162,
'copper' => 2_567
}
-def convert_between_temperature_units(degrees, input, output)
- return degrees if input == output
- CONVERTER[output][input].call(degrees)
+def convert_between_temperature_units(degrees, input_units, output_units)
+ return degrees if input_units == output_units
+ CONVERTER[output_units][input_units].call(degrees)
end
-def melting_point_of_substance(substance, output)
- convert_between_temperature_units(MELTING_POINT[substance], 'C', output)
+def melting_point_of_substance(substance, output_units)
+ convert_between_temperature_units(MELTING_POINT[substance], 'C', output_units)
end
-def boiling_point_of_substance(substance, output)
- convert_between_temperature_units(BOILING_POINT[substance], 'C', output)
+def boiling_point_of_substance(substance, output_units)
+ convert_between_temperature_units(BOILING_POINT[substance], 'C', output_units)
end

Кузман обнови решението на 16.10.2016 10:34 (преди над 7 години)

CONVERTER = {
'K' => {
'C' => -> (t) { t + 273.15 },
'F' => -> (t) { (t + 459.67) * 5 / 9.0 }
},
'F' => {
'C' => -> (t) { t * 9 / 5.0 + 32 },
'K' => -> (t) { t * 9 / 5.0 - 459.67 },
},
'C' => {
'K' => -> (t) { t - 273.15 },
'F' => -> (t) { (t - 32) * 5 / 9.0 }
}
}
+
MELTING_POINT = {
'water' => 0,
'ethanol' => -114,
'gold' => 1_064,
'silver' => 961.8,
'copper' => 1_085
}
+
BOILING_POINT = {
'water' => 100,
'ethanol' => 78.37,
'gold' => 2_700,
'silver' => 2_162,
'copper' => 2_567
}
def convert_between_temperature_units(degrees, input_units, output_units)
return degrees if input_units == output_units
CONVERTER[output_units][input_units].call(degrees)
end
def melting_point_of_substance(substance, output_units)
convert_between_temperature_units(MELTING_POINT[substance], 'C', output_units)
end
def boiling_point_of_substance(substance, output_units)
convert_between_temperature_units(BOILING_POINT[substance], 'C', output_units)
end