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

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

Към профила на Христо Христов

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 17 успешни тест(а)
  • 0 неуспешни тест(а)

Код

HASH_TO_CELSIUM = {
'F' => -> (temperature) { (temperature - 32) * (5.0 / 9) },
'K' => -> (temperature) { temperature - 273.15 },
'C' => -> (t) { t }
}
HASH_FROM_CELSIUM = {
'F' => -> (temperature) { temperature * (9.0 / 5) + 32 },
'K' => -> (temperature) { temperature + 273.15 },
'C' => -> (t) { t }
}
def convert_between_temperature_units(temperature, type, type_goal)
temperature = HASH_TO_CELSIUM[type].call(temperature)
temperature = HASH_FROM_CELSIUM[type_goal].call(temperature)
temperature.round(7)
end
DATA_MELT = {'water' => 0, 'ethanol' => -114, 'gold' => 1064, 'silver' => 961.8, 'copper' => 1085}
DATA_BOIL = {'water' => 100, 'ethanol' => 78.37, 'gold' => 2700, 'silver' => 2162, 'copper' => 2567}
def melting_point_of_substance(substance, type_goal)
convert_between_temperature_units(DATA_MELT[substance], 'C', type_goal)
end
def boiling_point_of_substance(substance, type_goal)
convert_between_temperature_units(DATA_BOIL[substance], 'C', type_goal)
end

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

.................

Finished in 0.00955 seconds
17 examples, 0 failures

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

Христо обнови решението на 11.10.2016 19:27 (преди около 8 години)

+def convert_between_temperature_units(temperature, type, type_goal)
+ case type
+ when 'F'
+ temperature = (temperature - 32) * (5.0 / 9)
+ when 'K'
+ temperature -= 273.15
+ when 'C'
+ else
+ puts "wat"
+ end
+
+ case type_goal
+ when 'F'
+ temperature = temperature * (9.0 / 5) + 32
+ when 'K'
+ temperature += 273.15
+ when 'C'
+
+ else
+ puts "wat"
+ end
+
+ # return temperature
+ temperature
+end
+
+puts "Input temperature in degrees"
+
+temperature = gets.to_f
+
+puts "Input degree scale(C, F, K)"
+
+type = gets.to_s
+type.delete!("\n")
+
+puts "Input desired degree scale(C, F, K)"
+
+type_goal = gets.to_s
+type_goal.delete!("\n")
+
+convert_between_temperature_units(temperature, type, type_goal)
+
+# ---------------------------------
+
+def read_substance(input)
+ case input
+ when 'water'
+ return 0
+ when 'ethanol'
+ return 1
+ when 'gold'
+ return 2
+ when 'silver'
+ return 3
+ when 'copper'
+ return 4
+ else
+ return "wot"
+end
+
+def melting_point_of_substance(substance, type_goal)
+ data_melt = [0, -114, 1064, 961.8, 1085]
+
+ temperature = data_melt[read_substance(substance).to_i]
+
+ # return convert_between_temperature_units(temperature,'C',type_goal)
+
+ convert_between_temperature_units(temperature, 'C', type_goal)
+end
+
+puts "Enter substance"
+
+substance = gets.to_s
+substance.delete!("\n")
+
+puts "Enter desired degree scale"
+
+type_goal = gets.to_s
+type_goal.delete!("\n")
+
+melting_point_of_substance(substance, type_goal)
+
+def boiling_point_of_substance(substance, type_goal)
+ data_boil = [100, 78.37, 2700, 2162, 2567]
+
+ temperature = data_boil[read_substance(substance).to_i]
+
+ # return convert_between_temperature_units(temperature,'C',type_goal)
+
+ convert_between_temperature_units(temperature, 'C', type_goal)
+end
+
+puts "Enter substance"
+
+substance = gets.to_s
+substance.delete!("\n")
+
+puts "Enter desired degree scale"
+
+type_goal = gets.to_s
+type_goal.delete!("\n")
+
+boiling_point_of_substance(substance, type_goal)

