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

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

Към профила на Дарин Тодоров

Резултати

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

Код

# Temperature Converter
def convert_between_temperature_units(degree, first_unit, second_unit)
case [first_unit, second_unit]
when ['C', 'K']
degree + 273.15
when ['C', 'F']
degree * 1.8 + 32
when ['K', 'C']
degree - 273.15
when ['K', 'F']
( degree - 273.15 ) * 1.8 + 32
when ['F', 'C']
( degree - 32 ) * Rational(5, 9)
when ['F', 'K']
( degree - 32 ) * Rational(5, 9) + 273.15
when [first_unit, first_unit]
degree
end
end
TABLE_OF_TEMPERATURES = {
'water' => [0, 100],
'ethanol' => [-114, 78.37],
'gold' => [1_064, 2_700],
'silver' => [961.8, 2_162],
'copper' => [1_085, 2_567]
}
# Melting Temperature
def melting_point_of_substance(substance_name, unit)
result = TABLE_OF_TEMPERATURES[substance_name].at(0)
convert_between_temperature_units(result, 'C', unit)
end
# Boiling Temperature
def boiling_point_of_substance(substance_name, unit)
result = TABLE_OF_TEMPERATURES[substance_name].at(1)
convert_between_temperature_units(result, 'C', unit)
end

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

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

Finished in 0.00821 seconds
17 examples, 0 failures

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

Дарин обнови решението на 16.10.2016 06:22 (преди почти 5 години)

+
+# Temperature Converter
+
+def convert_between_temperature_units(degree, first_unit, second_unit)
+ if first_unit == 'C' && second_unit == 'K'

Много е къдраво, не трудно е например да намериш къде се превръща от F към C.

  1. Няма нужда да присвояваш на degree
  2. Този Rational е излишен (1.8 :?)
  3. Винаги правиш сравнение на едни и същи неща. Тоест по-добре ще стане с case или с hash + един gaurd за когато са равни.
+ degree += 273.15
+ elsif first_unit == 'C' && second_unit == 'F'
+ degree = degree * Rational(9, 5) + 32
+ elsif first_unit == 'K' && second_unit == 'C'
+ degree -= 273.15
+ elsif first_unit == 'K' && second_unit == 'F'
+ degree = ( degree - 273.15 ) * Rational(9, 5) + 32
+ elsif first_unit == 'F' && second_unit == 'C'
+ degree = ( degree - 32 ) * Rational(5, 9)
+ elsif first_unit == 'F' && second_unit == 'K'
+ degree = ( degree - 32 ) * Rational(5, 9) + 273.15
+ elsif first_unit == second_unit
+ degree
+ end
+end
+
+# Melting Temperature
+
+def melting_point_of_substance(substance_name, unit)
+ table = {
+ 'water' => 0,
+ 'ethanol' => -114,
+ 'gold' => 1_064,
+ 'silver' => 961.8,
+ 'copper' => 1_085
+ }
+ convert_between_temperature_units(table[substance_name], 'C', unit)
+end
+
+# Boiling Temperature
+
+def boiling_point_of_substance(substance_name, unit)
+ table = {
+ 'water' => 100,
+ 'ethanol' => 78.37,
+ 'gold' => 2_700,
+ 'silver' => 2_162,
+ 'copper' => 2_567
+ }
+ convert_between_temperature_units(table[substance_name], 'C', unit)
+end

Дарин обнови решението на 16.10.2016 22:50 (преди почти 5 години)

# Temperature Converter
def convert_between_temperature_units(degree, first_unit, second_unit)
- if first_unit == 'C' && second_unit == 'K'
- degree += 273.15
- elsif first_unit == 'C' && second_unit == 'F'
- degree = degree * Rational(9, 5) + 32
- elsif first_unit == 'K' && second_unit == 'C'
- degree -= 273.15
- elsif first_unit == 'K' && second_unit == 'F'
- degree = ( degree - 273.15 ) * Rational(9, 5) + 32
- elsif first_unit == 'F' && second_unit == 'C'
- degree = ( degree - 32 ) * Rational(5, 9)
- elsif first_unit == 'F' && second_unit == 'K'
- degree = ( degree - 32 ) * Rational(5, 9) + 273.15
- elsif first_unit == second_unit
+ case [first_unit, second_unit]
+ when ['C', 'K']
+ degree + 273.15
+ when ['C', 'F']
+ degree * 1.8 + 32
+ when ['K', 'C']
+ degree - 273.15
+ when ['K', 'F']
+ ( degree - 273.15 ) * 1.8 + 32
+ when ['F', 'C']
+ ( degree - 32 ) * Rational(5, 9)
+ when ['F', 'K']
+ ( degree - 32 ) * Rational(5, 9) + 273.15
+ when [first_unit, first_unit]
degree
end
end
+TABLE_OF_TEMPERATURES = {
+ 'water' => [0, 100],
+ 'ethanol' => [-114, 78.37],
+ 'gold' => [1_064, 2_700],
+ 'silver' => [961.8, 2_162],
+ 'copper' => [1_085, 2_567]
+}
+
# Melting Temperature
def melting_point_of_substance(substance_name, unit)
- table = {
- 'water' => 0,
- 'ethanol' => -114,
- 'gold' => 1_064,
- 'silver' => 961.8,
- 'copper' => 1_085
- }
- convert_between_temperature_units(table[substance_name], 'C', unit)
+ result = TABLE_OF_TEMPERATURES[substance_name].at(0)
+ convert_between_temperature_units(result, 'C', unit)
end
# Boiling Temperature
def boiling_point_of_substance(substance_name, unit)
- table = {
- 'water' => 100,
+ result = TABLE_OF_TEMPERATURES[substance_name].at(1)
- 'ethanol' => 78.37,
+ convert_between_temperature_units(result, 'C', unit)
- 'gold' => 2_700,
+end
- 'silver' => 2_162,
- 'copper' => 2_567
- }
- convert_between_temperature_units(table[substance_name], 'C', unit)
-end