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

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

Към профила на Мариян Асенов

Резултати

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

Код

def convert_between_temperature_units(degrees, from_unit, to_unit)
formula_key = from_unit + to_unit
k_to_c = degrees - 273.15
c_to_k = degrees + 273.15
c_to_f = degrees * (9 / 5.to_f) + 32
f_to_c = (degrees - 32) * (5 / 9.to_f)
k_to_f = degrees * (9 / 5.to_f) - 459.67
f_to_k = (degrees + 459.67) * (5 / 9.to_f)
formulas = {
'KC' => k_to_c, 'CK' => c_to_k,
'CF' => c_to_f, 'FC' => f_to_c,
'KF' => k_to_f, 'FK' => f_to_k,
'KK' => degrees, 'CC' => degrees,
'FF' => degrees

Табличката е доста добро решение с този подход :) Обаче, има по-добър подход, в който няма да ти се наложи да генерираш всички комбинации (с наредба). Можеш ли да се сетиш как да направиш преобразуванията на две стъпки, така че програмата сама да се сети за някои от формулите?

}
formulas[formula_key]
end
MELTING_POINT_IN_CELSIUS = {
'water' => 0,
'ethanol' => - 114,
'gold' => 1_064,
'silver' => 961.8,
'copper' => 1_085
}
def melting_point_of_substance(substance, unit)
convert_between_temperature_units(MELTING_POINT_IN_CELSIUS[substance], 'C', unit)
end
BOILING_POINT_IN_CELSIUS = {
'water' => 100,
'ethanol' => 78.37,
'gold' => 2_700,
'silver' => 2_162,
'copper' => 2_567
}
def boiling_point_of_substance(substance, unit)
convert_between_temperature_units(BOILING_POINT_IN_CELSIUS[substance], 'C', unit)
end

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

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

Finished in 0.00833 seconds
17 examples, 0 failures

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

Мариян обнови решението на 13.10.2016 13:01 (преди над 7 години)

+def convert_between_temperature_units(degrees, from_unit, to_unit)
+ formula_key = from_unit.dup
+ formula_key << to_unit
+ k_to_c = degrees - 273.15
+ c_to_k = degrees + 273.15
+ c_to_f = degrees * Rational(9, 5).to_f + 32
+ f_to_c = (degrees - 32) * Rational(5, 9).to_f
+ k_to_f = degrees * Rational(9, 5).to_f - 459.67
+ f_to_k = (degrees + 459.67) * Rational(5, 9).to_f
+ formulas = {
+ 'KC' => k_to_c, 'CK' => c_to_k,
+ 'CF' => c_to_f, 'FC' => f_to_c,
+ 'KF' => k_to_f, 'FK' => f_to_k
+ }
+ formulas[formula_key]
+end
+
+def check(degrees, unit)
+ if unit == 'C'
+ degrees
+ else
+ convert_between_temperature_units(degrees, 'C', unit)
+ end
+end
+
+def melting_point_of_substance(substance, unit)
+ hash = {
+ 'water' => 0,
+ 'ethanol' => -114,
+ 'gold' => 1_064,
+ 'siver' => 961.8,
+ 'copper' => 1_085
+ }
+ check(hash[substance], unit)
+end
+
+def boiling_point_of_substance(substance, unit)
+ hash = {
+ 'water' => 100,
+ 'ethanol' => 78.37,
+ 'gold' => 2_700,
+ 'siver' => 2_162,
+ 'copper' => 2_567
+ }
+ check(hash[substance], unit)
+end

Мариян обнови решението на 13.10.2016 13:14 (преди над 7 години)

