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

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

Към профила на Йордан Иванов

Резултати

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

Код

def convert_from_c(temp, output_units)
if output_units == 'C'
temp
elsif output_units == 'F'
temp * 1.8 + 32
else
temp + 273.15
end
end
def convert_from_f(temp, output_units)
if output_units == 'F'
temp
elsif output_units == 'C'
(temp - 32) * 5 / 9.0
else
(temp + 459.67) * 5 / 9.0
end
end
def convert_from_k(temp, output_units)
if output_units == 'K'
temp
elsif output_units == 'C'
temp - 273.15
else
temp * 1.8 - 459.67
end
end
def convert_between_temperature_units(temp, input_units, output_units)
if input_units == 'C'
convert_from_c(temp, output_units)
elsif input_units == 'F'
convert_from_f(temp, output_units)
else
convert_from_k(temp, output_units)
end
end
def melting_point_of_substance(substance, units)
melting_points = {
water: 0,
ethanol: -114,
gold: 1064,
silver: 961.8,
copper: 1085
}
convert_between_temperature_units(melting_points.fetch(substance.to_sym), 'C', units)
end
def boiling_point_of_substance(substance, units)
boiling_points = {
water: 100,
ethanol: 78.37,
gold: 2700,
silver: 2162,
copper: 2567
}
convert_between_temperature_units(boiling_points.fetch(substance.to_sym), 'C', units)
end

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

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

Finished in 0.00781 seconds
17 examples, 0 failures

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

Йордан обнови решението на 15.10.2016 10:10 (преди над 7 години)

+def convert_from_c(temp, output_units)
+ if output_units == 'C'
+ temp
+ elsif output_units == 'F'
+ temp * 9.0 / 5 + 32
+ else
+ temp + 273.15
+ end
+end
+
+def convert_from_f(temp, output_units)
+ if output_units == 'F'
+ temp
+ elsif output_units == 'C'
+ (temp - 32) * 5 / 9.0
+ else
+ (temp + 459.67) * 5 / 9.0
+ end
+end
+
+def convert_from_k(temp, output_units)
+ if output_units == 'K'
+ temp
+ elsif output_units == 'C'
+ temp - 273.15
+ else
+ temp * 9.0 / 5 - 459.67
+ end
+end
+
+def convert_between_temperature_units(temp, input_units, output_units)
+ if input_units == 'C'
+ convert_from_C(temp, output_units)
+ elsif input_units == 'F'
+ convert_from_F(temp, output_units)
+ else
+ convert_from_K(temp, output_units)
+ end
+end
+
+def melting_point_of_substance(substance, units)
+ if substance == 'water'
+ convert_between_temperature_units(0, 'C', units)
+ elsif substance == 'ethanol'

Тук Hash-овете, за които говорихме на последната лекция, биха влезли в много уместна употреба. Така може да си дефинираш двойките "substance" => temperature, и да достъпиш точно температурата на веществото, което ти трябва, вместо да имаш if/elsif/elsif/elsif

+ convert_between_temperature_units(-114, 'C', units)
+ elsif substance == 'gold'
+ convert_between_temperature_units(1064, 'C', units)
+ elsif substance == 'silver'
+ convert_between_temperature_units(961.8, 'C', units)
+ elsif substance == 'copper'
+ convert_between_temperature_units(1085, 'C', units)
+ end
+end
+
+def boiling_point_of_substance(substance, units)
+ if substance == 'water'
+ convert_between_temperature_units(100, 'C', units)
+ elsif substance == 'ethanol'
+ convert_between_temperature_units(78.37, 'C', units)
+ elsif substance == 'gold'
+ convert_between_temperature_units(2700, 'C', units)
+ elsif substance == 'silver'
+ convert_between_temperature_units(2162, 'C', units)
+ elsif substance == 'copper'
+ convert_between_temperature_units(2567, 'C', units)
+ end
+end

Йордан обнови решението на 16.10.2016 16:46 (преди над 7 години)

def convert_from_c(temp, output_units)
if output_units == 'C'
temp
elsif output_units == 'F'
- temp * 9.0 / 5 + 32
+ temp * 1.8 + 32
else
temp + 273.15
end
end
def convert_from_f(temp, output_units)
if output_units == 'F'
temp
elsif output_units == 'C'
(temp - 32) * 5 / 9.0
else
(temp + 459.67) * 5 / 9.0
end
end
def convert_from_k(temp, output_units)
if output_units == 'K'
temp
elsif output_units == 'C'
temp - 273.15
else
- temp * 9.0 / 5 - 459.67
+ temp * 1.8 - 459.67
end
end
def convert_between_temperature_units(temp, input_units, output_units)
if input_units == 'C'
- convert_from_C(temp, output_units)
+ convert_from_c(temp, output_units)
elsif input_units == 'F'
- convert_from_F(temp, output_units)
+ convert_from_f(temp, output_units)
else
- convert_from_K(temp, output_units)
+ convert_from_k(temp, output_units)
end
end
def melting_point_of_substance(substance, units)
- if substance == 'water'
- convert_between_temperature_units(0, 'C', units)
- elsif substance == 'ethanol'
- convert_between_temperature_units(-114, 'C', units)
- elsif substance == 'gold'
- convert_between_temperature_units(1064, 'C', units)
- elsif substance == 'silver'
- convert_between_temperature_units(961.8, 'C', units)
- elsif substance == 'copper'
- convert_between_temperature_units(1085, 'C', units)
- end
+ melting_points = {
+ water: 0,
+ ethanol: -114,
+ gold: 1064,
+ silver: 961.8,
+ copper: 1085
+ }
+ convert_between_temperature_units(melting_points.fetch(substance.to_sym), 'C', units)
end
def boiling_point_of_substance(substance, units)
- if substance == 'water'
- convert_between_temperature_units(100, 'C', units)
+ boiling_points = {
- elsif substance == 'ethanol'
+ water: 100,
- convert_between_temperature_units(78.37, 'C', units)
+ ethanol: 78.37,
- elsif substance == 'gold'
+ gold: 2700,
- convert_between_temperature_units(2700, 'C', units)
+ silver: 2162,
- elsif substance == 'silver'
+ copper: 2567
- convert_between_temperature_units(2162, 'C', units)
+ }
- elsif substance == 'copper'
+ convert_between_temperature_units(boiling_points.fetch(substance.to_sym), 'C', units)
- convert_between_temperature_units(2567, 'C', units)
+end
- end
-end