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

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

Към профила на Константин Нецов

Резултати

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

Код

def convert_between_temperature_units(degrees, measure_from, measure_to)
rez = 0
if measure_from == "C"
if measure_to == "F"
rez = (degrees * (9.0 / 5)) + 32
elsif measure_to == "K"
rez = degrees + 273.15
end
elsif measure_from == "K"
if measure_to == "F"
rez = (degrees - 273.15) * (9.0 / 5) + 32
elsif measure_to == "C"
rez = (degrees - 273.15)
elsif measure_to == "K"
rez = degrees
end
elsif measure_from == "F"
if measure_to == "C"
rez = (degrees - 32) * (5.0 / 9)
elsif measure_to == "K"
rez = (degrees - 32) * (5.0 / 9) + 273.15
elsif measure_to == "F"
rez = degrees
end
end
rez
end
# def boiling_melting(substance, measure, mode)
# rezult = 0.0
# h = {
# "water" => [0, 100],
# "ethanol" => [-114, 78.37],
# "gold" => [1064, 2700],
# "silver" => [961.8, 2162],
# "copper" => [1085, 2567]
# }
# if measure == "K" && mode == "melting"
# rezult = convert_between_temperature_units(h[substance.to_sym][0], "C", "K")
# elsif measure == "F" && mode == "melting"
# rezult = convert_between_temperature_units(h[substance.to_sym][0], "C", "F")
# elsif measure == "C" && mode == "melting"
# rezult = convert_between_temperature_units(h[substance.to_sym][0], "C", "C")
# elsif measure == "K" && mode == "boiling"
# rezult = convert_between_temperature_units(h[substance.to_sym][1], "C", "K")
# elsif measure == "F" && mode == "boiling"
# rezult = convert_between_temperature_units(h[substance.to_sym][1], "C", "F")
# elsif measure == "C" && mode == "boiling"
# rezult = convert_between_temperature_units(h[substance.to_sym][1], "C", "C")
# end
# rezult.round(2)
# end
def melting_point_of_substance(substance, measure)
# boiling_melting(substance, measure, "melting")
meltings = {
water:
{
F: convert_between_temperature_units(0, "C", "F"),
C: convert_between_temperature_units(0, "C", "C"),
K: convert_between_temperature_units(0, "C", "K")
},
ethanol:
{
F: convert_between_temperature_units(-114, "C", "F"),
C: convert_between_temperature_units(-114, "C", "C"),
K: convert_between_temperature_units(-114, "C", "K")
},
gold:
{
F: convert_between_temperature_units(1064, "C", "F"),
C: convert_between_temperature_units(1064, "C", "C"),
K: convert_between_temperature_units(1064, "C", "K")
},
silver:
{
F: convert_between_temperature_units(961.8, "C", "F"),
C: convert_between_temperature_units(961.8, "C", "C"),
K: convert_between_temperature_units(961.8, "C", "K")
},
copper:
{
F: convert_between_temperature_units(1085, "C", "F"),
C: convert_between_temperature_units(1085, "C", "C"),
K: convert_between_temperature_units(1085, "C", "K")
}
}
meltings[substance.to_sym][measure.to_sym].round(2)
end
def boiling_point_of_substance(substance, measure)
boiling = {
water:
{
F: convert_between_temperature_units(100.0, "C", "F"),
C: 100,
K: convert_between_temperature_units(100.0, "C", "K")
},
ethanol:
{
F: convert_between_temperature_units(78.37, "C", "F"),
C: convert_between_temperature_units(78.37, "C", "C"),
K: convert_between_temperature_units(78.37, "C", "K")
},
gold:
{
F: convert_between_temperature_units(2700, "C", "F"),
C: convert_between_temperature_units(2700, "C", "C"),
K: convert_between_temperature_units(2700, "C", "K")
},
silver:
{
F: convert_between_temperature_units(2162, "C", "F"),
C: convert_between_temperature_units(2162, "C", "C"),
K: convert_between_temperature_units(2162, "C", "K")
},
copper:
{
F: convert_between_temperature_units(2567, "C", "F"),
C: convert_between_temperature_units(2567, "C", "C"),
K: convert_between_temperature_units(2567, "C", "K")
}
}
boiling[substance.to_sym][measure.to_sym].round(2)
end

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

