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

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

Към профила на Богомила Пенева

Резултати

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

Код

def convert_between_temperature_units(degrees, degrees_unit, result_unit)
if degrees_unit == 'C' && result_unit == 'K'
degrees += 273.15
elsif degrees_unit == 'C' && result_unit == 'C'
degrees
elsif degrees_unit == 'C' && result_unit == 'F'
degrees * 1.8000 + 32
elsif degrees_unit == 'K' && result_unit == 'K'
degrees
elsif degrees_unit == 'K' && result_unit == 'C'
degrees -= 273.15
elsif degrees_unit == 'K' && result_unit == 'F'
(degrees - 273.15) * 1.8000 + 32.00
elsif degrees_unit == 'F' && result_unit == 'F'
degrees
elsif degrees_unit == 'F' && result_unit == 'C'
(degrees - 32.00) / 1.8
else
(degrees + 459.67) / 1.8
end
end
def melting_point_of_substance(substance, degrees)
if substance == "water" && degrees == 'C'
0
elsif substance == "water" && degrees == 'K'
273.15
elsif substance == "water" && degrees == 'F'
32.00
elsif substance == "ethanol" && degrees == 'C'
-114.00
elsif substance == "ethanol" && degrees == 'K'
159.15
elsif substance == "ethanol" && degrees == 'F'
-173.2
elsif substance == "gold" && degrees == 'C'
1_064
elsif substance == "gold" && degrees == 'F'
1947.20
elsif substance == "gold" && degrees == 'K'
1337.15
elsif substance == "silver" && degrees == 'C'
961.8
elsif substance == "silver" && degrees == 'F'
1763.24
elsif substance == "silver" && degrees == 'K'
1234.95
elsif substance == "copper" && degrees == 'C'
1_085
elsif substance == "copper" && degrees == 'K'
1358.15
else
1985.00
end
end
def boiling_point_of_substance(substance, degrees)
if substance == "water" && degrees == 'C'
100
elsif substance == "water" && degrees == 'F'
212.00
elsif substance == "water" && degrees == 'K'
373.15
elsif substance == "ethanol" && degrees == 'C'
78.37
elsif substance == "ethanol" && degrees == 'F'
173.066
elsif substance == "ethanol" && degrees == 'K'
351.52
elsif substance == "gold" && degrees == 'C'
2_700
elsif substance == "gold" && degrees == 'F'
4892.00
elsif substance == "gold" && degrees == 'K'
2973.15
elsif substance == "silver" && degrees == 'C'
2_162
elsif substance == "silver" && degrees == 'F'
3923.60
elsif substance == "silver" && degrees == 'K'
2435.15
elsif substance == "copper" && degrees == 'C'
2_567
elsif substance == "copper" && degrees == 'F'
4652.60
else
2840.15
end
end

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

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

Finished in 0.00771 seconds
17 examples, 0 failures

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

Богомила обнови решението на 16.10.2016 19:39 (преди около 8 години)

