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

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

Към профила на Петко Митков

Резултати

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

Код

TEMPERATURE_CONVERSION_CHART = {
'C' => { 'C' => [1, 0], 'F' => [1.8, 32], 'K' => [1, 273.15] },
'F' => { 'C' => [0.55555, -17.77], 'F' => [1, 0], 'K' => [0.55555, 255.37] },
'K' => { 'C' => [1, -273.15], 'F' => [1.8, -459.67], 'K' => [1, 0] },
}

Доста интересно решение на проблема с умножението и събирането.
Не ми допада особено използването на масиви за целта, но пък така е по-компактна таблицата, така че съм склонен да се съглася.

MELTING_AND_BOILING_POINTS_IN_CELSIUS = {
'water' => { melting_point: 0, boiling_point: 100 },
'ethanol' => { melting_point: -114, boiling_point: 78.37 },
'gold' => { melting_point: 1064, boiling_point: 2700 },
'silver' => { melting_point: 961.8, boiling_point: 2162 },
'copper' => { melting_point: 1085, boiling_point: 2567 },
}
def convert_between_temperature_units(degrees, input_unit, output_unit)
slope, intercept = TEMPERATURE_CONVERSION_CHART[input_unit][output_unit]
(degrees * slope + intercept).round(2)
end
def melting_point_of_substance(substance, unit)
melting_pt = MELTING_AND_BOILING_POINTS_IN_CELSIUS[substance][:melting_point]
convert_between_temperature_units(melting_pt, 'C', unit)
end
def boiling_point_of_substance(substance, unit)
boiling_pt = MELTING_AND_BOILING_POINTS_IN_CELSIUS[substance][:boiling_point]
convert_between_temperature_units(boiling_pt, 'C', unit)
end

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

....F............

Failures:

  1) #convert_between_temperature_units can convert Fahrenheit to Celsius
     Failure/Error: expect(actual_to_value).to be_within(0.0001).of(expected_to_value)
       expected 1.01 to be within 0.0001 of 1
     # /tmp/d20161018-13513-t93fyf/spec.rb:57:in `expect_conversion'
     # /tmp/d20161018-13513-t93fyf/spec.rb:31:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

Finished in 0.00792 seconds
17 examples, 1 failure

Failed examples:

rspec /tmp/d20161018-13513-t93fyf/spec.rb:30 # #convert_between_temperature_units can convert Fahrenheit to Celsius

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

Петко обнови решението на 11.10.2016 10:26 (преди над 7 години)

+TEMPERATURE_CONVERSION_CHART = {
+ 'C' => { 'C' => [1, 0], 'F' => [1.8, 32], 'K' => [1, 273.15] },
+ 'F' => { 'C' => [0.55555, -17.77], 'F' => [1, 0], 'K' => [0.55555, 255.37] },
+ 'K' => { 'C' => [1, -273.15], 'F' => [1.8, -459.67], 'K' => [1, 0] },
+}
+
+MELTING_AND_BOILING_POINTS_IN_CELSIUS = {
+ 'water' => [0, 100],
+ 'ethanol' => [-114, 78.37],
+ 'gold' => [1064, 2700],
+ 'silver' => [961.8, 2162],
+ 'copper' => [1085, 2567],
+}
+
+def convert_between_temperature_units(degrees, input_unit, output_unit)
+ slope, intercept = TEMPERATURE_CONVERSION_CHART[input_unit][output_unit]
+ (degrees * slope + intercept).round(2)
+end
+
+def melting_point_of_substance(substance, unit)
+ melting_point_in_celsius = MELTING_AND_BOILING_POINTS_IN_CELSIUS[substance][0]
+ convert_between_temperature_units(melting_point_in_celsius, 'C', unit)
+end
+
+def boiling_point_of_substance(substance, unit)
+ boiling_point_in_celsius = MELTING_AND_BOILING_POINTS_IN_CELSIUS[substance][1]
+ convert_between_temperature_units(boiling_point_in_celsius, 'C', unit)
+end

Петко обнови решението на 11.10.2016 16:57 (преди над 7 години)

TEMPERATURE_CONVERSION_CHART = {
'C' => { 'C' => [1, 0], 'F' => [1.8, 32], 'K' => [1, 273.15] },
'F' => { 'C' => [0.55555, -17.77], 'F' => [1, 0], 'K' => [0.55555, 255.37] },
'K' => { 'C' => [1, -273.15], 'F' => [1.8, -459.67], 'K' => [1, 0] },
}

Доста интересно решение на проблема с умножението и събирането.
Не ми допада особено използването на масиви за целта, но пък така е по-компактна таблицата, така че съм склонен да се съглася.

MELTING_AND_BOILING_POINTS_IN_CELSIUS = {
'water' => [0, 100],
'ethanol' => [-114, 78.37],
- 'gold' => [1064, 2700],
+ 'gold' => [1064, 2700],
'silver' => [961.8, 2162],
'copper' => [1085, 2567],

Тук вече нямаш извинение да използваш масиви :) Можеш просто да сложиш хешове вместо масивите - [0, 100] => {melting_point: 0, boiling_point: 100} или да направиш две таблици.

