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

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

Към профила на Мила Бянкова

Резултати

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

Код

UNITS = {celsius: 'C', fahrenheit: 'F', kelvin: 'K'}
def convert_between_temperature_units(degrees, unit_from, unit_to)
if unit_from == unit_to
degrees
elsif unit_from == UNITS[:celsius] || unit_to == UNITS[:celsius]
calculate_temperature(degrees, unit_from, unit_to)
else
degrees_in_celsius =
calculate_temperature(degrees, unit_from, UNITS[:celsius])
calculate_temperature(degrees_in_celsius, UNITS[:celsius], unit_to)
end
end
def calculate_temperature(degrees, unit_from, unit_to)
celsius_to_kelvin_difference = 273.15
celsius_to_fahrenheit_ratio = 1.8
celsius_to_fahrenheit_difference = 32.0
case unit_from
when UNITS[:celsius]
if unit_to == UNITS[:kelvin]
degrees + celsius_to_kelvin_difference
elsif unit_to == UNITS[:fahrenheit]
(degrees * celsius_to_fahrenheit_ratio) +
celsius_to_fahrenheit_difference
end
when UNITS[:kelvin]
degrees - celsius_to_kelvin_difference
when UNITS[:fahrenheit]
(degrees - celsius_to_fahrenheit_difference) *
(1 / celsius_to_fahrenheit_ratio)
end
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 melting_point_of_substance(substance, unit)
convert_between_temperature_units(MELTING_POINTS[substance], 'C', unit)
end
def boiling_point_of_substance(substance, unit)
convert_between_temperature_units(BOILING_POINTS[substance], 'C', unit)
end

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

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

Finished in 0.00792 seconds
17 examples, 0 failures

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

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

+def convert_between_temperature_units(degrees, unit_from, unit_to)
+ case [unit_from, unit_to]
+ when ['C', 'F']
+ (degrees * 1.8) + 32
+ when ['C', 'K']
+ degrees + 273.15
+ when ['C', 'C']
+ degrees
+ when ['F', 'F']
+ degrees
+ when ['F', 'K']
+ (degrees - 32.0) * Rational(5, 9) + 273.15
+ when ['F', 'C']
+ (degrees - 32.0) * Rational(5, 9)
+ when ['K', 'F']
+ (degrees - 273.15) * 1.8 + 32
+ when ['K', 'K']
+ degrees
+ when ['K', 'C']

Хм, доста интересно използване на when-а :)

Тук имаш N * N случая. Можеш ли да се сетиш как може да го направиш с N + N случая? Тоест, вместо switch по комбинации (с наредба) от две неща, да имаш два switch-а по един вид нещо.

В този вид, ако решиш да добавиш нова мерна единица ще трябва да сложиш 7 нови случая. В другия ще трябва да сложиш 2.

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

Points of melting не е правилен английски. Трябва да е melting points.

Аз бих изкарал това в една константа над функцията + всяко нещо да е на отделен ред. Ще стане много по-ясно разделението между логика и данни. Пък и по-ясно за окото.

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

Мила обнови решението на 16.10.2016 16:26 (преди около 8 години)

def convert_between_temperature_units(degrees, unit_from, unit_to)
- case [unit_from, unit_to]
- when ['C', 'F']
- (degrees * 1.8) + 32
- when ['C', 'K']
- degrees + 273.15
- when ['C', 'C']
+ if unit_from == unit_to
degrees
- when ['F', 'F']
- degrees
- when ['F', 'K']
- (degrees - 32.0) * Rational(5, 9) + 273.15
- when ['F', 'C']
- (degrees - 32.0) * Rational(5, 9)
- when ['K', 'F']
- (degrees - 273.15) * 1.8 + 32
- when ['K', 'K']
- degrees
- when ['K', 'C']
+ elsif unit_from == 'C' || unit_to == 'C'
+ calculate_conversion_between_temperature_units(degrees, unit_from, unit_to)
+ else
+ degrees_in_celsius =
+ calculate_conversion_between_temperature_units(degrees, unit_from, 'C')
+ calculate_conversion_between_temperature_units(degrees_in_celsius, 'C', unit_to)
+ end
+end
+
+def calculate_conversion_between_temperature_units(degrees, unit_from, unit_to)
+ case unit_from
+ when 'C'
+ if unit_to == 'K'
+ degrees + 273.15
+ elsif unit_to == 'F'
+ (degrees * 1.8) + 32
+ end
+ when 'K'
degrees - 273.15
+ when 'F'
+ (degrees - 32.0) * 0.5556
end
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 melting_point_of_substance(substance, unit)
- points_of_melting = { 'water' => 0, 'ethanol' => -114, 'gold' => 1_064, 'silver' => 961.8, 'copper' => 1_085 }
- convert_between_temperature_units(points_of_melting[substance], 'C', unit)
+ convert_between_temperature_units(MELTING_POINTS[substance], 'C', unit)
end
def boiling_point_of_substance(substance, unit)
- points_of_boiling = { 'water' => 100, 'ethanol' => 78.37, 'gold' => 2_700, 'silver' => 2_162, 'copper' => 2_567 }
- convert_between_temperature_units(points_of_boiling[substance], 'C', unit)
+ convert_between_temperature_units(BOILING_POINTS[substance], 'C', unit)
end

Мила обнови решението на 17.10.2016 00:34 (преди около 8 години)

+UNITS = {celsius: 'C', fahrenheit: 'F', kelvin: 'K'}
+
def convert_between_temperature_units(degrees, unit_from, unit_to)
if unit_from == unit_to
degrees
- elsif unit_from == 'C' || unit_to == 'C'
- calculate_conversion_between_temperature_units(degrees, unit_from, unit_to)
+ elsif unit_from == UNITS[:celsius] || unit_to == UNITS[:celsius]
+ calculate_temperature(degrees, unit_from, unit_to)
else
degrees_in_celsius =
- calculate_conversion_between_temperature_units(degrees, unit_from, 'C')
- calculate_conversion_between_temperature_units(degrees_in_celsius, 'C', unit_to)
+ calculate_temperature(degrees, unit_from, UNITS[:celsius])
+ calculate_temperature(degrees_in_celsius, UNITS[:celsius], unit_to)
end
end
-def calculate_conversion_between_temperature_units(degrees, unit_from, unit_to)
+def calculate_temperature(degrees, unit_from, unit_to)
+ celsius_to_kelvin_difference = 273.15
+ celsius_to_fahrenheit_ratio = 1.8
+ celsius_to_fahrenheit_difference = 32.0
case unit_from
- when 'C'
- if unit_to == 'K'
- degrees + 273.15
- elsif unit_to == 'F'
- (degrees * 1.8) + 32
+ when UNITS[:celsius]
+ if unit_to == UNITS[:kelvin]
+ degrees + celsius_to_kelvin_difference
+ elsif unit_to == UNITS[:fahrenheit]
+ (degrees * celsius_to_fahrenheit_ratio) +
+ celsius_to_fahrenheit_difference
end
- when 'K'
- degrees - 273.15
- when 'F'
- (degrees - 32.0) * 0.5556
+ when UNITS[:kelvin]
+ degrees - celsius_to_kelvin_difference
+ when UNITS[:fahrenheit]
+ (degrees - celsius_to_fahrenheit_difference) *
+ (1 / celsius_to_fahrenheit_ratio)
end
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 melting_point_of_substance(substance, unit)
convert_between_temperature_units(MELTING_POINTS[substance], 'C', unit)
end
def boiling_point_of_substance(substance, unit)
convert_between_temperature_units(BOILING_POINTS[substance], 'C', unit)
end