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

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

Към профила на Елеонора Кайкова

Резултати

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

Код

def convert_between_temperature_units(temperature, from, to)
case from
when 'F'
temperature = (temperature - 32) / 1.8
when 'K'
temperature -= 273.15
end
case to
when 'F'
temperature = temperature * 1.8 + 32
when 'K'
temperature += 273.15
end
temperature
end
MELTING_POINT_OF = {
'ethanol' => -114,
'gold' => 1_064,
'silver' => 961.8,
'copper' => 1_085
}
BOILING_POINT_OF = {
'water' => 100,
'ethanol' => 78.37,
'gold' => 2_700,
'silver' => 2_162,
'copper' => 2_567
}
def melting_point_of_substance(substance, temperature)
convert_between_temperature_units(MELTING_POINT_OF[substance], 'C', temperature)
end
def boiling_point_of_substance(substance, temperature)
convert_between_temperature_units(BOILING_POINT_OF[substance], 'C', temperature)
end

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

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

Finished in 0.00764 seconds
17 examples, 0 failures

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

Елеонора обнови решението на 16.10.2016 18:14 (преди над 7 години)

+def convert_between_temperature_units(temp, from, to)
+ case from
+ when 'C'
+ if to == 'F'
+ temp = temp * 1.8 + 32
+ elsif to == 'K'
+ temp += 273.15
+ end
+
+ when 'F'
+ if to == 'C'
+ temp = (temp - 32) / 1.8
+ elsif to == 'K'
+ temp = (temp + 459.67) / 1.8
+ end
+
+ when 'K'
+ if to == 'F'
+ temp = temp * 1.8 - 459.67
+ elsif to == 'C'
+ temp -= 273.15
+ end
+ end
+ temp.round(4)
+end
+
+MELTING = {

Топене. Топене? Топене! Опитай се да измислиш име, така че да можеш да прочетеш цялата операция като смислено изречение. В момента чета "Топенето се състои от вода - 0, етанол - -114...".

+ 'ethanol' => -114,
+ 'gold' => 1_064,
+ 'silver' => 961.8,
+ 'copper' => 1_085
+}
+
+BOILING = {
+ 'water' => 100,
+ 'ethanol' => 78.37,
+ 'gold' => 2_700,
+ 'silver' => 2_162,
+ 'copper' => 2_567
+}
+
+def melting_point_of_substance(sub, t)
+ newt = MELTING[sub]
+ newt = convert_between_temperature_units(newt, 'C', t) if t != 'C'

Имаше ли някакъв начин да избегнеш този специален случай (и съответно чуденето за име на временна променлива). Какво ще стане, ако на convert_between_temperature_units кажеш да конвертира от целзии в целзии?

+ newt
+end
+
+def boiling_point_of_substance(sub, t)
+ newt = BOILING[sub]
+ newt = convert_between_temperature_units(newt, 'C', t) if t != 'C'
+ newt
+end

Елеонора обнови решението на 16.10.2016 23:28 (преди над 7 години)

-def convert_between_temperature_units(temp, from, to)
+def convert_between_temperature_units(temperature, from, to)
case from
- when 'C'
- if to == 'F'
- temp = temp * 1.8 + 32
- elsif to == 'K'
- temp += 273.15
- end
-
when 'F'
- if to == 'C'
- temp = (temp - 32) / 1.8
- elsif to == 'K'
- temp = (temp + 459.67) / 1.8
- end
+ temperature = (temperature - 32) / 1.8
+ when 'K'
+ temperature -= 273.15
+ end
+ case to
+ when 'F'
+ temperature = temperature * 1.8 + 32
when 'K'
- if to == 'F'
- temp = temp * 1.8 - 459.67
- elsif to == 'C'
- temp -= 273.15
- end
+ temperature += 273.15
end
- temp.round(4)
+ temperature
end
-MELTING = {
+MELTING_POINT_OF = {
'water' => 0,
'ethanol' => -114,
'gold' => 1_064,
'silver' => 961.8,
'copper' => 1_085
}
-BOILING = {
+BOILING_POINT_OF = {
'water' => 100,
'ethanol' => 78.37,
'gold' => 2_700,
'silver' => 2_162,
'copper' => 2_567
}
-def melting_point_of_substance(sub, t)
- newt = MELTING[sub]
- newt = convert_between_temperature_units(newt, 'C', t) if t != 'C'
- newt
+def melting_point_of_substance(substance, temperature)
+ convert_between_temperature_units(MELTING_POINT_OF[substance], 'C', temperature)
end
-def boiling_point_of_substance(sub, t)
- newt = BOILING[sub]
- newt = convert_between_temperature_units(newt, 'C', t) if t != 'C'
- newt
+def boiling_point_of_substance(substance, temperature)
+ convert_between_temperature_units(BOILING_POINT_OF[substance], 'C', temperature)
end