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

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

Към профила на Здравко Петров

Резултати

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

Код

def convert_from_celsium(degree, unit)
from_celsium = {'C' => degree, 'F' => (degree * 1.8) + 32, 'K' => (degree + 273.15)}
from_celsium[unit]
end
def convert_from_fahrenheit(degree, unit)
from_fahrenheit = {'F' => degree, 'C' => (degree - 32) / 1.8, 'K' => (degree + 459.67) / 1.8}
from_fahrenheit[unit]
end
def convert_from_kalvin(degree, unit)
from_kalvin = {'K' => degree, 'C' => (degree - 273.15), 'F' => (degree * 1.8) - 459.67}
from_kalvin[unit]
end
def convert_between_temperature_units(degree, current_unit, convert_unit)
if current_unit == 'C'
convert_from_celsium(degree, convert_unit)
elsif current_unit == 'F'
convert_from_fahrenheit(degree, convert_unit)
elsif current_unit == 'K'
convert_from_kalvin(degree, convert_unit)
end
end
MELTING_POINT = { 'water' => 0, 'ethanol' => -114, 'gold' => 1064, 'silver' => 961.8, 'copper' => 1085 }
def melting_point_of_substance(substance, unit)
convert_between_temperature_units(MELTING_POINT[substance], 'C', unit)
end
BOILING_POINT = { 'water' => 100, 'ethanol' => 78.37, 'gold' => 2700, 'silver' => 2162, 'copper' => 2567 }
def boiling_point_of_substance(substance, unit)
convert_between_temperature_units(BOILING_POINT[substance], 'C', unit)
end

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

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

Finished in 0.00952 seconds
17 examples, 0 failures

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

Здравко обнови решението на 11.10.2016 17:49 (преди около 8 години)

+def convert_between_temperature_units(degree, current_unit, convert_unit)
+ if current_unit == 'C'
+ if convert_unit == 'C'
+ degree
+ elsif convert_unit == 'F'
+ (degree * 1.8 + 32.0)
+ elsif convert_unit == 'K'
+ (degree + 273.15)
+ end
+ elsif current_unit == 'F'
+ if convert_unit == 'F'
+ degree
+ elsif convert_unit == 'C'
+ (degree - 32) / 1.8
+ elsif convert_unit == 'K'
+ (degree + 459.67) / 1.8
+ end
+ elsif current_unit == 'K'
+ if convert_unit == 'K'
+ degree
+ elsif convert_unit == 'C'
+ (degree - 273.15)
+ elsif convert_unit == 'F'
+ (degree * 1.8 - 459.67)
+ end
+ end
+end
+
+def melting_point_of_substance(substance, unit)
+ if substance == 'water'
+ if unit == 'C'
+ 0
+ elsif unit == 'K' || unit == 'F'
+ convert_between_temperature_units(0, 'C', unit)
+ end
+ elsif substance == 'ethanol'
+ if unit == 'C'
+ -114
+ elsif unit == 'K' || unit == 'F'
+ convert_between_temperature_units(-114, 'C', unit)
+ end
+ elsif substance == 'gold'
+ if unit == 'C'
+ 1064
+ elsif unit == 'K' || unit == 'F'
+ convert_between_temperature_units(1064, 'C', unit)
+ end
+ elsif substance == 'silver'
+ if unit == 'C'
+ 961.8
+ elsif unit == 'K' || unit == 'F'
+ convert_between_temperature_units(961.8, 'C', unit)
+ end
+ elsif substance == 'copper'
+ if unit == 'C'
+ 1085
+ elsif unit == 'K' || unit == 'F'
+ convert_between_temperature_units(1085, 'C', unit)
+ end
+ end
+end
+
+def boiling_point_of_substance(substance, unit)
+ if substance == 'water'
+ if unit == 'C'
+ 100
+ elsif unit == 'K' || unit == 'F'
+ convert_between_temperature_units(100, 'C', unit)
+ end
+ elsif substance == 'ethanol'
+ if unit == 'C'
+ 78.37
+ elsif unit == 'K' || unit == 'F'
+ convert_between_temperature_units(78.37, 'C', unit)
+ end
+ elsif substance == 'gold'
+ if unit == 'C'
+ 2700
+ elsif unit == 'K' || unit == 'F'
+ convert_between_temperature_units(2700, 'C', unit)
+ end
+ elsif substance == 'silver'
+ if unit == 'C'
+ 2162
+ elsif unit == 'K' || unit == 'F'
+ convert_between_temperature_units(2162, 'C', unit)
+ end
+ elsif substance == 'copper'
+ if unit == 'C'
+ 2567
+ elsif unit == 'K' || unit == 'F'
+ convert_between_temperature_units(2567, 'C', unit)
+ end
+ end
+end

