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

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

Към профила на Антон Сотиров

Резултати

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

Код

def convert_between_temperature_units(degrees, from, to)
if from == 'C' && to == 'F'
(9.0 / 5 * degrees) + 32
elsif from == 'C' && to == 'K'
degrees + 273.15
elsif from == 'F' && to == 'C'
5.0 / 9 * (degrees - 32)
elsif from == 'F' && to == 'K'
5.0 / 9 * (degrees - 32) + 273.15
elsif from == 'K' && to == 'F'
9.0 / 5 * (degrees - 273.15) + 32
elsif from == 'K' && to == 'C'
degrees - 273.15
end
end
def convert_from_celsius_in_hash(hash, substance, unit)
if unit == 'C'
hash[substance]
else
convert_between_temperature_units(hash[substance], 'C', unit)
end
end
def melting_point_of_substance(substance, unit)
melting_point_in_c = {'water' => 0, 'ethanol' => -144, 'gold' => 1_064, 'silver' => 961.8, 'copper' => 1_085}
convert_from_celsius_in_hash(melting_point_in_c, substance, unit)
end
def boiling_point_of_substance(substance, unit)
boiling_point_in_c = {'water' => 100, 'ethanol' => 78.37, 'gold' => 2_700, 'silver' => 2_162, 'copper' => 2_567}
convert_from_celsius_in_hash(boiling_point_in_c, substance, unit)
end

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

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-qf3z7s/spec.rb:57:in `expect_conversion'
     # /tmp/d20161018-13513-qf3z7s/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)>'

  2) #melting_point_of_substance knows the melting point of ethanol
     Failure/Error: expect(melting_point_of_substance(substance, units)).to be_within(0.01).of(expected_degrees)
       expected -144 to be within 0.01 of -114
     # /tmp/d20161018-13513-qf3z7s/spec.rb:93:in `expect_melting_point_of'
     # /tmp/d20161018-13513-qf3z7s/spec.rb:69: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.00935 seconds
17 examples, 2 failures

Failed examples:

rspec /tmp/d20161018-13513-qf3z7s/spec.rb:2 # #convert_between_temperature_units can convert to the same unit
rspec /tmp/d20161018-13513-qf3z7s/spec.rb:68 # #melting_point_of_substance knows the melting point of ethanol

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

Антон обнови решението на 17.10.2016 00:57 (преди около 8 години)

+def convert_between_temperature_units(degrees, from, to)
+ if from == 'C' && to == 'F'
+ (9.0 / 5 * degrees) + 32
+ elsif from == 'C' && to == 'K'
+ degrees + 273.15
+ elsif from == 'F' && to == 'C'
+ 5.0 / 9 * (degrees - 32)
+ elsif from == 'F' && to == 'K'
+ 5.0 / 9 * (degrees - 32) + 273.15
+ elsif from == 'K' && to == 'F'
+ 9.0 / 5 * (degrees - 273.15) + 32
+ elsif from == 'K' && to == 'C'
+ degrees - 273.15
+ end
+end
+
+def convert_from_celsius_in_hash(hash, substance, unit)
+ if unit == 'C'
+ hash[substance]
+ else
+ convert_between_temperature_units(hash[substance], 'C', unit)
+ end
+end
+
+def melting_point_of_substance(substance, unit)
+ melting_point_in_c = {'water' => 0, 'ethanol' => -144, 'gold' => 1_064, 'silver' => 961.8, 'copper' => 1_085}
+ convert_from_celsius_in_hash(melting_point_in_c, substance, unit)
+end
+
+def boiling_point_of_substance(substance, unit)
+ boiling_point_in_c = {'water' => 100, 'ethanol' => 78.37, 'gold' => 2_700, 'silver' => 2_162, 'copper' => 2_567}
+ convert_from_celsius_in_hash(boiling_point_in_c, substance, unit)
+end