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

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

Към профила на Красимир Тренчев

Резултати

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

Код

# Function returns unconverted units if incorrect input is given.
def convert_between_temperature_units(units, from, to)
case from
when 'K'
convert_kelvin_to_other(units, to)
when 'F'
convert_fahrenheit_to_other(units, to)
when 'C'
convert_celsius_to_other(units, to)
else units
end
end
def convert_kelvin_to_other(kelvins, other_type)
case other_type
when 'F'
kelvins * 9.0 / 5.0 - 459.67
# Conversion formula source http://www.rapidtables.com/convert/temperature/how-kelvin-to-fahrenheit.htm
when 'C'
kelvins - 273.15
# Conversion formula source http://www.rapidtables.com/convert/temperature/how-kelvin-to-celsius.htm
else
kelvins
end
end
def convert_celsius_to_other(celsius, other_type)
case other_type
when 'F'
celsius * 9.0 / 5.0 + 32
# Conversion formula source http://www.rapidtables.com/convert/temperature/how-celsius-to-fahrenheit.htm
when 'K'
celsius + 273.15
# Conversion formula source http://www.rapidtables.com/convert/temperature/how-celsius-to-kelvin.htm
else
celsius
end
end
def convert_fahrenheit_to_other(fahrenheit, other_type)
case other_type
when 'C'
(fahrenheit - 32) * 5.0 / 9.0
# Conversion formula source http://www.rapidtables.com/convert/temperature/how-fahrenheit-to-celsius.htm
when 'K'
(fahrenheit + 459.67) * 5.0 / 9.0
# Conversion formula source http://www.rapidtables.com/convert/temperature/fahrenheit-to-kelvin.htm
else
fahrenheit
end
end
# Source http://fmi.ruby.bg/tasks/1
TEMPERATURES_DATASHEET = {
water: [0, 100],
ethanol: [-114, 78.37],
gold: [1064, 2700],
silver: [961.8, 2162],
copper: [1085, 2567]
}
MELTING = 0
BOILING = 1
# Function ignores the possiblity of incorrect input.
def boiling_point_of_substance(substance, output_unit)
input_unit = 'C'
convert_between_temperature_units(TEMPERATURES_DATASHEET[substance.to_sym][BOILING], input_unit, output_unit)
end
# Function ignores the possiblity of incorrect input.
def melting_point_of_substance(substance, output_unit)
input_unit = 'C'
convert_between_temperature_units(TEMPERATURES_DATASHEET[substance.to_sym][MELTING], input_unit, output_unit)
end

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

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

Finished in 0.00773 seconds
17 examples, 0 failures

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

Красимир обнови решението на 16.10.2016 15:34 (преди над 7 години)

+# Function returns unconverted units if incorrect input is given.
+def convert_between_temperature_units(units, from, to)
+ case from
+ when 'K'
+ convert_kelvin_to_other(units, to)
+ when 'F'
+ convert_fahrenheit_to_other(units, to)
+ when 'C'
+ convert_celsius_to_other(units, to)
+ else units
+ end
+end
+
+def convert_kelvin_to_other(kelvins, other_type)
+ case other_type
+ when 'F'
+ kelvins * 9.0 / 5.0 - 459.67
+ # Conversion formula source http://www.rapidtables.com/convert/temperature/how-kelvin-to-fahrenheit.htm
+ when 'C'
+ kelvins - 273.15
+ # Conversion formula source http://www.rapidtables.com/convert/temperature/how-kelvin-to-celsius.htm
+ else
+ kelvins
+ end
+end
+
+def convert_celsius_to_other(celsius, other_type)
+ case other_type
+ when 'F'
+ celsius * 9.0 / 5.0 + 32
+ # Conversion formula source http://www.rapidtables.com/convert/temperature/how-celsius-to-fahrenheit.htm
+ when 'K'
+ celsius + 273.15
+ # Conversion formula source http://www.rapidtables.com/convert/temperature/how-celsius-to-kelvin.htm
+ else
+ celsius
+ end
+end
+
+def convert_fahrenheit_to_other(fahrenheit, other_type)
+ case other_type
+ when 'C'
+ (fahrenheit - 32) * 5.0 / 9.0
+ # Conversion formula source http://www.rapidtables.com/convert/temperature/how-fahrenheit-to-celsius.htm
+ when 'K'
+ (fahrenheit + 459.67) * 5.0 / 9.0
+ # Conversion formula source http://www.rapidtables.com/convert/temperature/fahrenheit-to-kelvin.htm
+ else
+ fahrenheit
+ end
+end
+
+# Source http://fmi.ruby.bg/tasks/1
+TEMPERATURES_DATASHEET = {
+ water: [0, 100],
+ ethanol: [-114, 78.37],
+ gold: [1064, 2700],
+ silver: [961.8, 2162],
+ copper: [1085, 2567]
+}
+
+MELTING = 0
+BOILING = 1
+
+# Function ignores the possiblity of incorrect input.
+def boiling_point_of_substance(substance, output_unit)
+ input_unit = 'C'
+ convert_between_temperature_units(TEMPERATURES_DATASHEET[substance.to_sym][BOILING], input_unit, output_unit)
+end
+
+# Function ignores the possiblity of incorrect input.
+def melting_point_of_substance(substance, output_unit)
+ input_unit = 'C'
+ convert_between_temperature_units(TEMPERATURES_DATASHEET[substance.to_sym][MELTING], input_unit, output_unit)
+end