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

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

Към профила на Мая Терзиева

Резултати

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

Код

SUBSTANCE_TEMPERATURES = {
'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(degrees, from, to)
from_celsius(to_celsius(degrees, from), to)
end
def melting_point_of_substance(name, unit)
from_celsius(SUBSTANCE_TEMPERATURES[name][:melting_point], unit)
end
def boiling_point_of_substance(name, unit)
from_celsius(SUBSTANCE_TEMPERATURES[name][:boiling_point], unit)
end
def from_celsius(degrees, to)
case to
when 'F' then degrees * 1.8 + 32
when 'K' then degrees + 273.15
else degrees
end
end
def to_celsius(degrees, to)
case to
when 'F' then (degrees - 32) / 1.8
when 'K' then degrees - 273.15
else degrees
end
end

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

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

Finished in 0.00768 seconds
17 examples, 0 failures

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

Мая обнови решението на 12.10.2016 17:41 (преди около 8 години)

+SUBSTANCE_TEMPERATURES = [
+ { name: 'water', melting_point: 0, boiling_point: 100 },
+ { name: 'ethanol', melting_point: -114, boiling_point: 78.37 },
+ { name: 'gold', melting_point: 1064, boiling_point: 2700 },
+ { name: 'silver', melting_point: 961.8, boiling_point: 2162 },
+ { name: 'copper', melting_point: 1085, boiling_point: 2567 }
+].freeze
+
+def convert_between_temperature_units(degrees, from, to)
+ case from
+ when 'C' then from_celsius(degrees, to)
+ when 'K' then from_kelvin(degrees, to)
+ else
+ from_fahrenheit(degrees, to)
+ end
+end
+
+def melting_point_of_substance(name, unit)
+ from_celsius(
+ SUBSTANCE_TEMPERATURES
+ .select { |substance| substance[:name] == name }
+ .first[:melting_point],
+ unit
+ )
+end
+
+def boiling_point_of_substance(name, unit)
+ from_celsius(
+ SUBSTANCE_TEMPERATURES
+ .select { |substance| substance[:name] == name }
+ .first[:boiling_point],
+ unit
+ )
+end
+
+def from_celsius(degrees, to)
+ case to
+ when 'C' then degrees
+ when 'F' then degrees * 1.8 + 32
+ when 'K' then degrees + 273.15
+ end
+end
+
+def from_fahrenheit(degrees, to)
+ case to
+ when 'F' then degrees
+ when 'C' then (degrees - 32) / 1.8
+ when 'K' then (degrees + 459.67) / 1.8
+ end
+end
+
+def from_kelvin(degrees, to)
+ case to
+ when 'K' then degrees
+ when 'C' then degrees - 273.15
+ when 'F' then degrees * 1.8 - 459.67
+ end
+end

Мая обнови решението на 13.10.2016 12:21 (преди около 8 години)

SUBSTANCE_TEMPERATURES = [
{ name: 'water', melting_point: 0, boiling_point: 100 },
{ name: 'ethanol', melting_point: -114, boiling_point: 78.37 },
{ name: 'gold', melting_point: 1064, boiling_point: 2700 },
{ name: 'silver', melting_point: 961.8, boiling_point: 2162 },
{ name: 'copper', melting_point: 1085, boiling_point: 2567 }
].freeze
def convert_between_temperature_units(degrees, from, to)
case from
when 'C' then from_celsius(degrees, to)
when 'K' then from_kelvin(degrees, to)
else
from_fahrenheit(degrees, to)
end
end
def melting_point_of_substance(name, unit)
from_celsius(
SUBSTANCE_TEMPERATURES
.select { |substance| substance[:name] == name }
.first[:melting_point],
unit
)
end
def boiling_point_of_substance(name, unit)
from_celsius(
SUBSTANCE_TEMPERATURES
.select { |substance| substance[:name] == name }
.first[:boiling_point],
unit
)
end
def from_celsius(degrees, to)
case to
- when 'C' then degrees
when 'F' then degrees * 1.8 + 32
when 'K' then degrees + 273.15
+ else
+ degrees
end
end
def from_fahrenheit(degrees, to)
case to
- when 'F' then degrees
when 'C' then (degrees - 32) / 1.8
when 'K' then (degrees + 459.67) / 1.8
+ else
+ degrees
end
end
def from_kelvin(degrees, to)
case to
- when 'K' then degrees
when 'C' then degrees - 273.15
when 'F' then degrees * 1.8 - 459.67
+ else
+ degrees
end
end

Мая обнови решението на 14.10.2016 12:52 (преди около 8 години)

-SUBSTANCE_TEMPERATURES = [
- { name: 'water', melting_point: 0, boiling_point: 100 },
- { name: 'ethanol', melting_point: -114, boiling_point: 78.37 },
- { name: 'gold', melting_point: 1064, boiling_point: 2700 },
- { name: 'silver', melting_point: 961.8, boiling_point: 2162 },
- { name: 'copper', melting_point: 1085, boiling_point: 2567 }
-].freeze
+SUBSTANCE_TEMPERATURES = {
+ '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(degrees, from, to)
- case from
- when 'C' then from_celsius(degrees, to)
- when 'K' then from_kelvin(degrees, to)
- else
- from_fahrenheit(degrees, to)
- end
+ from_celsius(to_celsius(degrees, from), to)
end
def melting_point_of_substance(name, unit)
- from_celsius(
- SUBSTANCE_TEMPERATURES
- .select { |substance| substance[:name] == name }
- .first[:melting_point],
- unit
- )
+ from_celsius(SUBSTANCE_TEMPERATURES[name][:melting_point], unit)
end
def boiling_point_of_substance(name, unit)
- from_celsius(
- SUBSTANCE_TEMPERATURES
- .select { |substance| substance[:name] == name }
- .first[:boiling_point],
- unit
- )
+ from_celsius(SUBSTANCE_TEMPERATURES[name][:boiling_point], unit)
end
def from_celsius(degrees, to)
case to
when 'F' then degrees * 1.8 + 32
when 'K' then degrees + 273.15
- else
- degrees
+ else degrees
end
end
-def from_fahrenheit(degrees, to)
+def to_celsius(degrees, to)
case to
- when 'C' then (degrees - 32) / 1.8
- when 'K' then (degrees + 459.67) / 1.8
- else
- degrees
- end
-end
-
-def from_kelvin(degrees, to)
- case to
- when 'C' then degrees - 273.15
- when 'F' then degrees * 1.8 - 459.67
- else
- degrees
+ when 'F' then (degrees - 32) / 1.8
+ when 'K' then degrees - 273.15
+ else degrees
end
end

Много, много благодаря, Георги!
Имах чувството, че пропускам нещо с тия методи за конвертиране на една единица в друга и че има по-хитър начин.
Така? (Хм, малко странно изглежда с твоите ред по ред коментари, надявам се, че се чете.)