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

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

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

Резултати

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

Код

def convert_between_temperature_units(degrees, unit_from, unit_to)
if unit_from == "C"
if unit_to == "K"
degrees + 273.15
elsif unit_to == "F"
degrees * 1.8 + 32
end
elsif unit_from == "K"
if unit_to == "C"
degrees - 273.15
elsif unit_to == "F"
degrees * 1.8 - 459.67
end
elsif unit_from == "F"
if unit_to == "C"
(degrees - 32) / 1.8
elsif unit_to == "K"
(degrees + 459.67) * 5 / 9
end
end
end
def melting_point_of_substance(substance, unit)
melting_point = {
"water" => 0, "ethanol" => -114, "gold" => 1064,
"silver" => 961.8, "copper" => 1085
}
if unit == "C"
melting_point[substance]
else
convert_between_temperature_units(melting_point[substance], "C", unit)
end
end
def boiling_point_of_substance(substance, unit)
boiling_point = {
"water" => 100, "ethanol" => 78.37, "gold" => 2700,
"silver" => 2162, "copper" => 2567
}
if unit == "C"
boiling_point[substance]
else
convert_between_temperature_units(boiling_point[substance], "C", unit)
end
end

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

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-vugxgg/spec.rb:57:in `expect_conversion'
     # /tmp/d20161018-13513-vugxgg/spec.rb:3: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.01297 seconds
17 examples, 1 failure

Failed examples:

rspec /tmp/d20161018-13513-vugxgg/spec.rb:2 # #convert_between_temperature_units can convert to the same unit

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

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

+def convert_between_temperature_units(degrees, unit_from, unit_to)
+ if unit_from == "C"
+ if unit_to == "K"
+ degrees + 273.15
+ elsif unit_to == "F"
+ degrees * 1.8 + 32
+ end
+
+ elsif unit_from == "K"
+ if unit_to == "C"
+ degrees - 273.15
+ elsif unit_to == "F"
+ degrees * 1.8 - 459.67
+ end
+
+ elsif unit_from == "F"
+ if unit_to == "C"
+ (degrees - 32) / 1.8
+ elsif unit_to == "K"
+ (degrees + 459.67) * 5 / 9
+ end
+ end
+end
+
+def melting_point_of_substance(substance, unit)
+ melting_point = {
+ "water" => 0, "ethanol" => -114, "gold" => 1064,
+ "silver" => 961.8, "copper" => 1085
+ }
+
+ if unit == "C"
+ melting_point[substance]
+
+ else
+ convert_between_temperature_units(melting_point[substance], "C", unit)
+ end
+end
+
+def boiling_point_of_substance(substance, unit)
+ boiling_point = {
+ "water" => 100, "ethanol" => 78.37, "gold" => 2700,
+ "silver" => 2162, "copper" => 2567
+ }
+
+ if unit == "C"
+ boiling_point[substance]
+
+ else
+ convert_between_temperature_units(boiling_point[substance], "C", unit)
+ end
+end