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

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

Към профила на Йордан Пулов

Резултати

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

Код

# convertion class for Celsius Kelvin Fahrenheit
class TemperatureConvertion
attr_accessor :from, :to
def initialize(from, to)
self.from = from
self.to = to
end
def c_to_f(celsius)
celsius * 1.8 + 32
end
def c_to_k(celsius)
celsius + 273.15
end
def f_to_c(fahrenheit)
(fahrenheit - 32) / 1.8
end
def k_to_c(kelvin)
kelvin - 273.15
end
def k_to_f(kelvin)
c_to_f(k_to_c(kelvin))
end
def f_to_k(fahrenheit)
c_to_k(f_to_c(fahrenheit))
end
def id(val)
val
end
def convert(value)
convertion = {
'C' => { 'C' => 'id', 'F' => 'c_to_f', 'K' => 'c_to_k' },
'K' => { 'C' => 'k_to_c', 'F' => 'k_to_f', 'K' => 'id' },
'F' => { 'C' => 'f_to_c', 'F' => 'id', 'K' => 'f_to_k' }
}
public_send(convertion[from][to], value)
end
end
# function that converts values between C K F
def convert_between_temperature_units(degree, from, to)
convertion = TemperatureConvertion.new(from, to)
convertion.convert(degree)
end
# helper function for melting_point_of_substance and boiling_point_of_substance
def point_of_substance(substance, degree_type, point_type)
substances = {
'water' => { 'melting' => 0, 'boiling' => 100 },
'ethanol' => { 'melting' => -114, 'boiling' => 78.37 },
'gold' => { 'melting' => 1064, 'boiling' => 2700 },
'silver' => { 'melting' => 961.8, 'boiling' => 2162 },
'copper' => { 'melting' => 1085, 'boiling' => 2567 }
}
substance_degree = substances[substance][point_type]
convert_between_temperature_units(substance_degree, 'C', degree_type)
end
def melting_point_of_substance(substance, degree_type)
point_of_substance(substance, degree_type, 'melting')
end
def boiling_point_of_substance(substance, degree_type)
point_of_substance(substance, degree_type, 'boiling')
end

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

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

Finished in 0.00873 seconds
17 examples, 0 failures

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

Йордан обнови решението на 14.10.2016 22:06 (преди около 8 години)

+# convertion helpers for Celsius Kelvin Fahrenheit
+def c_to_f(celsius)
+ celsius * 1.8 + 32
+end
+
+def c_to_k(celsius)
+ celsius + 273.15
+end
+
+def f_to_c(fahrenheit)
+ (fahrenheit - 32) / 1.8
+end
+
+def k_to_c(kelvin)
+ kelvin - 273.15
+end
+
+def k_to_f(kelvin)
+ c_to_f(k_to_c(kelvin))
+end
+
+def f_to_k(fahrenheit)
+ c_to_k(f_to_c(fahrenheit))
+end
+
+def id(val)
+ val
+end
+
+# function that converts values between C K F
+def convert_between_temperature_units(degree, from, to)
+ convertion = {
+ 'C' => { 'C' => ->(v) { id(v) }, 'F' => ->(v) { c_to_f(v) }, 'K' => ->(v) { c_to_k(v) } },

Опитай да го опростиш.

Вариант 1 - В руби има методи като send/public_send, който ти позволяват да викаш директно методи с техните имена. Разгледай ги. Най-добре ще е да сложиш един клас и да правиш public_send. Иначе с send можеш директно да викнеш методите, които са горе.

Вариант 2 - правиш първо провеката дали за from == to, т.е. gaurd клауза и горе може да обединиш методите за прехвърляне от и до целзии.

+ 'K' => { 'C' => ->(v) { k_to_c(v) }, 'F' => ->(v) { k_to_f(v) }, 'K' => ->(v) { id(v) } },
+ 'F' => { 'C' => ->(v) { f_to_c(v) }, 'F' => ->(v) { id(v) }, 'K' => ->(v) { f_to_k(v) } }
+ }
+ convertion[from][to].call(degree)
+end
+
+# helper function for melting_point_of_substance and boiling_point_of_substance
+def point_of_substance(substance, degree_type, point_type)
+ substances = {
+ 'water' => { 'melting' => 0, 'boiling' => 100 },
+ 'ethanol' => { 'melting' => -114, 'boiling' => 78.37 },
+ 'gold' => { 'melting' => 1064, 'boiling' => 2700 },
+ 'silver' => { 'melting' => 961.8, 'boiling' => 2162 },
+ 'copper' => { 'melting' => 1085, 'boiling' => 2567 }
+ }
+ substance_degree = substances[substance][point_type]
+ convert_between_temperature_units(substance_degree, 'C', degree_type)
+end
+
+def melting_point_of_substance(substance, degree_type)
+ point_of_substance(substance, degree_type, 'melting')
+end
+
+def boiling_point_of_substance(substance, degree_type)
+ point_of_substance(substance, degree_type, 'boiling')
+end

