Решение на Първа задача от Михаела Иванова

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

Към профила на Михаела Иванова

Резултати

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

Код

def convert_to_bgn(price, currency)
case currency
when :usd then (price * 1.7408).round(2)
when :eur then (price * 1.9557).round(2)
when :gbp then (price * 2.6415).round(2)
when :bgn then price.round(2)
end
end
def compare_prices(first_price, first_currency, second_price, second_currency)
first_price_in_bgn = convert_to_bgn(first_price, first_currency)
second_price_in_bgn = convert_to_bgn(second_price, second_currency)
first_price_in_bgn <=> second_price_in_bgn
end

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

........

Finished in 0.00641 seconds
8 examples, 0 failures

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

Михаела обнови решението на 08.10.2015 22:07 (преди над 8 години)

+def convert_to_bgn(value, currency)
+ case currency
+ when :usd then (value*1.7408).round(2)
+ when :eur then (value*1.9557).round(2)
+ when :gbp then (value*2.6415).round(2)
+ end
+end
+def compare_prices (value_one, currency_one, value_two, currency_two)
+ first_price = convert_to_bgn(value_one, currency_one)
+ second_price = convert_to_bgn(value_two, currency_two)
+ first_price <=> second_price
+end

Като цяло добре, малко храна за размисъл:

  • Дали има още някакъв случай (отвъд тези 3)?
  • Погледни style guide-a. Има няколко whitespace-а, които би било добре да добавиш/махнеш
  • Опитай се да измислиш по-добро име от value. Има ли нещо, което по-точно назовава въпросното нещо?
  • Добре е да избираш консистентни имена. first_price и value_one като че ли са почти едно и също нещо. Няма причина да използваш различни думи (first vs one, price vs value, редът на двете е разменен)

Михаела обнови решението на 08.10.2015 23:57 (преди над 8 години)

-def convert_to_bgn(value, currency)
+def convert_to_bgn(price, currency)
case currency
- when :usd then (value*1.7408).round(2)
- when :eur then (value*1.9557).round(2)
- when :gbp then (value*2.6415).round(2)
+ when :usd then (price * 1.7408).round(2)
+ when :eur then (price * 1.9557).round(2)
+ when :gbp then (price * 2.6415).round(2)
+ when :bgn then price.round(2)
end
end
-def compare_prices (value_one, currency_one, value_two, currency_two)
- first_price = convert_to_bgn(value_one, currency_one)
- second_price = convert_to_bgn(value_two, currency_two)
- first_price <=> second_price
+def compare_prices(price_one, currency_one, price_two, currency_two)
+ price_one = convert_to_bgn(price_one, currency_one)
+ price_two = convert_to_bgn(price_two, currency_two)
+ price_one <=> price_two
end

Въпрос, в първия вариант на решението ми price_one и first_price не са ли различни неща? И ако са, как тогава вторият вариант работи? Първо се оценява convert_to_bgn(price_one, currency_one) и след това price_one се свързва с тази оценка?

Михаела обнови решението на 11.10.2015 21:24 (преди над 8 години)

def convert_to_bgn(price, currency)
case currency
when :usd then (price * 1.7408).round(2)
when :eur then (price * 1.9557).round(2)
when :gbp then (price * 2.6415).round(2)
when :bgn then price.round(2)
end
end
-def compare_prices(price_one, currency_one, price_two, currency_two)
- price_one = convert_to_bgn(price_one, currency_one)
- price_two = convert_to_bgn(price_two, currency_two)
- price_one <=> price_two
+
+def compare_prices(first_price, first_currency, second_price, second_currency)
+ first_price_in_bgn = convert_to_bgn(first_price, first_currency)
+ second_price_in_bgn = convert_to_bgn(second_price, second_currency)
+ first_price_in_bgn <=> second_price_in_bgn
end