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

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

Към профила на Иван Иванов

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 15 успешни тест(а)
  • 2 неуспешни тест(а)

Код

MY_HASH =
{
'water' => [0, 100], 'ethanol' => [-114, 78.37],
'gold' => [1064, 2700], 'silver' => [961.8, 2162],
'copper' => [1085, 2567]
}.freeze
def convert_between_temperature_units(temp, from, to)
if temp.is_a?Numeric
if 'C' == from
convert_from_celsius(temp, to)
elsif 'F' == from
convert_from_farenheit(temp, to)
elsif 'K' == from
convert_from_kelvin(temp, to)
end
else puts 'Wrong parameters!'
end
end
def convert_from_celsius(temp, to)
if 'F' == to
temp * 1.8 + 32
elsif 'K' == to
temp + 273.15
elsif 'C' == to
temp
else
puts 'Wrong parameters!'
end
end
def convert_from_kelvin(temp, to)
if 'C' == to
temp - 273.15
elsif 'F' == to
(temp * 1.8) - 459.67
else
puts 'Wrong parameters!'
end
end
def convert_from_farenheit(temp, to)
if 'C' == to
(temp - 32) / 1.8
elsif 'K' == to
(temp + 459.67) * 0.5555
else
puts 'Wrong parameters!'
end
end
def melting_point_of_substance(substance, metric)
if !MY_HASH[substance].nil?
convert_between_temperature_units(MY_HASH[substance][0], 'C', metric)
else puts 'Wrong parameters!'
end
end
def boiling_point_of_substance(substance, metric)
if !MY_HASH[substance].nil?
convert_between_temperature_units(MY_HASH[substance][1], 'C', metric)
else puts 'Wrong parameters!'
end
end
puts convert_between_temperature_units(5, 'C', 'K')
puts melting_point_of_substance('gold', 'K')
puts boiling_point_of_substance('gold', 'K')

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

278.15
1337.15
2973.15
Wrong parameters!
F.....F..........

Failures:

  1) #convert_between_temperature_units can convert to the same unit
     Failure/Error: expect(actual_to_value).to be_within(0.0001).of(expected_to_value)
     ArgumentError:
       The actual value (nil) must respond to `-`
     # /tmp/d20161018-13513-pirthr/spec.rb:57:in `expect_conversion'
     # /tmp/d20161018-13513-pirthr/spec.rb:4: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)>'

  2) #convert_between_temperature_units can convert Fahrenheit to Kelvin
     Failure/Error: expect(actual_to_value).to be_within(0.0001).of(expected_to_value)
       expected 274.122585 to be within 0.0001 of 274.15
     # /tmp/d20161018-13513-pirthr/spec.rb:57:in `expect_conversion'
     # /tmp/d20161018-13513-pirthr/spec.rb:47: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.00786 seconds
17 examples, 2 failures

Failed examples:

rspec /tmp/d20161018-13513-pirthr/spec.rb:2 # #convert_between_temperature_units can convert to the same unit
rspec /tmp/d20161018-13513-pirthr/spec.rb:46 # #convert_between_temperature_units can convert Fahrenheit to Kelvin

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

Иван обнови решението на 17.10.2016 15:10 (преди над 7 години)

+MY_HASH =
+ {
+ 'water' => [0, 100], 'ethanol' => [-114, 78.37],
+ 'gold' => [1064, 2700], 'silver' => [961.8, 2162],
+ 'copper' => [1085, 2567]
+ }.freeze
+
+def convert_between_temperature_units(temp, from, to)
+ if temp.is_a?Numeric
+ if 'C' == from
+ convert_from_celsius(temp, to)
+ elsif 'F' == from
+ convert_from_farenheit(temp, to)
+ elsif 'K' == from
+ convert_from_kelvin(temp, to)
+ end
+ else puts 'Wrong parameters!'
+ end
+end
+
+def convert_from_celsius(temp, to)
+ if 'F' == to
+ temp * 1.8 + 32
+ elsif 'K' == to
+ temp + 273.15
+ elsif 'C' == to
+ temp
+ else
+ puts 'Wrong parameters!'
+ end
+end
+
+def convert_from_kelvin(temp, to)
+ if 'C' == to
+ temp - 273.15
+ elsif 'F' == to
+ (temp * 1.8) - 459.67
+ else
+ puts 'Wrong parameters!'
+ end
+end
+
+def convert_from_farenheit(temp, to)
+ if 'C' == to
+ (temp - 32) / 1.8
+ elsif 'K' == to
+ (temp + 459.67) * 0.5555
+ else
+ puts 'Wrong parameters!'
+ end
+end
+
+def melting_point_of_substance(substance, metric)
+ if !MY_HASH[substance].nil?
+ convert_between_temperature_units(MY_HASH[substance][0], 'C', metric)
+ else puts 'Wrong parameters!'
+ end
+end
+
+def boiling_point_of_substance(substance, metric)
+ if !MY_HASH[substance].nil?
+ convert_between_temperature_units(MY_HASH[substance][1], 'C', metric)
+ else puts 'Wrong parameters!'
+ end
+end
+
+puts convert_between_temperature_units(5, 'C', 'K')
+puts melting_point_of_substance('gold', 'K')
+puts boiling_point_of_substance('gold', 'K')