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

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

Към профила на Стефан Якимов

Резултати

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

Код

def convert_between_temperature_units(degrees, input_unit, output_unit)
degrees = convert_to_celsius(degrees, input_unit) if input_unit != 'C'
output_unit == 'C' ? degrees : convert_from_celsius(degrees, output_unit)
end
def convert_to_celsius(degrees, input_unit)
input_unit == 'K' ? degrees - 273.15 : (degrees + 40) / 1.8 - 40
end
def convert_from_celsius(degrees, output_unit)
output_unit == 'K' ? degrees + 273.15 : (degrees + 40) * 1.8 - 40
end
def melting_point_of_substance(substance, unit)
convert_between_temperature_units(
Substance_to_melting_boiling_temp[substance][0], 'C', unit
)
end
def boiling_point_of_substance(substance, unit)
convert_between_temperature_units(
Substance_to_melting_boiling_temp[substance][1], 'C', unit
)
end
Substance_to_melting_boiling_temp = {
'water' => [0, 100], 'ethanol' => [-114, 78.37], 'gold' => [1_064, 2_700],
'silver' => [961.8, 2_162], 'copper' => [1_085, 2_567]
}.freeze

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

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

Finished in 0.00772 seconds
17 examples, 0 failures

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

Стефан обнови решението на 14.10.2016 17:57 (преди над 7 години)

+def convert_between_temperature_units(degrees, input_unit, output_unit)
+ degrees = convert_to_celsius(degrees, input_unit) if input_unit != 'C'
+ output_unit == 'C' ? degrees : convert_from_celsius(degrees, output_unit)
+end
+
+def convert_to_celsius(degrees, input_unit)
+ input_unit == 'K' ? degrees - 273.15 : (degrees + 40) / 1.8 - 40
+end
+
+def convert_from_celsius(degrees, output_unit)
+ output_unit == 'K' ? degrees + 273.15 : (degrees + 40) * 1.8 - 40
+end
+
+def melting_point_of_substance(substance, unit)
+ dic = {

dic...

Я го напиши както трябва, че някой може да го разчете като нещо различно от dictionary. Също, това име ми казва, че изобщо не си се замислил как да кръстиш променливата. Да кръстиш нещо на типа му не е добра идея. Кръщавай го на смисъла, на идеята на съдържанието, не на типа.

Още, аз бих изкарал това като константа извън функцията и бих написал стойностите на отделни редове. Така става по-ясно разделянето на данни от логика и ще са по-лесни за сканиране данните.

+ 'water' => 0, 'ethanol' => -114, 'gold' => 1_064,
+ 'silver' => 961.8, 'copper' => 1_085
+ }
+
+ convert_between_temperature_units(dic[substance], 'C', unit)
+end
+
+def boiling_point_of_substance(substance, unit)
+ dic = {
+ 'water' => 100, 'ethanol' => 78.37, 'gold' => 2_700,
+ 'silver' => 2_162, 'copper' => 2_567
+ }
+
+ convert_between_temperature_units(dic[substance], 'C', unit)
+end

Стефан обнови решението на 17.10.2016 14:57 (преди над 7 години)

def convert_between_temperature_units(degrees, input_unit, output_unit)
degrees = convert_to_celsius(degrees, input_unit) if input_unit != 'C'
output_unit == 'C' ? degrees : convert_from_celsius(degrees, output_unit)
end
def convert_to_celsius(degrees, input_unit)
input_unit == 'K' ? degrees - 273.15 : (degrees + 40) / 1.8 - 40
end
def convert_from_celsius(degrees, output_unit)
output_unit == 'K' ? degrees + 273.15 : (degrees + 40) * 1.8 - 40
end
def melting_point_of_substance(substance, unit)
- dic = {
- 'water' => 0, 'ethanol' => -114, 'gold' => 1_064,
- 'silver' => 961.8, 'copper' => 1_085
- }
-
- convert_between_temperature_units(dic[substance], 'C', unit)
+ convert_between_temperature_units(
+ Substance_to_melting_boiling_temp[substance][0], 'C', unit
+ )
end
def boiling_point_of_substance(substance, unit)
- dic = {
- 'water' => 100, 'ethanol' => 78.37, 'gold' => 2_700,
- 'silver' => 2_162, 'copper' => 2_567
- }
-
- convert_between_temperature_units(dic[substance], 'C', unit)
+ convert_between_temperature_units(
+ Substance_to_melting_boiling_temp[substance][1], 'C', unit
+ )
end
+
+Substance_to_melting_boiling_temp = {
+ 'water' => [0, 100], 'ethanol' => [-114, 78.37], 'gold' => [1_064, 2_700],
+ 'silver' => [961.8, 2_162], 'copper' => [1_085, 2_567]
+}.freeze