Здравко обнови решението на 11.10.2016 17:52 (преди около 8 години)

def convert_between_temperature_units(degree, current_unit, convert_unit)
if current_unit == 'C'
if convert_unit == 'C'
degree
elsif convert_unit == 'F'
(degree * 1.8 + 32.0)
elsif convert_unit == 'K'
(degree + 273.15)
end
elsif current_unit == 'F'
if convert_unit == 'F'
degree
elsif convert_unit == 'C'
(degree - 32) / 1.8
elsif convert_unit == 'K'
(degree + 459.67) / 1.8
end
elsif current_unit == 'K'
if convert_unit == 'K'
degree
elsif convert_unit == 'C'
(degree - 273.15)
elsif convert_unit == 'F'
(degree * 1.8 - 459.67)
end
end
end
def melting_point_of_substance(substance, unit)
if substance == 'water'
- if unit == 'C'
- 0
- elsif unit == 'K' || unit == 'F'
+ if unit == 'K' || unit == 'F' || unit == 'C'
convert_between_temperature_units(0, 'C', unit)
end
elsif substance == 'ethanol'
- if unit == 'C'
- -114
- elsif unit == 'K' || unit == 'F'
+ if unit == 'K' || unit == 'F' || unit == 'C'
convert_between_temperature_units(-114, 'C', unit)
end
elsif substance == 'gold'
- if unit == 'C'
- 1064
- elsif unit == 'K' || unit == 'F'
+ if unit == 'K' || unit == 'F' || unit == 'C'
convert_between_temperature_units(1064, 'C', unit)
end
elsif substance == 'silver'
- if unit == 'C'
- 961.8
- elsif unit == 'K' || unit == 'F'
+ if unit == 'K' || unit == 'F' || unit == 'C'
convert_between_temperature_units(961.8, 'C', unit)
end
elsif substance == 'copper'
- if unit == 'C'
- 1085
- elsif unit == 'K' || unit == 'F'
+ if unit == 'K' || unit == 'F' || unit == 'C'
convert_between_temperature_units(1085, 'C', unit)
end
end
end
def boiling_point_of_substance(substance, unit)
if substance == 'water'
- if unit == 'C'
- 100
- elsif unit == 'K' || unit == 'F'
+ if unit == 'K' || unit == 'F' || unit == 'C'
convert_between_temperature_units(100, 'C', unit)
end
elsif substance == 'ethanol'
- if unit == 'C'
- 78.37
- elsif unit == 'K' || unit == 'F'
+ if unit == 'K' || unit == 'F' || unit == 'C'
convert_between_temperature_units(78.37, 'C', unit)
end
elsif substance == 'gold'
- if unit == 'C'
- 2700
- elsif unit == 'K' || unit == 'F'
+ if unit == 'K' || unit == 'F' || unit == 'C'
convert_between_temperature_units(2700, 'C', unit)
end
elsif substance == 'silver'
- if unit == 'C'
- 2162
- elsif unit == 'K' || unit == 'F'
+ if unit == 'K' || unit == 'F' || unit == 'C'
convert_between_temperature_units(2162, 'C', unit)
end
elsif substance == 'copper'
- if unit == 'C'
- 2567
- elsif unit == 'K' || unit == 'F'
+ if unit == 'K' || unit == 'F' || unit == 'C'
convert_between_temperature_units(2567, 'C', unit)
end
end
end