+def convert_between_temperature_units(num, deg1, deg2)
+ if deg1 == 'C' && deg2 == 'K'
+ num += 273.15
+ elsif deg1 == 'C' && deg2 == 'C'
+ num
+ elsif deg1 == 'C' && deg2 == 'F'
+ num = num * 1.8000 + 32
+ elsif deg1 == 'K' && deg2 == 'K'
+ num
+ elsif deg1 == 'K' && deg2 == 'C'
+ num -= 273.15
+ elsif deg1 == 'K' && deg2 == 'F'
+ (num - 273.15) * 1.8000 + 32.00
+ elsif deg1 == 'F' && deg2 == 'F'
+ num
+ elsif deg1 == 'F' && deg2 == 'C'
+ (num - 32.00) / 1.8
+ else
+ (num + 459.67) / 1.8
+ end
+end
+
+def melting_point_of_substance(subs, deg)
+ if subs == "water" && deg == 'C'
+ 0
+ elsif subs == "water" && deg == 'K'
+ 273.15
+ elsif subs == "water" && deg == 'F'
+ 32.00
+ elsif subs == "ethanol" && deg == 'C'
+ -114.00
+ elsif subs == "ethanol" && deg == 'K'
+ 159.15
+ elsif subs == "ethanol" && deg == 'F'
+ -173.2
+ elsif subs == "gold" && deg == 'C'
+ 1_064
+ elsif subs == "gold" && deg == 'F'
+ 1947.20
+ elsif subs == "gold" && deg == 'K'
+ 1337.15
+ elsif subs == "silver" && deg == 'C'
+ 961.8
+ elsif subs == "silver" && deg == 'F'
+ 1763.24
+ elsif subs == "silver" && deg == 'K'
+ 1234.95
+ elsif subs == "copper" && deg == 'C'
+ 1_085
+ elsif subs == "copper" && deg == 'K'
+ 1358.15
+ else
+ 1985.00
+ end
+end
+
+def boiling_point_of_substance(subs, deg)
+ if subs == "water" && deg == 'C'
+ 100
+ elsif subs == "water" && deg == 'F'
+ 212.00
+ elsif subs == "water" && deg == 'K'
+ 373.15
+ elsif subs == "ethanol" && deg == 'C'
+ 78.37
+ elsif subs == "ethanol" && deg == 'F'
+ 173.066
+ elsif subs == "ethanol" && deg == 'K'
+ 351.52
+ elsif subs == "gold" && deg == 'C'
+ 2_700
+ elsif subs == "gold" && deg == 'F'
+ 4892.00
+ elsif subs == "gold" && deg == 'K'
+ 2973.15
+ elsif subs == "silver" && deg == 'C'
+ 2_162
+ elsif subs == "silver" && deg == 'F'
+ 3923.60
+ elsif subs == "silver" && deg == 'K'
+ 2435.15
+ elsif subs == "copper" && deg == 'C'
+ 2_567
+ elsif subs == "copper" && deg == 'F'
+ 4652.60
+ else
+ 2840.15
+ end
+end

Не съкращавай имена на променливи по този начин. Те трябва да разказват история на читателя, добра евристика е - може ли да прочетеш кода си дословно като изречение.

Опитай се да измисиш някакъв начин намалиш броя на случаите, които разглеждаш. Възможни решения в тази насока - използване на хешове, викане на един метод от друг, помощни методи, конвертиране към някаква базова мерна единица.

Прегледай нашето и няколко други решения след крайния срок.

Богомила обнови решението на 16.10.2016 23:40 (преди около 8 години)