Това, че е хеш ти позволява долу да кажеш [:melting_point] вместо [0] или [1]

}
def convert_between_temperature_units(degrees, input_unit, output_unit)
slope, intercept = TEMPERATURE_CONVERSION_CHART[input_unit][output_unit]
(degrees * slope + intercept).round(2)
end
def melting_point_of_substance(substance, unit)
melting_point_in_celsius = MELTING_AND_BOILING_POINTS_IN_CELSIUS[substance][0]
convert_between_temperature_units(melting_point_in_celsius, 'C', unit)
end
def boiling_point_of_substance(substance, unit)
boiling_point_in_celsius = MELTING_AND_BOILING_POINTS_IN_CELSIUS[substance][1]
convert_between_temperature_units(boiling_point_in_celsius, 'C', unit)
end

Петко обнови решението на 14.10.2016 15:58 (преди над 7 години)

TEMPERATURE_CONVERSION_CHART = {
'C' => { 'C' => [1, 0], 'F' => [1.8, 32], 'K' => [1, 273.15] },
'F' => { 'C' => [0.55555, -17.77], 'F' => [1, 0], 'K' => [0.55555, 255.37] },
'K' => { 'C' => [1, -273.15], 'F' => [1.8, -459.67], 'K' => [1, 0] },
}
MELTING_AND_BOILING_POINTS_IN_CELSIUS = {
- 'water' => [0, 100],
- 'ethanol' => [-114, 78.37],
- 'gold' => [1064, 2700],
- 'silver' => [961.8, 2162],
- 'copper' => [1085, 2567],
+ 'water' => {melting_point: 0, boiling_point: 100 },
+ 'ethanol' => {melting_point: -114, boiling_point: 78.37 },
+ 'gold' => {melting_point: 1064, boiling_point: 2700 },
+ 'silver' => {melting_point: 961.8, boiling_point: 2162 },
+ 'copper' => {melting_point: 1085, boiling_point: 2567 },
}
def convert_between_temperature_units(degrees, input_unit, output_unit)
slope, intercept = TEMPERATURE_CONVERSION_CHART[input_unit][output_unit]
(degrees * slope + intercept).round(2)
end
def melting_point_of_substance(substance, unit)
- melting_point_in_celsius = MELTING_AND_BOILING_POINTS_IN_CELSIUS[substance][0]
- convert_between_temperature_units(melting_point_in_celsius, 'C', unit)
+ melting_pt = MELTING_AND_BOILING_POINTS_IN_CELSIUS[substance][:melting_point]
+ convert_between_temperature_units(melting_pt, 'C', unit)
end
def boiling_point_of_substance(substance, unit)
- boiling_point_in_celsius = MELTING_AND_BOILING_POINTS_IN_CELSIUS[substance][1]
- convert_between_temperature_units(boiling_point_in_celsius, 'C', unit)
+ boiling_pt = MELTING_AND_BOILING_POINTS_IN_CELSIUS[substance][:boiling_point]
+ convert_between_temperature_units(boiling_pt, 'C', unit)
end

Петко обнови решението на 14.10.2016 15:59 (преди над 7 години)

TEMPERATURE_CONVERSION_CHART = {
'C' => { 'C' => [1, 0], 'F' => [1.8, 32], 'K' => [1, 273.15] },
'F' => { 'C' => [0.55555, -17.77], 'F' => [1, 0], 'K' => [0.55555, 255.37] },
'K' => { 'C' => [1, -273.15], 'F' => [1.8, -459.67], 'K' => [1, 0] },
}
MELTING_AND_BOILING_POINTS_IN_CELSIUS = {
- 'water' => {melting_point: 0, boiling_point: 100 },
- 'ethanol' => {melting_point: -114, boiling_point: 78.37 },
- 'gold' => {melting_point: 1064, boiling_point: 2700 },
- 'silver' => {melting_point: 961.8, boiling_point: 2162 },
- 'copper' => {melting_point: 1085, boiling_point: 2567 },
+ 'water' => { melting_point: 0, boiling_point: 100 },
+ 'ethanol' => { melting_point: -114, boiling_point: 78.37 },
+ 'gold' => { melting_point: 1064, boiling_point: 2700 },
+ 'silver' => { melting_point: 961.8, boiling_point: 2162 },
+ 'copper' => { melting_point: 1085, boiling_point: 2567 },
}
def convert_between_temperature_units(degrees, input_unit, output_unit)
slope, intercept = TEMPERATURE_CONVERSION_CHART[input_unit][output_unit]
(degrees * slope + intercept).round(2)
end
def melting_point_of_substance(substance, unit)
melting_pt = MELTING_AND_BOILING_POINTS_IN_CELSIUS[substance][:melting_point]
convert_between_temperature_units(melting_pt, 'C', unit)
end
def boiling_point_of_substance(substance, unit)
boiling_pt = MELTING_AND_BOILING_POINTS_IN_CELSIUS[substance][:boiling_point]
convert_between_temperature_units(boiling_pt, 'C', unit)
end