Здравко обнови решението на 11.10.2016 17:58 (преди около 8 години)

def convert_between_temperature_units(degree, current_unit, convert_unit)
if current_unit == 'C'
if convert_unit == 'C'
degree
elsif convert_unit == 'F'
(degree * 1.8 + 32.0)
elsif convert_unit == 'K'
(degree + 273.15)
end
elsif current_unit == 'F'
if convert_unit == 'F'
degree
elsif convert_unit == 'C'
(degree - 32) / 1.8
elsif convert_unit == 'K'
(degree + 459.67) / 1.8
end
elsif current_unit == 'K'
if convert_unit == 'K'
degree
elsif convert_unit == 'C'
(degree - 273.15)
elsif convert_unit == 'F'
(degree * 1.8 - 459.67)
end
end
end
def melting_point_of_substance(substance, unit)
- if substance == 'water'
- if unit == 'K' || unit == 'F' || unit == 'C'
- convert_between_temperature_units(0, 'C', unit)
+ result =
+ if substance == 'water'
+ 0
+ elsif substance == 'ethanol'
+ -114
+ elsif substance == 'gold'
+ 1064
+ elsif substance == 'silver'
+ 961.8
+ elsif substance == 'copper'
+ 1085
end
- elsif substance == 'ethanol'
- if unit == 'K' || unit == 'F' || unit == 'C'
- convert_between_temperature_units(-114, 'C', unit)
- end
- elsif substance == 'gold'
- if unit == 'K' || unit == 'F' || unit == 'C'
- convert_between_temperature_units(1064, 'C', unit)
- end
- elsif substance == 'silver'
- if unit == 'K' || unit == 'F' || unit == 'C'
- convert_between_temperature_units(961.8, 'C', unit)
- end
- elsif substance == 'copper'
- if unit == 'K' || unit == 'F' || unit == 'C'
- convert_between_temperature_units(1085, 'C', unit)
- end
- end
+ convert_between_temperature_units(result, 'C', unit)
end
def boiling_point_of_substance(substance, unit)
- if substance == 'water'
- if unit == 'K' || unit == 'F' || unit == 'C'
- convert_between_temperature_units(100, 'C', unit)
+ result =
+ if substance == 'water'
+ 100
+ elsif substance == 'ethanol'
+ 78.37
+ elsif substance == 'gold'
+ 2700
+ elsif substance == 'silver'
+ 2162
+ elsif substance == 'copper'
+ 2567
end
- elsif substance == 'ethanol'
- if unit == 'K' || unit == 'F' || unit == 'C'
- convert_between_temperature_units(78.37, 'C', unit)
- end
- elsif substance == 'gold'
- if unit == 'K' || unit == 'F' || unit == 'C'
- convert_between_temperature_units(2700, 'C', unit)
- end
- elsif substance == 'silver'
- if unit == 'K' || unit == 'F' || unit == 'C'
- convert_between_temperature_units(2162, 'C', unit)
- end
- elsif substance == 'copper'
- if unit == 'K' || unit == 'F' || unit == 'C'
- convert_between_temperature_units(2567, 'C', unit)
- end
- end
+ convert_between_temperature_units(result, 'C', unit)
end

Здравко обнови решението на 12.10.2016 20:58 (преди около 8 години)

def convert_between_temperature_units(degree, current_unit, convert_unit)
if current_unit == 'C'
if convert_unit == 'C'
degree
elsif convert_unit == 'F'
(degree * 1.8 + 32.0)
elsif convert_unit == 'K'
(degree + 273.15)
+ else
+ "Invalid input"

В условието пише, цитирам, Няма да подаваме мерни единици, различни от тези три.. Няма нужда да проверяваш за този случай. Просто изпусни else-а.