def convert_between_temperature_units(degrees, from_unit, to_unit)
formula_key = from_unit.dup
formula_key << to_unit
k_to_c = degrees - 273.15
c_to_k = degrees + 273.15
c_to_f = degrees * Rational(9, 5).to_f + 32
f_to_c = (degrees - 32) * Rational(5, 9).to_f
k_to_f = degrees * Rational(9, 5).to_f - 459.67
f_to_k = (degrees + 459.67) * Rational(5, 9).to_f
formulas = {
'KC' => k_to_c, 'CK' => c_to_k,
'CF' => c_to_f, 'FC' => f_to_c,
'KF' => k_to_f, 'FK' => f_to_k
}
formulas[formula_key]
end
-def check(degrees, unit)
+def convert_from_celsius(degrees, unit)
if unit == 'C'
degrees
else
convert_between_temperature_units(degrees, 'C', unit)
end
end
def melting_point_of_substance(substance, unit)
- hash = {
+ melting_point_in_celsius = {
'water' => 0,
'ethanol' => -114,
'gold' => 1_064,
'siver' => 961.8,
'copper' => 1_085
}
- check(hash[substance], unit)
+ convert_from_celsius(melting_point_in_celsius[substance], unit)
end
def boiling_point_of_substance(substance, unit)
- hash = {
+ boiling_point_in_celsius = {
'water' => 100,
'ethanol' => 78.37,
'gold' => 2_700,
'siver' => 2_162,
'copper' => 2_567
}
- check(hash[substance], unit)
+ convert_from_celsius(boiling_point_in_celsius[substance], unit)
end

Мариян обнови решението на 13.10.2016 23:18 (преди над 7 години)

def convert_between_temperature_units(degrees, from_unit, to_unit)
formula_key = from_unit.dup
formula_key << to_unit
k_to_c = degrees - 273.15
c_to_k = degrees + 273.15
c_to_f = degrees * Rational(9, 5).to_f + 32
f_to_c = (degrees - 32) * Rational(5, 9).to_f
k_to_f = degrees * Rational(9, 5).to_f - 459.67
f_to_k = (degrees + 459.67) * Rational(5, 9).to_f
formulas = {
'KC' => k_to_c, 'CK' => c_to_k,
'CF' => c_to_f, 'FC' => f_to_c,
'KF' => k_to_f, 'FK' => f_to_k
}
formulas[formula_key]
end
def convert_from_celsius(degrees, unit)
if unit == 'C'
degrees
else
convert_between_temperature_units(degrees, 'C', unit)
end
end
def melting_point_of_substance(substance, unit)
melting_point_in_celsius = {
'water' => 0,
'ethanol' => -114,
'gold' => 1_064,
- 'siver' => 961.8,
+ 'silver' => 961.8,
'copper' => 1_085
}
convert_from_celsius(melting_point_in_celsius[substance], unit)
end
def boiling_point_of_substance(substance, unit)
boiling_point_in_celsius = {
'water' => 100,
'ethanol' => 78.37,
'gold' => 2_700,
- 'siver' => 2_162,
+ 'silver' => 2_162,
'copper' => 2_567
}
convert_from_celsius(boiling_point_in_celsius[substance], unit)
end

Мариян обнови решението на 14.10.2016 12:06 (преди над 7 години)

def convert_between_temperature_units(degrees, from_unit, to_unit)
formula_key = from_unit.dup
formula_key << to_unit
k_to_c = degrees - 273.15
c_to_k = degrees + 273.15
c_to_f = degrees * Rational(9, 5).to_f + 32

Рационалното число 9/5, превърнато в число с плаваща запетая, е точно 1.8. Разликата е че 1.8 се разбира от пръв поглед от някого, който чете кода, докато Rational(9, 5).to_f изисква момент замисляне. :)

Така е, но в 99.983% от случаите не ни трябва толкова точност. Например, не ни интересува, че сме с 0.00000000001 по-малко от реалната стойност. Тези градуси на практика са неизмерими. Пък и не правим сложни сметки, така че грешката да може да се натрупа.

Правилно, казахме че е най-точният начин за делене. Може би не сме обърнали достатъчно внимание, обаче, че на практика точността на Float ни е предостатъчна за всичко, освен за смятане на парични транзакции. :) Нека името не те заблуждава, Float числата са по-скоро еквивалент на Double в C++. Обикновено ако тази прецизност не ни е достатъчна, минаваме по-скоро към BigDecimal.