F.......FFFF.FFFF

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)
       expected 0 to be within 0.0001 of 42
     # /tmp/d20161018-13513-ok6zcz/spec.rb:57:in `expect_conversion'
     # /tmp/d20161018-13513-ok6zcz/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 0.0 to be within 0.01 of -114
     # /tmp/d20161018-13513-ok6zcz/spec.rb:93:in `expect_melting_point_of'
     # /tmp/d20161018-13513-ok6zcz/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)>'

  3) #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 0.0 to be within 0.01 of 1064
     # /tmp/d20161018-13513-ok6zcz/spec.rb:93:in `expect_melting_point_of'
     # /tmp/d20161018-13513-ok6zcz/spec.rb:75: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) #melting_point_of_substance knows the melting point of silver
     Failure/Error: expect(melting_point_of_substance(substance, units)).to be_within(0.01).of(expected_degrees)
       expected 0.0 to be within 0.01 of 961.8
     # /tmp/d20161018-13513-ok6zcz/spec.rb:93:in `expect_melting_point_of'
     # /tmp/d20161018-13513-ok6zcz/spec.rb:81: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)>'

  5) #melting_point_of_substance knows the melting point of copper
     Failure/Error: expect(melting_point_of_substance(substance, units)).to be_within(0.01).of(expected_degrees)
       expected 0.0 to be within 0.01 of 1085
     # /tmp/d20161018-13513-ok6zcz/spec.rb:93:in `expect_melting_point_of'
     # /tmp/d20161018-13513-ok6zcz/spec.rb:87: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)>'

  6) #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 0.0 to be within 0.01 of 78.37
     # /tmp/d20161018-13513-ok6zcz/spec.rb:129:in `expect_boiling_point_of'
     # /tmp/d20161018-13513-ok6zcz/spec.rb:105: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)>'

  7) #boiling_point_of_substance knows the boiling point of gold
     Failure/Error: expect(boiling_point_of_substance(substance, units)).to be_within(0.01).of(expected_degrees)
       expected 0.0 to be within 0.01 of 2700
     # /tmp/d20161018-13513-ok6zcz/spec.rb:129:in `expect_boiling_point_of'
     # /tmp/d20161018-13513-ok6zcz/spec.rb:111: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)>'

  8) #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 0.0 to be within 0.01 of 2162
     # /tmp/d20161018-13513-ok6zcz/spec.rb:129:in `expect_boiling_point_of'
     # /tmp/d20161018-13513-ok6zcz/spec.rb:117: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)>'

  9) #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 0.0 to be within 0.01 of 2567
     # /tmp/d20161018-13513-ok6zcz/spec.rb:129:in `expect_boiling_point_of'
     # /tmp/d20161018-13513-ok6zcz/spec.rb:123: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.00829 seconds
17 examples, 9 failures

Failed examples:

rspec /tmp/d20161018-13513-ok6zcz/spec.rb:2 # #convert_between_temperature_units can convert to the same unit
rspec /tmp/d20161018-13513-ok6zcz/spec.rb:68 # #melting_point_of_substance knows the melting point of ethanol
rspec /tmp/d20161018-13513-ok6zcz/spec.rb:74 # #melting_point_of_substance knows the melting point of gold
rspec /tmp/d20161018-13513-ok6zcz/spec.rb:80 # #melting_point_of_substance knows the melting point of silver
rspec /tmp/d20161018-13513-ok6zcz/spec.rb:86 # #melting_point_of_substance knows the melting point of copper
rspec /tmp/d20161018-13513-ok6zcz/spec.rb:104 # #boiling_point_of_substance knows the boiling point of ethanol
rspec /tmp/d20161018-13513-ok6zcz/spec.rb:110 # #boiling_point_of_substance knows the boiling point of gold
rspec /tmp/d20161018-13513-ok6zcz/spec.rb:116 # #boiling_point_of_substance knows the boiling point of silver
rspec /tmp/d20161018-13513-ok6zcz/spec.rb:122 # #boiling_point_of_substance knows the boiling point of copper

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

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