Също, да върнеш стринг не е добро поведение при грешка.

end
elsif current_unit == 'F'
if convert_unit == 'F'
degree
elsif convert_unit == 'C'
(degree - 32) / 1.8
elsif convert_unit == 'K'
(degree + 459.67) / 1.8
+ else
+ "Invalid input"
end
elsif current_unit == 'K'
if convert_unit == 'K'
degree
elsif convert_unit == 'C'
(degree - 273.15)
elsif convert_unit == 'F'
(degree * 1.8 - 459.67)
end
+ else
+ "Invalid input"
end
end
def melting_point_of_substance(substance, unit)
result =
if substance == 'water'
0
elsif substance == 'ethanol'
-114
elsif substance == 'gold'
1064
elsif substance == 'silver'
961.8
elsif substance == 'copper'
1085
end
convert_between_temperature_units(result, 'C', unit)
end
def boiling_point_of_substance(substance, unit)
result =
if substance == 'water'
100
elsif substance == 'ethanol'
78.37
elsif substance == 'gold'
2700
elsif substance == 'silver'
2162
elsif substance == 'copper'
2567
end
convert_between_temperature_units(result, 'C', unit)
end

Здравко обнови решението на 14.10.2016 13:56 (преди около 8 години)

def convert_between_temperature_units(degree, current_unit, convert_unit)
if current_unit == 'C'
if convert_unit == 'C'
degree
elsif convert_unit == 'F'
(degree * 1.8 + 32.0)
elsif convert_unit == 'K'
(degree + 273.15)
- else
- "Invalid input"
end
elsif current_unit == 'F'
if convert_unit == 'F'
degree
elsif convert_unit == 'C'
(degree - 32) / 1.8
elsif convert_unit == 'K'
(degree + 459.67) / 1.8
- else
- "Invalid input"
end
elsif current_unit == 'K'
if convert_unit == 'K'
degree
elsif convert_unit == 'C'
(degree - 273.15)
elsif convert_unit == 'F'
(degree * 1.8 - 459.67)
end
- else
- "Invalid input"
end
end
+MELTING_UNIT = { 'water' => 0, 'ethanol' => -114, 'gold' => 1064, 'silver' => 961.8, 'copper' => 1085 }
+
def melting_point_of_substance(substance, unit)
- result =
- if substance == 'water'
- 0
- elsif substance == 'ethanol'
- -114
- elsif substance == 'gold'
- 1064
- elsif substance == 'silver'
- 961.8
- elsif substance == 'copper'
- 1085
- end
- convert_between_temperature_units(result, 'C', unit)
+ convert_between_temperature_units(MELTING_UNIT[substance], 'C', unit)
end
+BOILING_UNIT = { 'water' => 100, 'ethanol' => 78.37, 'gold' => 2700, 'silver' => 2162, 'copper' => 2567 }
+
def boiling_point_of_substance(substance, unit)
- result =
- if substance == 'water'
- 100
- elsif substance == 'ethanol'
- 78.37
- elsif substance == 'gold'
- 2700
- elsif substance == 'silver'
- 2162
- elsif substance == 'copper'
- 2567
- end
- convert_between_temperature_units(result, 'C', unit)
+ convert_between_temperature_units(BOILING_UNIT[substance], 'C', unit)
end

Здравко обнови решението на 16.10.2016 22:09 (преди около 8 години)

