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

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

Към профила на Радослав Гайдаров

Резултати

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

Код

MELTING_POINTS = {'water' => 0, 'ethanol' => -114, 'gold' => 1064, 'silver' => 961.8, 'copper' => 1085}
BOILING_POINTS = {'water' => 100, 'ethanol' => 78.37, 'gold' => 2700, 'silver' => 2162, 'copper' => 2567}
def from_celsius(degrees, wanted_unit)
if wanted_unit == 'F'
degrees * 1.8 + 32
elsif wanted_unit == 'K'
degrees + 273.15
else
degrees
end
end
def to_celsius(degrees, current_unit)
if current_unit == 'F'
(degrees - 32) / 1.8
elsif current_unit == 'K'
degrees - 273.15
else
degrees
end
end
def convert_between_temperature_units(degrees, current_unit, wanted_unit)
from_celsius(to_celsius(degrees, current_unit), wanted_unit)
end
def melting_point_of_substance(substance, units)
from_celsius(MELTING_POINTS[substance], units)
end
def boiling_point_of_substance(substance, units)
from_celsius(BOILING_POINTS[substance], units)
end

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

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

Finished in 0.00985 seconds
17 examples, 0 failures

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

Радослав обнови решението на 14.10.2016 22:02 (преди над 7 години)

+def from_celsius(degrees, wanted_unit)
+ if wanted_unit == 'F'
+ (degrees * 9) / 5.0 + 32
+ elsif wanted_unit == 'K'
+ degrees + 273.15
+ else
+ degrees
+ end
+end
+
+def to_celsius(degrees, current_unit)
+ if current_unit == 'F'
+ ((degrees - 32) * 5) / 9.0
+ elsif current_unit == 'K'
+ degrees - 273.15
+ else
+ degrees
+ end
+end
+
+def convert_between_temperature_units(degrees, current_unit, wanted_unit)
+ from_celsius(to_celsius(degrees, current_unit), wanted_unit)
+end
+
+def melting_point_of_substance(substance, units)
+ substances = {'water' => 0, 'ethanol' => -114, 'gold' => 1064, 'silver' => 961.8, 'copper' => 1085}

Името на този hash би могло по-добре да описва какво съдържа. В него има точки на топене на различни елементи, следователно нещо от рода на melting_points е по-описателно. Съответно и вземането на температурата по-долу би изглеждало като melting_points[substance], което веднага ти казва какво има в резултата. Също, това е дреболия, но ще е малко по-прегледно да го дефинираш като константа в началото на файла, вместо да е вътре в метода. :)

+ from_celsius(substances[substance], units)
+end
+
+def boiling_point_of_substance(substance, units)
+ substances = {'water' => 100, 'ethanol' => 78.37, 'gold' => 2700, 'silver' => 2162, 'copper' => 2567}
+ from_celsius(substances[substance], units)
+end

Радослав обнови решението на 16.10.2016 00:15 (преди над 7 години)

+MELTING_POINTS = {'water' => 0, 'ethanol' => -114, 'gold' => 1064, 'silver' => 961.8, 'copper' => 1085}
+BOILING_POINTS = {'water' => 100, 'ethanol' => 78.37, 'gold' => 2700, 'silver' => 2162, 'copper' => 2567}
+
def from_celsius(degrees, wanted_unit)
if wanted_unit == 'F'
- (degrees * 9) / 5.0 + 32
+ degrees * 1.8 + 32
elsif wanted_unit == 'K'
degrees + 273.15
else
degrees
end
end
def to_celsius(degrees, current_unit)
if current_unit == 'F'
- ((degrees - 32) * 5) / 9.0
+ (degrees - 32) / 1.8
elsif current_unit == 'K'
degrees - 273.15
else
degrees
end
end
def convert_between_temperature_units(degrees, current_unit, wanted_unit)
from_celsius(to_celsius(degrees, current_unit), wanted_unit)
end
def melting_point_of_substance(substance, units)
- substances = {'water' => 0, 'ethanol' => -114, 'gold' => 1064, 'silver' => 961.8, 'copper' => 1085}
- from_celsius(substances[substance], units)
+ from_celsius(MELTING_POINTS[substance], units)
end
def boiling_point_of_substance(substance, units)
- substances = {'water' => 100, 'ethanol' => 78.37, 'gold' => 2700, 'silver' => 2162, 'copper' => 2567}
- from_celsius(substances[substance], units)
+ from_celsius(BOILING_POINTS[substance], units)
end