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

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

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

Резултати

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

Код

SUBSTANCE_TEMPERATURES_IN_C = {
'water' => { melting_point: 0, boiling_point: 100 },
'ethanol' => { melting_point: -114, boiling_point: 78.37 },
'gold' => { melting_point: 1064, boiling_point: 2700 },
'silver' => { melting_point: 961.8, boiling_point: 2162 },
'copper' => { melting_point: 1085, boiling_point: 2567 }
}
FAHRENHEIT = 'F'
KELVIN = 'K'
CELSIUS = 'C'
def convert_between_temperature_units(temperature, from, to)
convert_from_celsius(convert_to_celsius(temperature, from), to)
end
def melting_point_of_substance(substance, unit)
convert_from_celsius(SUBSTANCE_TEMPERATURES_IN_C[substance][:melting_point], unit)
end
def boiling_point_of_substance(substance, unit)
convert_from_celsius(SUBSTANCE_TEMPERATURES_IN_C[substance][:boiling_point], unit)
end
def convert_to_celsius(temperature, from)
case from
when FAHRENHEIT then (temperature - 32) * (5.0 / 9)
when KELVIN then temperature - 273.15
when CELSIUS then temperature
end
end
def convert_from_celsius(temperature, to)
case to
when FAHRENHEIT then temperature * (9.0 / 5) + 32
when KELVIN then temperature + 273.15
when CELSIUS then temperature
end
end

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

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

Finished in 0.00877 seconds
17 examples, 0 failures

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

Калина обнови решението на 11.10.2016 21:09 (преди над 7 години)

+# Substance temperature of melting and boiling in Celsius
+SUBSTANCES_TEMP_C = {

Харесва ми, че си изкарала тези в константа :)

Не ми харесва, че съдържа думичката TEMP. Не печелиш нищо от това, че си използвала съкращение. Защо не просто SUBSTANCES_TEMPERATURE_C. И като сме почнали да разнищваме това име, кое ти звучи по-правилно - substances temperature или substance temperatures? :)

Последно, C-то там е малко неясно. substance temperatures C не ми звучи правилно. Може би една думичка in ще оправи нещата? :)

EDIT: Като оправиш името и използваш хеш няма да има нужда от коментара - ще е очевидно от кода.

+ 'water' => [0, 100],
+ 'ethanol' => [-114, 78.37],
+ 'gold' => [1064, 2700],
+ 'silver' => [961.8, 2162],
+ 'copper' => [1085, 2567]

За температурите, вместо да имаш двойки може да използваш хешове - [0, 100] => {melting_point: 0, boiling_point: 100}. Така долу във функциите няма да ти се наложи да използваш индексите, а [:melting_point]

+}
+
+def convert_between_temperature_units(temperature, from, to)
+ convert_from_celsius(convert_to_celsius(temperature, from), to)
+end
+
+def melting_point_of_substance(substance, unit)
+ convert_from_celsius(SUBSTANCES_TEMP_C[substance][0], unit)
+end
+
+def boiling_point_of_substance(substance, unit)
+ convert_from_celsius(SUBSTANCES_TEMP_C[substance][1], unit)
+end
+
+def convert_to_celsius(temperature, from)
+ case from
+ when 'F'
+ (temperature - 32) * (5.0 / 9)
+ when 'K'
+ temperature - 273.15
+ when 'C'
+ temperature

Супер е, че си разделила двете функции от и до целзий. Тук можеш да го напишеш малко по-кратко като използваш inline варианта на when:

  case from
  when 'F' then (temperature - 32) * (5.0 / 9)
  when 'K' then temperature - 273.15
  when 'C' then temperature
  end
+ end
+end
+
+def convert_from_celsius(temperature, to)
+ case to
+ when 'F'
+ temperature * (9.0 / 5) + 32
+ when 'K'
+ temperature + 273.15
+ when 'C'
+ temperature
+ end
+end

Калина обнови решението на 14.10.2016 21:41 (преди над 7 години)