Христо обнови решението на 11.10.2016 22:19 (преди около 8 години)

def convert_between_temperature_units(temperature, type, type_goal)
- case type
- when 'F'
- temperature = (temperature - 32) * (5.0 / 9)
- when 'K'
- temperature -= 273.15
- when 'C'
- else
- puts "wat"
- end
-
- case type_goal
- when 'F'
- temperature = temperature * (9.0 / 5) + 32
- when 'K'
- temperature += 273.15
- when 'C'
-
- else
- puts "wat"
- end
-
- # return temperature
- temperature
+ temperature =
+ case type
+ when 'F' then (temperature - 32) * (5.0 / 9)
+ when 'K' then temperature - 273.15
+ when 'C' then temperature
+ else "wat"

Не е задължително case-овете да имат else. Казали сме, че няма да подаваме невалидни мерни единици => по-добре да не се грижиш за този случай. Също, това да върнеш някакъв стринг е много по-лошо, отколкото да го оставиш да гръмне (или да върне nil).

+ end
+
+ case type_goal
+ when 'F' then temperature * (9.0 / 5) + 32
+ when 'K' then temperature + 273.15
+ when 'C' then temperature
+ else "wat"
+ end
end
-puts "Input temperature in degrees"
-
-temperature = gets.to_f
-
-puts "Input degree scale(C, F, K)"
-
-type = gets.to_s
-type.delete!("\n")
-
-puts "Input desired degree scale(C, F, K)"
-
-type_goal = gets.to_s
-type_goal.delete!("\n")
-
-convert_between_temperature_units(temperature, type, type_goal)
-
-# ---------------------------------
-
def read_substance(input)
case input
- when 'water'
- return 0
- when 'ethanol'
- return 1
- when 'gold'
- return 2
- when 'silver'
- return 3
- when 'copper'
- return 4
- else
- return "wot"
+ when 'water' then 0
+ when 'ethanol' then 1
+ when 'gold' then 2
+ when 'silver' then 3
+ when 'copper' then 4
+ else "wot"
end
def melting_point_of_substance(substance, type_goal)
data_melt = [0, -114, 1064, 961.8, 1085]
temperature = data_melt[read_substance(substance).to_i]
- # return convert_between_temperature_units(temperature,'C',type_goal)
-
convert_between_temperature_units(temperature, 'C', type_goal)
end
-puts "Enter substance"
-
-substance = gets.to_s
-substance.delete!("\n")
-
-puts "Enter desired degree scale"
-
-type_goal = gets.to_s
-type_goal.delete!("\n")
-
-melting_point_of_substance(substance, type_goal)
-
def boiling_point_of_substance(substance, type_goal)
data_boil = [100, 78.37, 2700, 2162, 2567]
temperature = data_boil[read_substance(substance).to_i]
- # return convert_between_temperature_units(temperature,'C',type_goal)
-
convert_between_temperature_units(temperature, 'C', type_goal)
end
-
-puts "Enter substance"
-
-substance = gets.to_s
-substance.delete!("\n")
-
-puts "Enter desired degree scale"
-
-type_goal = gets.to_s
-type_goal.delete!("\n")
-
-boiling_point_of_substance(substance, type_goal)

Христо обнови решението на 14.10.2016 02:43 (преди около 8 години)

