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

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

Към профила на Даниела Русева

Резултати

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

Код

def convert_to_celsius(degrees, current_unit)
if current_unit == 'K'
degrees - 273.15
elsif current_unit == 'F'
(degrees - 32) / 1.8
end

Какво ще се случи ако се опитаме да конвертираме нещо от Келвин в Келвин, например?

А този дълъг if/elsif/elsif/elsif... би могъл да се раздели на две ако конвертираш температурата на 2 стъпки (с 2 помощни функции). Така ще ти останат само конверсиите до твоята междинна мерна единица по избор, както и от нея до всички.

end
def convert_from_celsius(degrees, conversion_unit)
if conversion_unit == 'K'
degrees + 273.15
elsif conversion_unit == 'F'
degrees * 1.8 + 32
end
end
def convert_between_temperature_units(degrees, current_unit, conversion_unit)
if current_unit == conversion_unit
degrees
elsif current_unit == 'C'
convert_from_celsius(degrees, conversion_unit)
else
convert_to_celsius(degrees, current_unit)
convert_from_celsius(degrees, conversion_unit)
end
end
def melting_point_of_substance(element, unit)
melting_points = {
'water' => 0, 'ethanol' => -114, 'gold' => 1064,
'silver' => 961.8, 'copper' => 1085
}
case unit
when 'C'
melting_points[element]
when 'K'
convert_between_temperature_units(melting_points[element], 'C', 'K')
when 'F'
convert_between_temperature_units(melting_points[element], 'C', 'F')
end
end
def boiling_point_of_substance(element, unit)
boiling_points = {
'water' => 100, 'ethanol' => 78.37, 'gold' => 2700,
'silver' => 2162, 'copper' => 2567
}
case unit
when 'C'
boiling_points[element]
when 'K'
convert_between_temperature_units(boiling_points[element], 'C', 'K')
when 'F'
convert_between_temperature_units(boiling_points[element], 'C', 'F')
end
end

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

..F.FFF..........

Failures:

  1) #convert_between_temperature_units can convert Kelvin to Celsius
     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-1x8qv6k/spec.rb:57:in `expect_conversion'
     # /tmp/d20161018-13513-1x8qv6k/spec.rb:17: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 Celsius
     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-1x8qv6k/spec.rb:57:in `expect_conversion'
     # /tmp/d20161018-13513-1x8qv6k/spec.rb:31: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) #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 525.47 to be within 0.0001 of 33.8
     # /tmp/d20161018-13513-1x8qv6k/spec.rb:57:in `expect_conversion'
     # /tmp/d20161018-13513-1x8qv6k/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)>'

  4) #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 306.95 to be within 0.0001 of 274.15
     # /tmp/d20161018-13513-1x8qv6k/spec.rb:57:in `expect_conversion'
     # /tmp/d20161018-13513-1x8qv6k/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.00788 seconds
17 examples, 4 failures

Failed examples:

rspec /tmp/d20161018-13513-1x8qv6k/spec.rb:16 # #convert_between_temperature_units can convert Kelvin to Celsius
rspec /tmp/d20161018-13513-1x8qv6k/spec.rb:30 # #convert_between_temperature_units can convert Fahrenheit to Celsius
rspec /tmp/d20161018-13513-1x8qv6k/spec.rb:38 # #convert_between_temperature_units can convert Kelvin to Fahrenheit
rspec /tmp/d20161018-13513-1x8qv6k/spec.rb:46 # #convert_between_temperature_units can convert Fahrenheit to Kelvin

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

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

+def convert_between_temperature_units(degrees, current_unit, conversion_unit)
+ if current_unit == 'C' && conversion_unit == 'K'
+ degrees + 273.15
+ elsif current_unit == 'C' && conversion_unit == 'F'
+ degrees * 1.8 + 32
+ elsif current_unit == 'K' && conversion_unit == 'C'
+ degrees - 273.15
+ elsif current_unit == 'K' && conversion_unit == 'F'
+ degrees * 9 / 5 - 459.67
+ elsif current_unit == 'F' && conversion_unit == 'C'
+ (degrees - 32) / 1.8
+ elsif current_unit == 'F' && conversion_unit == 'K'
+ (degrees + 459.67) * 5 / 9
+ end

Какво ще се случи ако се опитаме да конвертираме нещо от Келвин в Келвин, например?

А този дълъг if/elsif/elsif/elsif... би могъл да се раздели на две ако конвертираш температурата на 2 стъпки (с 2 помощни функции). Така ще ти останат само конверсиите до твоята междинна мерна единица по избор, както и от нея до всички.

