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

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

Към профила на Лазар Дилов

Резултати

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

Код

def convert_between_temperature_units(x, t1, t2)
if t1 == t2
x
elsif t1 == 'C' && t2 == 'K'
x + 273.15
elsif t1 == 'C' && t2 == 'F'
x * 1.8 + 32
elsif t1 == 'F' && t2 == 'C'
(x - 32) / 1.8
elsif t1 == 'F' && t2 == 'K'
(x + 459.67) * (5 / 9)
elsif t1 == 'K' && t2 == 'C'
x - 273.15
elsif t1 == 'K' && t2 == 'F'
x * (9 / 5) - 459.67
else
-1
# Просто връща нещо
end
end
SUBSTANCE_MELTING_POINT = {
'water' => 0,
'ethanol' => -114,
'gold' => 1_064,
'silver' => 961.8,
'copper' => 1_085
}
def melting_point_of_substance(substance, unit)
convert_between_temperature_units(SUBSTANCE_MELTING_POINT[substance], 'C', unit)
end
SUBSTANCE_BOILING_POINT = {
'water' => 100,
'ethanol' => 78.38,
'gold' => 2_700,
'silver' => 2_162,
'copper' => 2_567
}
def boiling_point_of_substance(substance, unit)
convert_between_temperature_units(SUBSTANCE_BOILING_POINT[substance], 'C', unit)
end

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

.....FF......F...

Failures:

  1) #convert_between_temperature_units can convert Kelvin to Fahrenheit
     Failure/Error: expect(actual_to_value).to be_within(0.0001).of(expected_to_value)
       expected -185.52000000000004 to be within 0.0001 of 33.8
     # /tmp/d20161018-13513-19jbwsa/spec.rb:57:in `expect_conversion'
     # /tmp/d20161018-13513-19jbwsa/spec.rb:39: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 0.0 to be within 0.0001 of 274.15
     # /tmp/d20161018-13513-19jbwsa/spec.rb:57:in `expect_conversion'
     # /tmp/d20161018-13513-19jbwsa/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)>'

  3) #boiling_point_of_substance knows the boiling point of ethanol
     Failure/Error: expect(boiling_point_of_substance(substance, units)).to be_within(0.01).of(expected_degrees)
       expected 173.084 to be within 0.01 of 173.066
     # /tmp/d20161018-13513-19jbwsa/spec.rb:129:in `expect_boiling_point_of'
     # /tmp/d20161018-13513-19jbwsa/spec.rb:107: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.00952 seconds
17 examples, 3 failures

Failed examples:

rspec /tmp/d20161018-13513-19jbwsa/spec.rb:38 # #convert_between_temperature_units can convert Kelvin to Fahrenheit
rspec /tmp/d20161018-13513-19jbwsa/spec.rb:46 # #convert_between_temperature_units can convert Fahrenheit to Kelvin
rspec /tmp/d20161018-13513-19jbwsa/spec.rb:104 # #boiling_point_of_substance knows the boiling point of ethanol

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

Лазар обнови решението на 11.10.2016 00:11 (преди около 8 години)

+def convert_between_temperature_units(x, t1, t2)
+ case t1
+ when t2
+ x
+ when 'C'
+ if t2 == 'F'
+ x * 1.8 + 32
+ else
+ x + 273.15
+ end
+ when 'F'
+ if t2 == 'K'
+ (x + 459.67) * (5 / 9)
+ else
+ (x - 32) / 1.8
+ end
+ when 'K'
+ if t2 == 'C'
+ x - 273.15
+ else
+ x * (9 / 5) - 459.67
+ end
+ end
+end
+
+def melting_point_of_substance(substance, unit)
+ case substance
+ when 'water'
+ convert_between_temperature_units(0, 'C', unit)
+ when 'ethanol'
+ convert_between_temperature_units(-114, 'C', unit)
+ when 'gold'
+ convert_between_temperature_units(1_064, 'C', unit)
+ when 'silver'
+ convert_between_temperature_units(961.8, 'C', unit)
+ when 'copper'
+ convert_between_temperature_units(1_085, 'C', unit)

