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

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

Към профила на Михаил Здравков

Резултати

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

Код

CRITICAL_TEMPERATURES = {
water: {melting: 0, boiling: 100},
ethanol: {melting: -114, boiling: 78.37},
gold: {melting: 1064, boiling: 2700},
silver: {melting: 961.8, boiling: 2162},
copper: {melting: 1085, boiling: 2567}
}
def fahrenheit_to_celsius(degrees)
(degrees - 32) * 5.0 / 9.0
end
def celsius_to_fahrenheit(degrees)
degrees * 9.0 / 5.0 + 32
end
def kelvin_to_celsius(degrees)
degrees - 273.15
end
def celsius_to_kelvin(degrees)
degrees + 273.15
end
def to_celsius(degrees, input_unit)
case input_unit
when 'F' then fahrenheit_to_celsius degrees
when 'K' then kelvin_to_celsius degrees
else degrees
end
end
def from_celsius(degrees, output_unit)
case output_unit
when 'F' then celsius_to_fahrenheit degrees
when 'K' then celsius_to_kelvin degrees
else degrees
end
end
def convert_between_temperature_units(degrees, input_unit, output_unit)
from_celsius(to_celsius(degrees, input_unit), output_unit)
end
def melting_point_of_substance(substance, unit)
melting_temperature = CRITICAL_TEMPERATURES[substance.to_sym][:melting]
convert_between_temperature_units melting_temperature, 'C', unit
end
def boiling_point_of_substance(substance, unit)
boiling_temperature = CRITICAL_TEMPERATURES[substance.to_sym][:boiling]
convert_between_temperature_units boiling_temperature, 'C', unit
end

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

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

Finished in 0.00787 seconds
17 examples, 0 failures

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

Михаил обнови решението на 16.10.2016 20:52 (преди над 7 години)

+
+def temps(substance)
+ temperatures = {
+ water: [0, 100],
+ ethanol: [-114, 78.37],
+ gold: [1064, 2700],
+ silver: [961.8, 2162],
+ copper: [1085, 2567]
+ }
+ temperatures[substance]
+end
+
+def fahrenheit_to_celsius(degree)
+ (degree - 32) * 5.0 / 9.0
+end
+
+def celsius_to_fahrenheit(degree)
+ degree * 9.0 / 5.0 + 32
+end
+
+def kelvin_to_celsius(degree)
+ degree - 273.15
+end
+
+def celsius_to_kelvin(degree)
+ degree + 273.15
+end
+
+def to_celsius(degree, input_unit)
+ case input_unit
+ when 'F' then fahrenheit_to_celsius degree
+ when 'K' then kelvin_to_celsius degree
+ else degree
+ end
+end
+
+def from_celsius(degree, output_unit)
+ case output_unit
+ when 'F' then celsius_to_fahrenheit degree
+ when 'K' then celsius_to_kelvin degree
+ else degree
+ end
+end
+
+def convert_between_temperature_units(degree, input_unit, output_unit)
+ from_celsius(to_celsius(degree, input_unit), output_unit)
+end
+
+def melting_point_of_substance(substance, unit)
+ convert_between_temperature_units temps[substance.to_sym][0], 'C', unit
+end
+
+def boiling_point_of_substance(substance, unit)
+ convert_between_temperature_units temps[substance.to_sym][1], 'C', unit
+end

Михаил обнови решението на 16.10.2016 20:53 (преди над 7 години)

def temps(substance)

Имам 2 въпроса за това:

  1. Върти ми се в главата нещо за ограничение от 7 реда в метод. Има ли такова нещо? (Rubocop-а явно няма такова изискване)

  2. Смятам, че това е доста по-смислено да е глобална променлива, независимо, че разни хора смятат, че глобалните променливи са абсолютното зло. Rubocop-а се оплаква от глобалните променливи, затова го направих с метод, но това е кофти: хем кода е по-грозен, хем на всяко извикване на метода се инициализира хеша наново. Какво е вашето мнение по въпроса?

  1. Не сме поставили ограничения за тази задача.
  2. Може да използваш константа.

Все пак помисли за по-добро име. Не съкращавай по този начин. Измисли нещо по-конкретно от temperatures.

temperatures = {
water: [0, 100],
ethanol: [-114, 78.37],
gold: [1064, 2700],
silver: [961.8, 2162],
copper: [1085, 2567]
}
temperatures[substance]
end
(degree - 32) * 5.0 / 9.0
end
def celsius_to_fahrenheit(degree)
degree * 9.0 / 5.0 + 32
end
def kelvin_to_celsius(degree)
degree - 273.15
end
def celsius_to_kelvin(degree)
degree + 273.15
end
def to_celsius(degree, input_unit)
case input_unit
when 'F' then fahrenheit_to_celsius degree
when 'K' then kelvin_to_celsius degree
else degree
end
end
def from_celsius(degree, output_unit)
case output_unit
when 'F' then celsius_to_fahrenheit degree
when 'K' then celsius_to_kelvin degree
else degree
end
end
def convert_between_temperature_units(degree, input_unit, output_unit)
from_celsius(to_celsius(degree, input_unit), output_unit)
end
def melting_point_of_substance(substance, unit)
- convert_between_temperature_units temps[substance.to_sym][0], 'C', unit
+ convert_between_temperature_units temps(substance.to_sym)[0], 'C', unit
end
def boiling_point_of_substance(substance, unit)
- convert_between_temperature_units temps[substance.to_sym][1], 'C', unit
+ convert_between_temperature_units temps(substance.to_sym)[1], 'C', unit
end