+end
+
+def melting_point_of_substance(element, unit)
+ elements = {
+ 'water' => 0, 'ethanol' => -114, 'gold' => 1064,
+ 'silver' => 961.8, 'copper' => 1085
+ }
+ case unit
+ when 'C'
+ elements[element]
+ when 'K'
+ convert_between_temperature_units(elements[element], 'C', 'K')
+ when 'F'
+ convert_between_temperature_units(elements[element], 'C', 'F')
+ end
+end
+
+def boiling_point_of_substance(element, unit)
+ elements = {
+ 'water' => 100, 'ethanol' => 78.37, 'gold' => 2700,
+ 'silver' => 2162, 'copper' => 2567
+ }
+ case unit
+ when 'C'
+ elements[element]
+ when 'K'
+ convert_between_temperature_units(elements[element], 'C', 'K')
+ when 'F'
+ convert_between_temperature_units(elements[element], 'C', 'F')
+ end
+end

Даниела обнови решението на 15.10.2016 23:22 (преди около 8 години)

-def convert_between_temperature_units(degrees, current_unit, conversion_unit)
- if current_unit == 'C' && conversion_unit == 'K'
- degrees + 273.15
- elsif current_unit == 'C' && conversion_unit == 'F'
- degrees * 1.8 + 32
- elsif current_unit == 'K' && conversion_unit == 'C'
+def convert_to_celsius(degrees, current_unit)
+ if current_unit == 'K'
degrees - 273.15
- elsif current_unit == 'K' && conversion_unit == 'F'
- degrees * 9 / 5 - 459.67
- elsif current_unit == 'F' && conversion_unit == 'C'
+ elsif current_unit == 'F'
(degrees - 32) / 1.8
- elsif current_unit == 'F' && conversion_unit == 'K'
- (degrees + 459.67) * 5 / 9
end
end
+def convert_from_celsius(degrees, conversion_unit)
+ if conversion_unit == 'K'
+ degrees + 273.15
+ elsif conversion_unit == 'F'
+ degrees * 1.8 + 32
+ end
+end
+
+def convert_between_temperature_units(degrees, current_unit, conversion_unit)
+ if current_unit == conversion_unit
+ degree
+ elsif current_unit == 'C'
+ convert_from_celsius(degrees, conversion_unit)
+ else
+ convert_to_celsius(degrees, current_unit)
+ convert_from_celsius(degrees, conversion_unit)
+ end
+end
+
def melting_point_of_substance(element, unit)
- elements = {
+ melting_points = {
'water' => 0, 'ethanol' => -114, 'gold' => 1064,
'silver' => 961.8, 'copper' => 1085
}
case unit
when 'C'
- elements[element]
+ melting_points[element]
when 'K'
- convert_between_temperature_units(elements[element], 'C', 'K')
+ convert_between_temperature_units(melting_points[element], 'C', 'K')
when 'F'
- convert_between_temperature_units(elements[element], 'C', 'F')
+ convert_between_temperature_units(melting_points[element], 'C', 'F')
end
end
def boiling_point_of_substance(element, unit)
- elements = {
+ boiling_points = {
'water' => 100, 'ethanol' => 78.37, 'gold' => 2700,
'silver' => 2162, 'copper' => 2567
}
case unit
when 'C'
- elements[element]
+ boiling_points[element]
when 'K'
- convert_between_temperature_units(elements[element], 'C', 'K')
+ convert_between_temperature_units(boiling_points[element], 'C', 'K')
when 'F'
- convert_between_temperature_units(elements[element], 'C', 'F')
+ convert_between_temperature_units(boiling_points[element], 'C', 'F')
end
end

Даниела обнови решението на 17.10.2016 16:32 (преди около 8 години)

def convert_to_celsius(degrees, current_unit)
if current_unit == 'K'
degrees - 273.15
elsif current_unit == 'F'
(degrees - 32) / 1.8
end
end
def convert_from_celsius(degrees, conversion_unit)
if conversion_unit == 'K'
degrees + 273.15
elsif conversion_unit == 'F'
degrees * 1.8 + 32
end
end
def convert_between_temperature_units(degrees, current_unit, conversion_unit)
if current_unit == conversion_unit
- degree
+ degrees
elsif current_unit == 'C'
convert_from_celsius(degrees, conversion_unit)
else
convert_to_celsius(degrees, current_unit)
convert_from_celsius(degrees, conversion_unit)
end
end
def melting_point_of_substance(element, unit)
melting_points = {
'water' => 0, 'ethanol' => -114, 'gold' => 1064,
'silver' => 961.8, 'copper' => 1085
}
case unit
when 'C'
melting_points[element]
when 'K'
convert_between_temperature_units(melting_points[element], 'C', 'K')
when 'F'
convert_between_temperature_units(melting_points[element], 'C', 'F')
end
end
def boiling_point_of_substance(element, unit)
boiling_points = {
'water' => 100, 'ethanol' => 78.37, 'gold' => 2700,
'silver' => 2162, 'copper' => 2567
}
case unit
when 'C'
boiling_points[element]
when 'K'
convert_between_temperature_units(boiling_points[element], 'C', 'K')
when 'F'
convert_between_temperature_units(boiling_points[element], 'C', 'F')
end
end