Решение на Първа задача от Анджелин Неделчев

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

Към профила на Анджелин Неделчев

Резултати

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

Код

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

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

........

Finished in 0.00735 seconds
8 examples, 0 failures

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

Анджелин обнови решението на 11.10.2015 15:27 (преди над 8 години)

+def convert_to_bgn(amount, currency)
+ result = case currency
+ when :usd
+ amount * 1.7408
+ when :eur
+ amount * 1.9557
+ when :gbp
+ result = amount * 2.6415
+ end
+
+ result.round(2)
+end
+
+def compare_prices(first, f_currency, second, s_currency)
+ unless f_currency == :bgn
+ first = convert_to_bgn(first, f_currency)
+ end
+ unless s_currency == :bgn
+ second = convert_to_bgn(second, s_currency)
+ end
+
+ first <=> second
+end

Анджелин обнови решението на 11.10.2015 15:30 (преди над 8 години)

def convert_to_bgn(amount, currency)
result = case currency
when :usd
amount * 1.7408
when :eur
amount * 1.9557
when :gbp
- result = amount * 2.6415
+ amount * 2.6415
end
result.round(2)
end
def compare_prices(first, f_currency, second, s_currency)
unless f_currency == :bgn
first = convert_to_bgn(first, f_currency)
end
unless s_currency == :bgn
second = convert_to_bgn(second, s_currency)
end
first <=> second
end

Анджелин обнови решението на 11.10.2015 15:32 (преди над 8 години)

def convert_to_bgn(amount, currency)
- result = case currency
- when :usd
- amount * 1.7408
- when :eur
- amount * 1.9557
- when :gbp
- amount * 2.6415
- end
+ result = case currency
+ when :usd
+ amount * 1.7408
+ when :eur
+ amount * 1.9557
+ when :gbp
+ amount * 2.6415
+ end
- result.round(2)
+ result.round(2)
end
def compare_prices(first, f_currency, second, s_currency)
- unless f_currency == :bgn
- first = convert_to_bgn(first, f_currency)
- end
- unless s_currency == :bgn
- second = convert_to_bgn(second, s_currency)
- end
+ unless f_currency == :bgn
+ first = convert_to_bgn(first, f_currency)
+ end
+ unless s_currency == :bgn
+ second = convert_to_bgn(second, s_currency)
+ end
- first <=> second
+ first <=> second
end

Анджелин обнови решението на 11.10.2015 15:50 (преди над 8 години)

def convert_to_bgn(amount, currency)
result = case currency
when :usd
amount * 1.7408
when :eur
amount * 1.9557
when :gbp
amount * 2.6415
end
result.round(2)
end
def compare_prices(first, f_currency, second, s_currency)
- unless f_currency == :bgn
- first = convert_to_bgn(first, f_currency)
- end
- unless s_currency == :bgn
- second = convert_to_bgn(second, s_currency)
- end
+ first = convert_to_bgn(first, f_currency) unless f_currency == :bgn
+ second = convert_to_bgn(second, s_currency) unless s_currency == :bgn
first <=> second
end

Като цяло добре. Няколко идеи за подобрение

  • Бяха ли нужни проверките в compare_prices?
  • Опитай се да измислиш малко по-добри имена. Не прави съкращения като f_currency. Какво всъщност са first и second? Може би първа и втора цена. Избягвай result за име на променлива. Всичко е някакъв вид result. Замисли се какво всъщност представлява този резултат в контекста на решавания проблем (примерно цена в лева)
  • Можеш да използваш едноредовата версия на case (when x then y)

Анджелин обнови решението на 11.10.2015 21:41 (преди над 8 години)

def convert_to_bgn(amount, currency)
- result = case currency
- when :usd
- amount * 1.7408
- when :eur
- amount * 1.9557
- when :gbp
- amount * 2.6415
- end
+ bgn_amount = case currency
+ when :usd then amount * 1.7408
+ when :eur then amount * 1.9557
+ when :gbp then amount * 2.6415
+ else amount
+ end
- result.round(2)
+ bgn_amount.round(2)
end
-def compare_prices(first, f_currency, second, s_currency)
- first = convert_to_bgn(first, f_currency) unless f_currency == :bgn
- second = convert_to_bgn(second, s_currency) unless s_currency == :bgn
+def compare_prices(first_price, first_currency, second_price, second_currency)
+ first_price = convert_to_bgn(first_price, first_currency)
+ second_price = convert_to_bgn(second_price, second_currency)
- first <=> second
+ first_price <=> second_price
end