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

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

Към профила на Симеон Гергинов

Резултати

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

Код

MELTING_POINTS = {water: 0, ethanol: -114, gold: 1_064, silver: 961.8, copper: 1_085}
BOILING_POINTS = {water: 100, ethanol: 78.37, gold: 2_700, silver: 2_162, copper: 2_567}
def convert_to_celsius(temperature, from_which_temperature)
case from_which_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_which_temperature)
case to_which_temperature
when 'F' then temperature * (9.0 / 5) + 32
when 'K' then temperature + 273.15
when 'C' then temperature
end
end
def convert_between_temperature_units(temperature, from_which_temperature, to_which_temperature)
temperature_in_celsius = convert_to_celsius(temperature, from_which_temperature)
convert_from_celsius(temperature_in_celsius, to_which_temperature)
end
def melting_point_of_substance(substance, temperature_unit)
temperature_of_melting = MELTING_POINTS[substance.to_sym]
convert_from_celsius(temperature_of_melting, temperature_unit)
end
def boiling_point_of_substance(substance, temperature_unit)
temperature_of_boiling = BOILING_POINTS[substance.to_sym]
convert_from_celsius(temperature_of_boiling, temperature_unit)
end

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

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

Finished in 0.00767 seconds
17 examples, 0 failures

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

Симеон обнови решението на 13.10.2016 09:54 (преди над 7 години)

+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_to_celsius(temperature, from)
+ case from
+ when 'F'
+ (temperature - 32) * (5.0 / 9)
+ when 'K'
+ temperature - 273.15
+ when 'C'
+ 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
+ end
+end
+
+def convert_between_temperature_units(temperature, from, to)
+ convert_from_celsius(convert_to_celsius(temperature, from), to)

Супер е, че си се сетил да го направиш на две стъпки :)

Тук ще е по-ясно ако направиш и кода на две стъпки - просто два реда, например:

temperature_in_celsius = convert_to_celsius(temperature, from)
convert_from_celsius(temperature_in_celsius, to)

Това е дреболия, но все пак в някои случаи става доста по-ясно като изкараш нещо в променлива - самото име на променливата изяснява кода още :)

+end
+
+def melting_point_of_substance(substance, temperature_unit)
+ temperature_of_melting = MELTING_POINT[substance.to_sym]
+ convert_from_celsius(temperature_of_melting, temperature_unit)
+end
+
+def boiling_point_of_substance(substance, temperature_unit)
+ temperature_of_boiling = BOILING_POINT[substance.to_sym]
+ convert_from_celsius(temperature_of_boiling, temperature_unit)
+end

Симеон обнови решението на 16.10.2016 19:35 (преди над 7 години)

-MELTING_POINT = {water: 0, ethanol: -114, gold: 1_064, silver: 961.8, copper: 1_085}
+MELTING_POINTS = {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}
+BOILING_POINTS = {water: 100, ethanol: 78.37, gold: 2_700, silver: 2_162, copper: 2_567}
-def convert_to_celsius(temperature, from)
- case from
- when 'F'
- (temperature - 32) * (5.0 / 9)
- when 'K'
- temperature - 273.15
- when 'C'
- temperature
+def convert_to_celsius(temperature, from_which_temperature)
+ case from_which_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
+def convert_from_celsius(temperature, to_which_temperature)
+ case to_which_temperature
+ when 'F' then temperature * (9.0 / 5) + 32
+ when 'K' then temperature + 273.15
+ when 'C' then temperature
end
end
-def convert_between_temperature_units(temperature, from, to)
- convert_from_celsius(convert_to_celsius(temperature, from), to)
+def convert_between_temperature_units(temperature, from_which_temperature, to_which_temperature)
+ temperature_in_celsius = convert_to_celsius(temperature, from_which_temperature)
+ convert_from_celsius(temperature_in_celsius, to_which_temperature)
end
def melting_point_of_substance(substance, temperature_unit)
- temperature_of_melting = MELTING_POINT[substance.to_sym]
+ temperature_of_melting = MELTING_POINTS[substance.to_sym]
convert_from_celsius(temperature_of_melting, temperature_unit)
end
def boiling_point_of_substance(substance, temperature_unit)
- temperature_of_boiling = BOILING_POINT[substance.to_sym]
+ temperature_of_boiling = BOILING_POINTS[substance.to_sym]
convert_from_celsius(temperature_of_boiling, temperature_unit)
end