-def convert_between_temperature_units(num, deg1, deg2)
- if deg1 == 'C' && deg2 == 'K'
- num += 273.15
- elsif deg1 == 'C' && deg2 == 'C'
- num
- elsif deg1 == 'C' && deg2 == 'F'
- num = num * 1.8000 + 32
- elsif deg1 == 'K' && deg2 == 'K'
- num
- elsif deg1 == 'K' && deg2 == 'C'
- num -= 273.15
- elsif deg1 == 'K' && deg2 == 'F'
- (num - 273.15) * 1.8000 + 32.00
- elsif deg1 == 'F' && deg2 == 'F'
- num
- elsif deg1 == 'F' && deg2 == 'C'
- (num - 32.00) / 1.8
+def convert_between_temperature_units(degrees, degrees_unit, result_unit)
+ if degrees_unit == 'C' && result_unit == 'K'
+ degrees += 273.15
+ elsif degrees_unit == 'C' && result_unit == 'C'
+ degrees
+ elsif degrees_unit == 'C' && result_unit == 'F'
+ degrees * 1.8000 + 32
+ elsif degrees_unit == 'K' && result_unit == 'K'
+ degrees
+ elsif degrees_unit == 'K' && result_unit == 'C'
+ degrees -= 273.15
+ elsif degrees_unit == 'K' && result_unit == 'F'
+ (degrees - 273.15) * 1.8000 + 32.00
+ elsif degrees_unit == 'F' && result_unit == 'F'
+ degrees
+ elsif degrees_unit == 'F' && result_unit == 'C'
+ (degrees - 32.00) / 1.8
else
- (num + 459.67) / 1.8
+ (degrees + 459.67) / 1.8
end
end
-def melting_point_of_substance(subs, deg)
- if subs == "water" && deg == 'C'
+def melting_point_of_substance(substance, degrees)
+ if substance == "water" && degrees == 'C'
0
- elsif subs == "water" && deg == 'K'
+ elsif substance == "water" && degrees == 'K'
273.15
- elsif subs == "water" && deg == 'F'
+ elsif substance == "water" && degrees == 'F'
32.00
- elsif subs == "ethanol" && deg == 'C'
+ elsif substance == "ethanol" && degrees == 'C'
-114.00
- elsif subs == "ethanol" && deg == 'K'
+ elsif substance == "ethanol" && degrees == 'K'
159.15
- elsif subs == "ethanol" && deg == 'F'
+ elsif substance == "ethanol" && degrees == 'F'
-173.2
- elsif subs == "gold" && deg == 'C'
+ elsif substance == "gold" && degrees == 'C'
1_064
- elsif subs == "gold" && deg == 'F'
+ elsif substance == "gold" && degrees == 'F'
1947.20
- elsif subs == "gold" && deg == 'K'
+ elsif substance == "gold" && degrees == 'K'
1337.15
- elsif subs == "silver" && deg == 'C'
+ elsif substance == "silver" && degrees == 'C'
961.8
- elsif subs == "silver" && deg == 'F'
+ elsif substance == "silver" && degrees == 'F'
1763.24
- elsif subs == "silver" && deg == 'K'
+ elsif substance == "silver" && degrees == 'K'
1234.95
- elsif subs == "copper" && deg == 'C'
+ elsif substance == "copper" && degrees == 'C'
1_085
- elsif subs == "copper" && deg == 'K'
+ elsif substance == "copper" && degrees == 'K'
1358.15
else
1985.00
end
end
-def boiling_point_of_substance(subs, deg)
- if subs == "water" && deg == 'C'
+def boiling_point_of_substance(substance, degrees)
+ if substance == "water" && degrees == 'C'
100
- elsif subs == "water" && deg == 'F'
+ elsif substance == "water" && degrees == 'F'
212.00
- elsif subs == "water" && deg == 'K'
+ elsif substance == "water" && degrees == 'K'
373.15
- elsif subs == "ethanol" && deg == 'C'
+ elsif substance == "ethanol" && degrees == 'C'
78.37
- elsif subs == "ethanol" && deg == 'F'
+ elsif substance == "ethanol" && degrees == 'F'
173.066
- elsif subs == "ethanol" && deg == 'K'
+ elsif substance == "ethanol" && degrees == 'K'
351.52
- elsif subs == "gold" && deg == 'C'
+ elsif substance == "gold" && degrees == 'C'
2_700
- elsif subs == "gold" && deg == 'F'
+ elsif substance == "gold" && degrees == 'F'
4892.00
- elsif subs == "gold" && deg == 'K'
+ elsif substance == "gold" && degrees == 'K'
2973.15
- elsif subs == "silver" && deg == 'C'
+ elsif substance == "silver" && degrees == 'C'
2_162
- elsif subs == "silver" && deg == 'F'
+ elsif substance == "silver" && degrees == 'F'
3923.60
- elsif subs == "silver" && deg == 'K'
+ elsif substance == "silver" && degrees == 'K'
2435.15
- elsif subs == "copper" && deg == 'C'
+ elsif substance == "copper" && degrees == 'C'
2_567
- elsif subs == "copper" && deg == 'F'
+ elsif substance == "copper" && degrees == 'F'
4652.60
else
2840.15
end
end