-# Substance temperature of melting and boiling in Celsius
-SUBSTANCES_TEMP_C = {
- 'water' => [0, 100],
- 'ethanol' => [-114, 78.37],
- 'gold' => [1064, 2700],
- 'silver' => [961.8, 2162],
- 'copper' => [1085, 2567]
-}
+SUBSTANCE_TEMPERATURE_IN_C = {
+ 'water' => { melting_point: 0, boiling_point: 100 },
+ 'ethanol' => { melting_point: -114, boiling_point: 78.37 },
+ 'gold' => { melting_point: 1064, boiling_point: 2700 },
+ 'silver' => { melting_point: 961.8, boiling_point: 2162 },
+ 'copper' => { melting_point: 1085, boiling_point: 2567 }
+}.freeze
def convert_between_temperature_units(temperature, from, to)
convert_from_celsius(convert_to_celsius(temperature, from), to)
end
def melting_point_of_substance(substance, unit)
- convert_from_celsius(SUBSTANCES_TEMP_C[substance][0], unit)
+ convert_from_celsius(SUBSTANCE_TEMPERATURE_IN_C[substance][:melting_point], unit)
end
def boiling_point_of_substance(substance, unit)
- convert_from_celsius(SUBSTANCES_TEMP_C[substance][1], unit)
+ convert_from_celsius(SUBSTANCE_TEMPERATURE_IN_C[substance][:boiling_point], unit)
end
def convert_to_celsius(temperature, from)
case from
- when 'F'
- (temperature - 32) * (5.0 / 9)
- when 'K'
- temperature - 273.15
- when 'C'
- temperature
+ when 'F' then (temperature - 32) * (5.0 / 9)
+ when 'K' then temperature - 273.15
+ when 'C' then temperature
end
end
def convert_from_celsius(temperature, to)
case to
- when 'F'
- temperature * (9.0 / 5) + 32
- when 'K'
- temperature + 273.15
- when 'C'
- temperature
+ when 'F' then temperature * (9.0 / 5) + 32
+ when 'K' then temperature + 273.15
+ when 'C' then temperature
end
end

Калина обнови решението на 14.10.2016 21:57 (преди над 7 години)

-SUBSTANCE_TEMPERATURE_IN_C = {
+SUBSTANCE_TEMPERATURES_IN_C = {
'water' => { melting_point: 0, boiling_point: 100 },
'ethanol' => { melting_point: -114, boiling_point: 78.37 },
'gold' => { melting_point: 1064, boiling_point: 2700 },
'silver' => { melting_point: 961.8, boiling_point: 2162 },
'copper' => { melting_point: 1085, boiling_point: 2567 }
-}.freeze
+}
+FAHRENHEIT = 'F'
+KELVIN = 'K'
+CELSIUS = 'C'
def convert_between_temperature_units(temperature, from, to)
convert_from_celsius(convert_to_celsius(temperature, from), to)
end
def melting_point_of_substance(substance, unit)
- convert_from_celsius(SUBSTANCE_TEMPERATURE_IN_C[substance][:melting_point], unit)
+ convert_from_celsius(SUBSTANCE_TEMPERATURES_IN_C[substance][:melting_point], unit)
end
def boiling_point_of_substance(substance, unit)
- convert_from_celsius(SUBSTANCE_TEMPERATURE_IN_C[substance][:boiling_point], unit)
+ convert_from_celsius(SUBSTANCE_TEMPERATURES_IN_C[substance][:boiling_point], unit)
end
def convert_to_celsius(temperature, from)
case from
- when 'F' then (temperature - 32) * (5.0 / 9)
- when 'K' then temperature - 273.15
- when 'C' then temperature
+ when FAHRENHEIT then (temperature - 32) * (5.0 / 9)
+ when KELVIN then temperature - 273.15
+ when CELSIUS then temperature
end
end
def convert_from_celsius(temperature, to)
case to
- when 'F' then temperature * (9.0 / 5) + 32
- when 'K' then temperature + 273.15
- when 'C' then temperature
+ when FAHRENHEIT then temperature * (9.0 / 5) + 32
+ when KELVIN then temperature + 273.15
+ when CELSIUS then temperature
end
end