Тук има две неща, които може да подобриш:

  • Вместо case може да ползваш нещо, което показахме в сряда. Това нещо може да го изкараш в константа извън функцията. Ще стане много по-чисто.
  • Има много повторения на convert_between_temperature_units(..., 'C', unit). Спомни си, че всяко нещо е израз, включително и when :)
+ end
+end
+
+def boiling_point_of_substance(substance, unit)
+ case substance
+ when 'water'
+ convert_between_temperature_units(100, 'C', unit)
+ when 'ethanol'
+ convert_between_temperature_units(78.38, 'C', unit)
+ when 'gold'
+ convert_between_temperature_units(2_700, 'C', unit)
+ when 'silver'
+ convert_between_temperature_units(2_162, 'C', unit)
+ when 'copper'
+ convert_between_temperature_units(2_567, 'C', unit)
+ end
+end

Лазар обнови решението на 14.10.2016 00:04 (преди около 8 години)

def convert_between_temperature_units(x, t1, t2)
- case t1
- when t2
+ if t1 == t2
x
- when 'C'
- if t2 == 'F'
- x * 1.8 + 32
- else
- x + 273.15
- end
- when 'F'
- if t2 == 'K'
- (x + 459.67) * (5 / 9)
- else
- (x - 32) / 1.8
- end
- when 'K'
- if t2 == 'C'
- x - 273.15
- else
- x * (9 / 5) - 459.67
- end
- end
+ elsif t1 == 'C' && t2 == 'K'
+ x + 273.15
+ elsif t1 == 'C' && t2 == 'F'
+ x * 1.8 + 32
+ elsif t1 == 'F' && t2 == 'C'
+ (x - 32) / 1.8
+ elsif t1 == 'F' && t2 == 'K'
+ (x + 459.67) * (5 / 9)
+ elsif t1 == 'K' && t2 == 'C'
+ x - 273.15
+ elsif t1 == 'K' && t2 == 'F'
+ x * (9 / 5) - 459.67
+ else
+ -1
+ # Просто връща нещо
+ end
end
+SUBSTANCE_MELTING_POINT = {
+ 'water' => 0,
+ 'ethanol' => -114,
+ 'gold' => 1_064,
+ 'silver' => 961.8,
+ 'copper' => 1_085
+}
+
def melting_point_of_substance(substance, unit)
- case substance
- when 'water'
- convert_between_temperature_units(0, 'C', unit)
- when 'ethanol'
- convert_between_temperature_units(-114, 'C', unit)
- when 'gold'
- convert_between_temperature_units(1_064, 'C', unit)
- when 'silver'
- convert_between_temperature_units(961.8, 'C', unit)
- when 'copper'
- convert_between_temperature_units(1_085, 'C', unit)
- end
+ convert_between_temperature_units(SUBSTANCE_MELTING_POINT[substance], 'C', unit)
end
+SUBSTANCE_BOILING_POINT = {
+ 'water' => 100,
+ 'ethanol' => 78.38,
+ 'gold' => 2_700,
+ 'silver' => 2_162,
+ 'copper' => 2_567
+}
+
def boiling_point_of_substance(substance, unit)
- case substance
- when 'water'
- convert_between_temperature_units(100, 'C', unit)
- when 'ethanol'
- convert_between_temperature_units(78.38, 'C', unit)
- when 'gold'
- convert_between_temperature_units(2_700, 'C', unit)
- when 'silver'
- convert_between_temperature_units(2_162, 'C', unit)
- when 'copper'
- convert_between_temperature_units(2_567, 'C', unit)
- end
+ convert_between_temperature_units(SUBSTANCE_BOILING_POINT[substance], 'C', unit)
end