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

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

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

Резултати

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

Код

def convert_between_temperature_units(degrees, old_unit, new_unit)
return degrees if new_unit == old_unit
degrees_in_celsius = old_unit_to_celsius(degrees, old_unit)
celsius_to_new_unit(degrees_in_celsius, new_unit)
end
def old_unit_to_celsius(degrees, old_unit)
case old_unit
when 'C'
degrees
when 'K'
degrees - 273.15
else
(degrees - 32) * 5 / 9
end
end
def celsius_to_new_unit(degrees, new_unit)
case new_unit
when 'C'
degrees
when 'K'
degrees + 273.15
else
degrees * 9 / 5 + 32
end
end
def melting_point_of_substance(substance, unit)
substances = {'water' => 0, 'ethanol' => -114, 'gold' => 1_064, 'silver' => 961.8, 'copper' => 1_085}
celsius_to_new_unit(substances[substance], unit)
end
def boiling_point_of_substance(substance, unit)
substances = {'water' => 100, 'ethanol' => 78.37, 'gold' => 2_700, 'silver' => 2_162, 'copper' => 2_567}
celsius_to_new_unit(substances[substance], unit)
end

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

........FF.....FF

Failures:

  1) #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 -174 to be within 0.01 of -173.2
     # /tmp/d20161018-13513-1ilbdec/spec.rb:93:in `expect_melting_point_of'
     # /tmp/d20161018-13513-1ilbdec/spec.rb:71: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 gold
     Failure/Error: expect(melting_point_of_substance(substance, units)).to be_within(0.01).of(expected_degrees)
       expected 1947 to be within 0.01 of 1947.2
     # /tmp/d20161018-13513-1ilbdec/spec.rb:93:in `expect_melting_point_of'
     # /tmp/d20161018-13513-1ilbdec/spec.rb:77: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)>'

  3) #boiling_point_of_substance knows the boiling point of silver
     Failure/Error: expect(boiling_point_of_substance(substance, units)).to be_within(0.01).of(expected_degrees)
       expected 3923 to be within 0.01 of 3923.6
     # /tmp/d20161018-13513-1ilbdec/spec.rb:129:in `expect_boiling_point_of'
     # /tmp/d20161018-13513-1ilbdec/spec.rb:119: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)>'

  4) #boiling_point_of_substance knows the boiling point of copper
     Failure/Error: expect(boiling_point_of_substance(substance, units)).to be_within(0.01).of(expected_degrees)
       expected 4652 to be within 0.01 of 4652.6
     # /tmp/d20161018-13513-1ilbdec/spec.rb:129:in `expect_boiling_point_of'
     # /tmp/d20161018-13513-1ilbdec/spec.rb:125: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.0101 seconds
17 examples, 4 failures

Failed examples:

rspec /tmp/d20161018-13513-1ilbdec/spec.rb:68 # #melting_point_of_substance knows the melting point of ethanol
rspec /tmp/d20161018-13513-1ilbdec/spec.rb:74 # #melting_point_of_substance knows the melting point of gold
rspec /tmp/d20161018-13513-1ilbdec/spec.rb:116 # #boiling_point_of_substance knows the boiling point of silver
rspec /tmp/d20161018-13513-1ilbdec/spec.rb:122 # #boiling_point_of_substance knows the boiling point of copper

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

Георги обнови решението на 17.10.2016 14:14 (преди над 7 години)

+def convert_between_temperature_units(degrees, old_unit, new_unit)
+ if new_unit == old_unit
+ degrees
+ degrees_in_celsius = old_unit_to_celsius(degrees, old_unit)
+ celsius_to_new_unit(degrees_in_celsius, new_unit)
+ end
+end
+
+def old_unit_to_celsius(degrees, old_unit)
+ case old_unit
+ when 'C'
+ degrees
+ when 'K'
+ degrees - 273.15
+ else
+ (degrees - 32) * 5 / 9
+ end
+end
+
+def celsius_to_new_unit(degrees, new_unit)
+ case new_unit
+ when 'C'
+ degrees
+ when 'K'
+ degrees + 273.15
+ else
+ degrees * 9 / 5 + 32
+ end
+end
+
+def melting_point_of_substance(substance, unit)
+ substances = {water: 0, ethanol: -114, gold: 1_064, silver: 961.8, copper: 1_085}
+ celsius_to_new_unit(substances[substance], unit)
+end
+
+def boiling_point_of_substance(substance, unit)
+ substances = {water: 100, ethanol: 78.37, gold: 2_700, silver: 2_162, copper: 2_567}
+ celsius_to_new_unit(substances[substance], unit)
+end

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

def convert_between_temperature_units(degrees, old_unit, new_unit)
- if new_unit == old_unit
- degrees
- degrees_in_celsius = old_unit_to_celsius(degrees, old_unit)
- celsius_to_new_unit(degrees_in_celsius, new_unit)
- end
+ return degrees if new_unit == old_unit
+ degrees_in_celsius = old_unit_to_celsius(degrees, old_unit)
+ celsius_to_new_unit(degrees_in_celsius, new_unit)
end
def old_unit_to_celsius(degrees, old_unit)
case old_unit
when 'C'
degrees
when 'K'
degrees - 273.15
else
(degrees - 32) * 5 / 9
end
end
def celsius_to_new_unit(degrees, new_unit)
case new_unit
when 'C'
degrees
when 'K'
degrees + 273.15
else
degrees * 9 / 5 + 32
end
end
def melting_point_of_substance(substance, unit)
- substances = {water: 0, ethanol: -114, gold: 1_064, silver: 961.8, copper: 1_085}
+ substances = {'water' => 0, 'ethanol' => -114, 'gold' => 1_064, 'silver' => 961.8, 'copper' => 1_085}
celsius_to_new_unit(substances[substance], unit)
end
def boiling_point_of_substance(substance, unit)
- substances = {water: 100, ethanol: 78.37, gold: 2_700, silver: 2_162, copper: 2_567}
+ substances = {'water' => 100, 'ethanol' => 78.37, 'gold' => 2_700, 'silver' => 2_162, 'copper' => 2_567}
celsius_to_new_unit(substances[substance], unit)
end