def convert_between_temperature_units(degree, current_unit, convert_unit)
+ from_celsium = {'C' => degree, 'F' => (degree * 1.8) + 32, 'K' => (degree + 273.15)}
+ # Незнам дали това го приемате като валиден хеш, но импровизирах да видя дали ще стане и проработи.
+ from_fahrenheit = {'F' => degree, 'C' => (degree - 32) / 1.8, 'K' => (degree + 459.67) / 1.8}
+ from_kalvin = {'K' => degree, 'C' => (degree - 273.15), 'F' => (degree * 1.8) - 459.67}
if current_unit == 'C'
- if convert_unit == 'C'
- degree
- elsif convert_unit == 'F'
- (degree * 1.8 + 32.0)
- elsif convert_unit == 'K'
- (degree + 273.15)
- end
+ from_celsium[convert_unit]
elsif current_unit == 'F'
- if convert_unit == 'F'
- degree
- elsif convert_unit == 'C'
- (degree - 32) / 1.8
- elsif convert_unit == 'K'
- (degree + 459.67) / 1.8
- end
+ from_fahrenheit[convert_unit]
elsif current_unit == 'K'
- if convert_unit == 'K'
- degree
- elsif convert_unit == 'C'
- (degree - 273.15)
- elsif convert_unit == 'F'
- (degree * 1.8 - 459.67)
- end
+ from_kalvin[convert_unit]
end
end
MELTING_UNIT = { 'water' => 0, 'ethanol' => -114, 'gold' => 1064, 'silver' => 961.8, 'copper' => 1085 }
def melting_point_of_substance(substance, unit)
convert_between_temperature_units(MELTING_UNIT[substance], 'C', unit)
end
BOILING_UNIT = { 'water' => 100, 'ethanol' => 78.37, 'gold' => 2700, 'silver' => 2162, 'copper' => 2567 }
def boiling_point_of_substance(substance, unit)
convert_between_temperature_units(BOILING_UNIT[substance], 'C', unit)
end

Здравко обнови решението на 17.10.2016 13:43 (преди около 8 години)

-def convert_between_temperature_units(degree, current_unit, convert_unit)
+def convert_from_celsium(degree, unit)
from_celsium = {'C' => degree, 'F' => (degree * 1.8) + 32, 'K' => (degree + 273.15)}
- # Незнам дали това го приемате като валиден хеш, но импровизирах да видя дали ще стане и проработи.
+ from_celsium[unit]
+end
+
+def convert_from_fahrenheit(degree, unit)
from_fahrenheit = {'F' => degree, 'C' => (degree - 32) / 1.8, 'K' => (degree + 459.67) / 1.8}
+ from_fahrenheit[unit]
+end
+
+def convert_from_kalvin(degree, unit)
from_kalvin = {'K' => degree, 'C' => (degree - 273.15), 'F' => (degree * 1.8) - 459.67}
+ from_kalvin[unit]
+end
+
+def convert_between_temperature_units(degree, current_unit, convert_unit)
if current_unit == 'C'
- from_celsium[convert_unit]
+ convert_from_celsium(degree, convert_unit)
elsif current_unit == 'F'
- from_fahrenheit[convert_unit]
+ convert_from_fahrenheit(degree, convert_unit)
elsif current_unit == 'K'
- from_kalvin[convert_unit]
+ convert_from_kalvin(degree, convert_unit)
end
end
-MELTING_UNIT = { 'water' => 0, 'ethanol' => -114, 'gold' => 1064, 'silver' => 961.8, 'copper' => 1085 }
+MELTING_POINT = { 'water' => 0, 'ethanol' => -114, 'gold' => 1064, 'silver' => 961.8, 'copper' => 1085 }
def melting_point_of_substance(substance, unit)
- convert_between_temperature_units(MELTING_UNIT[substance], 'C', unit)
+ convert_between_temperature_units(MELTING_POINT[substance], 'C', unit)
end
-BOILING_UNIT = { 'water' => 100, 'ethanol' => 78.37, 'gold' => 2700, 'silver' => 2162, 'copper' => 2567 }
+BOILING_POINT = { 'water' => 100, 'ethanol' => 78.37, 'gold' => 2700, 'silver' => 2162, 'copper' => 2567 }
def boiling_point_of_substance(substance, unit)
- convert_between_temperature_units(BOILING_UNIT[substance], 'C', unit)
+ convert_between_temperature_units(BOILING_POINT[substance], 'C', unit)
end