+def convert_between_temperature_units(degrees, measure_from, measure_to)
+ rez = 0
+ if measure_from == "C"
+ if measure_to == "F"
+ rez = (degrees * (9.0 / 5)) + 32
+ elsif measure_to == "K"
+ rez = degrees + 273.15
+ end
+ elsif measure_from == "K"
+ if measure_to == "F"
+ rez = (degrees - 273.15) * (9.0 / 5) + 32
+ elsif measure_to == "C"
+ rez = (degrees - 273.15)
+ elsif measure_to == "K"
+ rez = degrees
+ end
+ elsif measure_from == "F"
+ if measure_to == "C"
+ rez = (degrees - 32) * (5.0 / 9)
+ elsif measure_to == "K"
+ rez = (degrees - 32) * (5.0 / 9) + 273.15
+ elsif measure_to == "F"
+ rez = degrees
+ end
+ end
+ rez
+end
+
+# def boiling_melting(substance, measure, mode)
+# rezult = 0.0
+
+# h = {
+# "water" => [0, 100],
+# "ethanol" => [-114, 78.37],
+# "gold" => [1064, 2700],
+# "silver" => [961.8, 2162],
+# "copper" => [1085, 2567]
+# }
+# if measure == "K" && mode == "melting"
+# rezult = convert_between_temperature_units(h[substance.to_sym][0], "C", "K")
+# elsif measure == "F" && mode == "melting"
+# rezult = convert_between_temperature_units(h[substance.to_sym][0], "C", "F")
+# elsif measure == "C" && mode == "melting"
+# rezult = convert_between_temperature_units(h[substance.to_sym][0], "C", "C")
+# elsif measure == "K" && mode == "boiling"
+# rezult = convert_between_temperature_units(h[substance.to_sym][1], "C", "K")
+# elsif measure == "F" && mode == "boiling"
+# rezult = convert_between_temperature_units(h[substance.to_sym][1], "C", "F")
+# elsif measure == "C" && mode == "boiling"
+# rezult = convert_between_temperature_units(h[substance.to_sym][1], "C", "C")
+# end
+# rezult.round(2)
+# end
+
+def melting_point_of_substance(substance, measure)
+ # boiling_melting(substance, measure, "melting")
+ meltings = {
+ water:
+ {
+ F: convert_between_temperature_units(0, "C", "F"),
+ C: convert_between_temperature_units(0, "C", "C"),
+ K: convert_between_temperature_units(0, "C", "K")
+ },
+ ethanol:
+ {
+ F: convert_between_temperature_units(-114, "C", "F"),
+ C: convert_between_temperature_units(-114, "C", "C"),
+ K: convert_between_temperature_units(-114, "C", "K")
+ },
+ gold:
+ {
+ F: convert_between_temperature_units(1064, "C", "F"),
+ C: convert_between_temperature_units(1064, "C", "C"),
+ K: convert_between_temperature_units(1064, "C", "K")
+ },
+ silver:
+ {
+ F: convert_between_temperature_units(961.8, "C", "F"),
+ C: convert_between_temperature_units(961.8, "C", "C"),
+ K: convert_between_temperature_units(961.8, "C", "K")
+ },
+ copper:
+ {
+ F: convert_between_temperature_units(1085, "C", "F"),
+ C: convert_between_temperature_units(1085, "C", "C"),
+ K: convert_between_temperature_units(1085, "C", "K")
+ }
+ }
+ meltings[substance.to_sym][measure.to_sym].round(2)
+end
+
+def boiling_point_of_substance(substance, measure)
+
+ boiling = {
+ water:
+ {
+ F: convert_between_temperature_units(100.0, "C", "F"),
+ C: 100,
+ K: convert_between_temperature_units(100.0, "C", "K")
+ },
+ ethanol:
+ {
+ F: convert_between_temperature_units(78.37, "C", "F"),
+ C: convert_between_temperature_units(78.37, "C", "C"),
+ K: convert_between_temperature_units(78.37, "C", "K")
+ },
+ gold:
+ {
+ F: convert_between_temperature_units(2700, "C", "F"),
+ C: convert_between_temperature_units(2700, "C", "C"),
+ K: convert_between_temperature_units(2700, "C", "K")
+ },
+ silver:
+ {
+ F: convert_between_temperature_units(2162, "C", "F"),
+ C: convert_between_temperature_units(2162, "C", "C"),
+ K: convert_between_temperature_units(2162, "C", "K")
+ },
+ copper:
+ {
+ F: convert_between_temperature_units(2567, "C", "F"),
+ C: convert_between_temperature_units(2567, "C", "C"),
+ K: convert_between_temperature_units(2567, "C", "K")
+ }
+ }
+ boiling[substance.to_sym][measure.to_sym].round(2)
+end