Йордан обнови решението на 16.10.2016 23:15 (преди около 8 години)

-# convertion helpers for Celsius Kelvin Fahrenheit
-def c_to_f(celsius)
- celsius * 1.8 + 32
-end
+# convertion class for Celsius Kelvin Fahrenheit
+class TemperatureConvertion
+ attr_accessor :from, :to
-def c_to_k(celsius)
- celsius + 273.15
-end
+ def initialize(from, to)
+ self.from = from
+ self.to = to
+ end
-def f_to_c(fahrenheit)
- (fahrenheit - 32) / 1.8
-end
+ def c_to_f(celsius)
+ celsius * 1.8 + 32
+ end
-def k_to_c(kelvin)
- kelvin - 273.15
-end
+ def c_to_k(celsius)
+ celsius + 273.15
+ end
-def k_to_f(kelvin)
- c_to_f(k_to_c(kelvin))
-end
+ def f_to_c(fahrenheit)
+ (fahrenheit - 32) / 1.8
+ end
-def f_to_k(fahrenheit)
- c_to_k(f_to_c(fahrenheit))
-end
+ def k_to_c(kelvin)
+ kelvin - 273.15
+ end
-def id(val)
- val
+ def k_to_f(kelvin)
+ c_to_f(k_to_c(kelvin))
+ end
+
+ def f_to_k(fahrenheit)
+ c_to_k(f_to_c(fahrenheit))
+ end
+
+ def id(val)
+ val
+ end
+
+ def convert(value)
+ convertion = {
+ 'C' => { 'C' => 'id', 'F' => 'c_to_f', 'K' => 'c_to_k' },
+ 'K' => { 'C' => 'k_to_c', 'F' => 'k_to_f', 'K' => 'id' },
+ 'F' => { 'C' => 'f_to_c', 'F' => 'id', 'K' => 'f_to_k' }
+ }
+ public_send(convertion[from][to], value)
+ end
end
-# function that converts values between C K F
+# function that converts values between C K F
def convert_between_temperature_units(degree, from, to)
- convertion = {
- 'C' => { 'C' => ->(v) { id(v) }, 'F' => ->(v) { c_to_f(v) }, 'K' => ->(v) { c_to_k(v) } },
- 'K' => { 'C' => ->(v) { k_to_c(v) }, 'F' => ->(v) { k_to_f(v) }, 'K' => ->(v) { id(v) } },
- 'F' => { 'C' => ->(v) { f_to_c(v) }, 'F' => ->(v) { id(v) }, 'K' => ->(v) { f_to_k(v) } }
- }
- convertion[from][to].call(degree)
+ convertion = TemperatureConvertion.new(from, to)
+ convertion.convert(degree)
end
# helper function for melting_point_of_substance and boiling_point_of_substance
def point_of_substance(substance, degree_type, point_type)
substances = {
'water' => { 'melting' => 0, 'boiling' => 100 },
'ethanol' => { 'melting' => -114, 'boiling' => 78.37 },
'gold' => { 'melting' => 1064, 'boiling' => 2700 },
'silver' => { 'melting' => 961.8, 'boiling' => 2162 },
'copper' => { 'melting' => 1085, 'boiling' => 2567 }
}
substance_degree = substances[substance][point_type]
convert_between_temperature_units(substance_degree, 'C', degree_type)
end
def melting_point_of_substance(substance, degree_type)
point_of_substance(substance, degree_type, 'melting')
end
def boiling_point_of_substance(substance, degree_type)
point_of_substance(substance, degree_type, 'boiling')
-end
+end