+HASH_TO_CELSIUM = {'F' => 1.to_i, 'K' => -1.to_i}
+
def convert_between_temperature_units(temperature, type, type_goal)
- temperature =
- case type
- when 'F' then (temperature - 32) * (5.0 / 9)
- when 'K' then temperature - 273.15
- when 'C' then temperature
- else "wat"
- end
- case type_goal
- when 'F' then temperature * (9.0 / 5) + 32
- when 'K' then temperature + 273.15
- when 'C' then temperature
- else "wat"
- end
-end
+ if type != 'C'
+ temperature = ( (temperature - 32) * 5.0 / 9) * ((HASH_TO_CELSIUM[type] + 1) / 2) + ( temperature - 273.15 ) * ( HASH_TO_CELSIUM[type] - 1) / -2
+ end
-def read_substance(input)
- case input
- when 'water' then 0
- when 'ethanol' then 1
- when 'gold' then 2
- when 'silver' then 3
- when 'copper' then 4
- else "wot"
+ if type_goal != 'C'
+ temperature = (temperature * (9.0 / 5) + 32) * ((HASH_TO_CELSIUM[type_goal] + 1) / 2) + (temperature + 273.15) * ( HASH_TO_CELSIUM[type_goal] - 1) / -2
end
+
+ temperature.round(7)
end
+DATA_MELT = {'water' => 0, 'ethanol' => -114, 'gold' => 1064, 'silver' => 961.8, 'copper' => 1085}
+DATA_BOIL = {'water' => 100, 'ethanol' => 78.37, 'gold' => 2700, 'silver' => 2162, 'copper' => 2567}
+
def melting_point_of_substance(substance, type_goal)
- data_melt = [0, -114, 1064, 961.8, 1085]
-
- temperature = data_melt[read_substance(substance).to_i]
-
- convert_between_temperature_units(temperature, 'C', type_goal)
+ convert_between_temperature_units(DATA_MELT[substance], 'C', type_goal)
end
def boiling_point_of_substance(substance, type_goal)
- data_boil = [100, 78.37, 2700, 2162, 2567]
-
+ convert_between_temperature_units(DATA_BOIL[substance], 'C', type_goal)
- temperature = data_boil[read_substance(substance).to_i]
+end
-
- convert_between_temperature_units(temperature, 'C', type_goal)
-end

Пределно ясно ми е, че първите 11 реда са канцерогенни, но просто реших да споделя какво ми хрумна в този късен час :D

По някое време ще го сменя, разбира се, но може да се използва за плашене на малки и големи, ддз

Христо обнови решението на 17.10.2016 01:14 (преди около 8 години)

-HASH_TO_CELSIUM = {'F' => 1.to_i, 'K' => -1.to_i}
+HASH_TO_CELSIUM = {
+ 'F' => -> (temperature) { (temperature - 32) * (5.0 / 9) },
+ 'K' => -> (temperature) { temperature - 273.15 },
+ 'C' => -> (t) { t }
+}
+HASH_FROM_CELSIUM = {
+ 'F' => -> (temperature) { temperature * (9.0 / 5) + 32 },
+ 'K' => -> (temperature) { temperature + 273.15 },
+ 'C' => -> (t) { t }
+}
+
def convert_between_temperature_units(temperature, type, type_goal)
+ temperature = HASH_TO_CELSIUM[type].call(temperature)
- if type != 'C'
- temperature = ( (temperature - 32) * 5.0 / 9) * ((HASH_TO_CELSIUM[type] + 1) / 2) + ( temperature - 273.15 ) * ( HASH_TO_CELSIUM[type] - 1) / -2
- end
-
- if type_goal != 'C'
- temperature = (temperature * (9.0 / 5) + 32) * ((HASH_TO_CELSIUM[type_goal] + 1) / 2) + (temperature + 273.15) * ( HASH_TO_CELSIUM[type_goal] - 1) / -2
- end
-
+ temperature = HASH_FROM_CELSIUM[type_goal].call(temperature)
+
temperature.round(7)
end
DATA_MELT = {'water' => 0, 'ethanol' => -114, 'gold' => 1064, 'silver' => 961.8, 'copper' => 1085}
DATA_BOIL = {'water' => 100, 'ethanol' => 78.37, 'gold' => 2700, 'silver' => 2162, 'copper' => 2567}
def melting_point_of_substance(substance, type_goal)
convert_between_temperature_units(DATA_MELT[substance], 'C', type_goal)
end
def boiling_point_of_substance(substance, type_goal)
convert_between_temperature_units(DATA_BOIL[substance], 'C', type_goal)
-end
+end