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

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

Към профила на Светослав Годжев

Резултати

  • 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(degrees, input_unit)
if input_unit == 'C'
degrees
elsif input_unit == 'K'
degrees - 273.15
elsif input_unit == 'F'
(degrees - 32) / 1.8
end
end
def convert_from_celsius(degrees, output_unit)
if output_unit == 'C'
degrees
elsif output_unit == 'K'
degrees + 273.15
elsif output_unit == 'F'
degrees * 1.8 + 32
end
end
def convert_between_temperature_units(degrees, input_unit, output_unit)
if input_unit == output_unit
degrees
else
temp = convert_to_celsius(degrees, input_unit)
convert_from_celsius(temp, output_unit)
end
end
def melting_point_of_substance(substance, unit)
convert_from_celsius(MELTING_POINTS[substance], unit)
end
def boiling_point_of_substance(substance, unit)
convert_from_celsius(BOILING_POINTS[substance], unit)
end

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

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

Finished in 0.00777 seconds
17 examples, 0 failures

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

Светослав обнови решението на 13.10.2016 22:49 (преди над 7 години)

+def convert_between_temperature_units(degrees, input_unit, output_unit)
+ if input_unit == 'C'
+ if output_unit == 'K'
+ degrees + 273.15
+ elsif output_unit == 'F'
+ degrees * 1.8 + 32
+ else
+ degrees
+ end
+
+ elsif input_unit == 'F'
+ if output_unit == 'C'
+ (degrees - 32) / 1.8
+ elsif output_unit == 'K'
+ (degrees - 32) / 1.8 + 273.15
+ else
+ degrees
+ end
+
+ elsif input_unit == 'K'
+ if output_unit == 'C'
+ degrees - 273.15
+ elsif output_unit == 'F'
+ (degrees - 273.15) * 1.8 + 32
+ else
+ degrees

Можеш ли да се сетиш как да промениш малко логиката по смятането, така че да правиш сметките на две стъпки? Тогава няма да имаш вложени if-ове и дори ще можеш да си разделиш кода на две помощни функции, които тук само ще се викат.

+ end
+ end
+end
+
+def melting_point_of_substance(substance, unit)
+ melting_table = { 'water' => 0, 'ethanol' => -114, 'gold' => 1_064, 'silver' => 961.8, 'copper' => 1_085 }

Какво е това? melting_table.. Ахааа! Топяща се маса! О, чакай...

Аз бих изкарал това в една константа - например MELTING_POINTS и бих сложил различните елементи на отделни редове. Така става по-ясно разделянето на код от данни, пък и е по-лесно за сканиране, вместо да трябва да скролвам хоризонтално.

+ if unit == 'C'
+ melting_table[substance]
+ else
+ convert_between_temperature_units(melting_table[substance], 'C', unit)
+ end
+end
+
+def boiling_point_of_substance(substance, unit)
+ boiling_table = { 'water' => 100, 'ethanol' => 78.37, 'gold' => 2_700, 'silver' => 2_162, 'copper' => 2_567 }
+ if unit == 'C'
+ boiling_table[substance]
+ else
+ convert_between_temperature_units(boiling_table[substance], 'C', unit)
+ end
+end

Светослав обнови решението на 16.10.2016 02:44 (преди над 7 години)

-def convert_between_temperature_units(degrees, input_unit, output_unit)
- if input_unit == 'C'
- if output_unit == 'K'
- degrees + 273.15
- elsif output_unit == 'F'
- degrees * 1.8 + 32
- else
- degrees
- end
+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(degrees, input_unit)
+ if input_unit == 'C'
+ degrees
+ elsif input_unit == 'K'
+ degrees - 273.15
elsif input_unit == 'F'
- if output_unit == 'C'
- (degrees - 32) / 1.8
- elsif output_unit == 'K'
- (degrees - 32) / 1.8 + 273.15
- else
- degrees
- end
+ (degrees - 32) / 1.8
+ end
+end
- elsif input_unit == 'K'
- if output_unit == 'C'
- degrees - 273.15
- elsif output_unit == 'F'
- (degrees - 273.15) * 1.8 + 32
- else
- degrees
- end
+def convert_from_celsius(degrees, output_unit)
+ if output_unit == 'C'
+ degrees
+ elsif output_unit == 'K'
+ degrees + 273.15
+ elsif output_unit == 'F'
+ degrees * 1.8 + 32
end
end
-def melting_point_of_substance(substance, unit)
- melting_table = { 'water' => 0, 'ethanol' => -114, 'gold' => 1_064, 'silver' => 961.8, 'copper' => 1_085 }
- if unit == 'C'
- melting_table[substance]
+def convert_between_temperature_units(degrees, input_unit, output_unit)
+ if input_unit == output_unit
+ degrees
else
- convert_between_temperature_units(melting_table[substance], 'C', unit)
+ temp = convert_to_celsius(degrees, input_unit)
+ convert_from_celsius(temp, output_unit)
end
end
+def melting_point_of_substance(substance, unit)
+ convert_from_celsius(MELTING_POINTS[substance], unit)
+end
+
def boiling_point_of_substance(substance, unit)
- boiling_table = { 'water' => 100, 'ethanol' => 78.37, 'gold' => 2_700, 'silver' => 2_162, 'copper' => 2_567 }
- if unit == 'C'
- boiling_table[substance]
- else
- convert_between_temperature_units(boiling_table[substance], 'C', unit)
- end
+ convert_from_celsius(BOILING_POINTS[substance], unit)
end