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

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

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

Резултати

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

Код

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

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

........

Finished in 0.00746 seconds
8 examples, 0 failures

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

Даниела обнови решението на 09.10.2015 11:49 (преди над 8 години)

+def convert_to_bgn(value, currency)
+ e = case currency
+ when :usd
+ 1.7408
+ when :eur
+ 1.9557
+ when :gbp
+ 2.6415
+ when :bgn
+ 1
+ end
+ (value * e).round(2)
+end
+
+def compare_prices(value_1, currency_1, value_2, currency_2)
+ x = convert_to_bgn(value_1, currency_1)
+ y = convert_to_bgn(value_2, currency_2)
+ x <=> y
+end
  • Можеш да използваш едноредовата версия на case - when x then y

Опитай се да измислиш малко по-добри имена

  • Еднобуквени имена като x, y, e etc рядко са добра идея (освен в математически/физични формули)
  • value почти винаги не значи нищо. Всяка променлива правена някога е някакъв вид value
  • Предпочитай да използваш думи, цифрите са малко криптични (first_currency vs currency_1)

За да добиеш представа как да подобриш имената си, просто си задай въпроса "Какво значи тази стойност в контекста на решавания проблем?". Например тук:

  • value всъщност е цена
  • e - обменен курс
  • x - първата цена в лева

Даниела обнови решението на 10.10.2015 11:19 (преди над 8 години)

-def convert_to_bgn(value, currency)
- e = case currency
- when :usd
- 1.7408
- when :eur
- 1.9557
- when :gbp
- 2.6415
- when :bgn
- 1
- end
- (value * e).round(2)
+def convert_to_bgn(price, currency)
+ exchange_rate = case currency
+ when :usd then 1.7408
+ when :eur then 1.9557
+ when :gbp then 2.6415
+ when :bgn then 1
+ end
+ (price * exchange_rate).round(2)
end
-def compare_prices(value_1, currency_1, value_2, currency_2)
- x = convert_to_bgn(value_1, currency_1)
- y = convert_to_bgn(value_2, currency_2)
- x <=> y
+def compare_prices(first_price, first_currency, second_price, second_currency)
+ first_price_bgn = convert_to_bgn(first_price, first_currency)
+ second_price_bgn = convert_to_bgn(second_price, second_currency)
+ first_price_bgn <=> second_price_bgn
end