Михаил обнови решението на 16.10.2016 22:15 (преди над 7 години)

+CRITICAL_TEMPERATURES = {
+ water: {melting: 0, boiling: 100},
+ ethanol: {melting: -114, boiling: 78.37},
+ gold: {melting: 1064, boiling: 2700},
+ silver: {melting: 961.8, boiling: 2162},
+ copper: {melting: 1085, boiling: 2567}
+}
-def temps(substance)
- temperatures = {
- water: [0, 100],
- ethanol: [-114, 78.37],
- gold: [1064, 2700],
- silver: [961.8, 2162],
- copper: [1085, 2567]
- }
- temperatures[substance]
+def fahrenheit_to_celsius(degrees)
+ (degrees - 32) * 5.0 / 9.0
end
-def fahrenheit_to_celsius(degree)
- (degree - 32) * 5.0 / 9.0
+def celsius_to_fahrenheit(degrees)
+ degrees * 9.0 / 5.0 + 32
end
-def celsius_to_fahrenheit(degree)
- degree * 9.0 / 5.0 + 32
+def kelvin_to_celsius(degrees)
+ degrees - 273.15
end
-def kelvin_to_celsius(degree)
- degree - 273.15
+def celsius_to_kelvin(degrees)
+ degrees + 273.15
end
-def celsius_to_kelvin(degree)
- degree + 273.15
-end
-
-def to_celsius(degree, input_unit)
+def to_celsius(degrees, input_unit)
case input_unit
- when 'F' then fahrenheit_to_celsius degree
- when 'K' then kelvin_to_celsius degree
- else degree
+ when 'F' then fahrenheit_to_celsius degrees
+ when 'K' then kelvin_to_celsius degrees
+ else degrees
end
end
-def from_celsius(degree, output_unit)
+def from_celsius(degrees, output_unit)
case output_unit
- when 'F' then celsius_to_fahrenheit degree
- when 'K' then celsius_to_kelvin degree
- else degree
+ when 'F' then celsius_to_fahrenheit degrees
+ when 'K' then celsius_to_kelvin degrees
+ else degrees
end
end
-def convert_between_temperature_units(degree, input_unit, output_unit)
- from_celsius(to_celsius(degree, input_unit), output_unit)
+def convert_between_temperature_units(degrees, input_unit, output_unit)
+ from_celsius(to_celsius(degrees, input_unit), output_unit)
end
def melting_point_of_substance(substance, unit)
- convert_between_temperature_units temps(substance.to_sym)[0], 'C', unit
+ melting_temperature = CRITICAL_TEMPERATURES[substance.to_sym][:melting]
+ convert_between_temperature_units melting_temperature, 'C', unit
end
def boiling_point_of_substance(substance, unit)
- convert_between_temperature_units temps(substance.to_sym)[1], 'C', unit
-end
+ boiling_temperature = CRITICAL_TEMPERATURES[substance.to_sym][:boiling]
+ convert_between_temperature_units boiling_temperature, 'C', unit
+end

Михаил обнови решението на 16.10.2016 22:16 (преди над 7 години)

CRITICAL_TEMPERATURES = {
- water: {melting: 0, boiling: 100},
- ethanol: {melting: -114, boiling: 78.37},
- gold: {melting: 1064, boiling: 2700},
- silver: {melting: 961.8, boiling: 2162},
- copper: {melting: 1085, boiling: 2567}
+ water: {melting: 0, boiling: 100},
+ ethanol: {melting: -114, boiling: 78.37},
+ gold: {melting: 1064, boiling: 2700},
+ silver: {melting: 961.8, boiling: 2162},
+ copper: {melting: 1085, boiling: 2567}
}
def fahrenheit_to_celsius(degrees)
(degrees - 32) * 5.0 / 9.0
end
def celsius_to_fahrenheit(degrees)
degrees * 9.0 / 5.0 + 32
end
def kelvin_to_celsius(degrees)
degrees - 273.15
end
def celsius_to_kelvin(degrees)
degrees + 273.15
end
def to_celsius(degrees, input_unit)
case input_unit
when 'F' then fahrenheit_to_celsius degrees
when 'K' then kelvin_to_celsius degrees
else degrees
end
end
def from_celsius(degrees, output_unit)
case output_unit
when 'F' then celsius_to_fahrenheit degrees
when 'K' then celsius_to_kelvin degrees
else degrees
end
end
def convert_between_temperature_units(degrees, input_unit, output_unit)
from_celsius(to_celsius(degrees, input_unit), output_unit)
end
def melting_point_of_substance(substance, unit)
melting_temperature = CRITICAL_TEMPERATURES[substance.to_sym][:melting]
convert_between_temperature_units melting_temperature, 'C', unit
end
def boiling_point_of_substance(substance, unit)
boiling_temperature = CRITICAL_TEMPERATURES[substance.to_sym][:boiling]
convert_between_temperature_units boiling_temperature, 'C', unit
end