f_to_c = (degrees - 32) * Rational(5, 9).to_f
k_to_f = degrees * Rational(9, 5).to_f - 459.67
f_to_k = (degrees + 459.67) * Rational(5, 9).to_f
formulas = {
'KC' => k_to_c, 'CK' => c_to_k,
'CF' => c_to_f, 'FC' => f_to_c,
- 'KF' => k_to_f, 'FK' => f_to_k
+ 'KF' => k_to_f, 'FK' => f_to_k,
+ 'KK' => degrees, 'CC' => degrees,
+ 'FF' => degrees

Табличката е доста добро решение с този подход :) Обаче, има по-добър подход, в който няма да ти се наложи да генерираш всички комбинации (с наредба). Можеш ли да се сетиш как да направиш преобразуванията на две стъпки, така че програмата сама да се сети за някои от формулите?

}
formulas[formula_key]
end
-def convert_from_celsius(degrees, unit)
- if unit == 'C'
- degrees
- else
- convert_between_temperature_units(degrees, 'C', unit)
- end
-end
-
def melting_point_of_substance(substance, unit)
melting_point_in_celsius = {
'water' => 0,
'ethanol' => -114,
'gold' => 1_064,
'silver' => 961.8,
'copper' => 1_085
}
- convert_from_celsius(melting_point_in_celsius[substance], unit)
+ convert_between_temperature_units(melting_point_in_celsius[substance], 'C', unit)
end
def boiling_point_of_substance(substance, unit)
boiling_point_in_celsius = {
'water' => 100,
'ethanol' => 78.37,
'gold' => 2_700,
'silver' => 2_162,
'copper' => 2_567
}
- convert_from_celsius(boiling_point_in_celsius[substance], unit)
+ convert_between_temperature_units(boiling_point_in_celsius[substance], 'C', unit)
end

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

def convert_between_temperature_units(degrees, from_unit, to_unit)
- formula_key = from_unit.dup
- formula_key << to_unit
+ formula_key = from_unit + to_unit
k_to_c = degrees - 273.15
c_to_k = degrees + 273.15
- c_to_f = degrees * Rational(9, 5).to_f + 32
- f_to_c = (degrees - 32) * Rational(5, 9).to_f
- k_to_f = degrees * Rational(9, 5).to_f - 459.67
- f_to_k = (degrees + 459.67) * Rational(5, 9).to_f
+ c_to_f = degrees * (9 / 5.to_f) + 32
+ f_to_c = (degrees - 32) * (5 / 9.to_f)
+ k_to_f = degrees * (9 / 5.to_f) - 459.67
+ f_to_k = (degrees + 459.67) * (5 / 9.to_f)
formulas = {
'KC' => k_to_c, 'CK' => c_to_k,
'CF' => c_to_f, 'FC' => f_to_c,
'KF' => k_to_f, 'FK' => f_to_k,
'KK' => degrees, 'CC' => degrees,
'FF' => degrees
}
formulas[formula_key]
end
+MELTING_POINT_IN_CELSIUS = {
+ 'water' => 0,
+ 'ethanol' => - 114,
+ 'gold' => 1_064,
+ 'silver' => 961.8,
+ 'copper' => 1_085
+}
def melting_point_of_substance(substance, unit)
- melting_point_in_celsius = {
- 'water' => 0,
- 'ethanol' => -114,
- 'gold' => 1_064,
- 'silver' => 961.8,
- 'copper' => 1_085
- }
- convert_between_temperature_units(melting_point_in_celsius[substance], 'C', unit)
+ convert_between_temperature_units(MELTING_POINT_IN_CELSIUS[substance], 'C', unit)
end
+BOILING_POINT_IN_CELSIUS = {
+ 'water' => 100,
+ 'ethanol' => 78.37,
+ 'gold' => 2_700,
+ 'silver' => 2_162,
+ 'copper' => 2_567
+}
def boiling_point_of_substance(substance, unit)
- boiling_point_in_celsius = {
- 'water' => 100,
- 'ethanol' => 78.37,
- 'gold' => 2_700,
- 'silver' => 2_162,
- 'copper' => 2_567
- }
- convert_between_temperature_units(boiling_point_in_celsius[substance], 'C', unit)
+ convert_between_temperature_units(BOILING_POINT_IN_CELSIUS[substance], 'C', unit)
end