Решение на Първа задача от Виктор Радивчев

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

Към профила на Виктор Радивчев

Резултати

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

Код

def round_to_second_digit(price)
price = price * 100
remainder = price % 1
if remainder >= 0.5
round_price = price.ceil / 100.0
else
round_price = price.floor / 100.0
end
round_price
end
def convert_to_bgn(price, currency)
if currency == :usd
round_to_second_digit(price * 1.7408)
elsif currency == :eur
round_to_second_digit(price * 1.9557)
elsif currency == :gbp
round_to_second_digit(price * 2.6415)
else
price
end
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.02784 seconds
8 examples, 0 failures

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

Виктор обнови решението на 11.10.2015 19:58 (преди над 8 години)

+def rounds_to_second_sign_after_comma(value)
+ value=value*100
+ remainder=value%1
+ if remainder>=0.5
+ round_value=value.ceil/100.0
+ else
+ round_value=value.floor/100.0
+ end
+ round_value
+end
+def convert_to_bgn(value, currency)
+ if currency==:usd
+ rounds_to_second_sign_after_comma(value*1.7408)
+ elsif currency==:eur
+ rounds_to_second_sign_after_comma(value*1.9557)
+ elsif currency==:gbp
+ rounds_to_second_sign_after_comma(value*2.6415)
+ else
+ value
+ end
+end
+def compare_prices(first_value, first_currency, second_value, second_currency)
+ first_price=convert_to_bgn(first_value, first_currency)
+ second_price=convert_to_bgn(second_value, second_currency)
+ first_price-second_price
+end
+puts compare_prices(100, :usd, 100, :bgn)

Виктор обнови решението на 11.10.2015 20:00 (преди над 8 години)

def rounds_to_second_sign_after_comma(value)
value=value*100
remainder=value%1
if remainder>=0.5
round_value=value.ceil/100.0
else
round_value=value.floor/100.0
end
round_value
end
def convert_to_bgn(value, currency)
if currency==:usd
rounds_to_second_sign_after_comma(value*1.7408)
elsif currency==:eur
rounds_to_second_sign_after_comma(value*1.9557)
elsif currency==:gbp
rounds_to_second_sign_after_comma(value*2.6415)
else
value
end
end
def compare_prices(first_value, first_currency, second_value, second_currency)
first_price=convert_to_bgn(first_value, first_currency)
second_price=convert_to_bgn(second_value, second_currency)
first_price-second_price
-end
-puts compare_prices(100, :usd, 100, :bgn)
+end
  • Не е нужно да преоткриваш колелото :) Погледни Float#round
  • Имаш малко violation-и на style guide-a що се отнася до whitespace-и. Слагай space-ове около оператори (x=y vs x = y). Разделяй методите с нов ред
  • value по принцип не е много добро име за променлива. Всичко е някакъв вид value. Каква всъщност е разликата между first_value и first_price? Че едното е value, а другото price? Или и двете са price, но второто е гарантирано да е в лева. Опитай се името да отразява това

Бележки:

  • Слагай интервали около операторите (=, /, *, >=, == и т.н.) - пише го в ръководството по стил.
  • value е твърде общо име на променлива; реално всичко е "стойност". Може би по-добро име би било amount или price.
  • Супер е, че не се притесняваш да слагаш дълги имена на методите си (напр. rounds_to_second_sign_after_comma). Така определено е по-ясно. Единствената ми бележка тук е, че "sign" означава знак. Може би си искал да използваш "digit", т.е. rounds_to_second_digit_after_comma. Аз бих изпуснал и s след round, т.е. round_to_second_digit (after_comma се подразбира).
  • Пак по темата със закръглянето, потърси ли дали няма вграден метод за това? В Ruby има доста вградени неща и преди да си напишем наше, е добре да потърсим добре дали вече го няма написано и вградено я в езика, я в стандартната библиотека.
  • Сравнението на цени е хитро :) Идеята е да използвате оператора <=>, но и с изваждане става. Ще го кажем по-натам.

Като цяло, решението ти е прилично.

Виктор обнови решението на 11.10.2015 23:38 (преди над 8 години)

-def rounds_to_second_sign_after_comma(value)
- value=value*100
- remainder=value%1
- if remainder>=0.5
- round_value=value.ceil/100.0
+def round_to_second_digit(price)
+ price = price * 100
+ remainder = price % 1
+ if remainder >= 0.5
+ round_price = price.ceil / 100.0
else
- round_value=value.floor/100.0
+ round_price = price.floor / 100.0
end
- round_value
+ round_price
end
-def convert_to_bgn(value, currency)
- if currency==:usd
- rounds_to_second_sign_after_comma(value*1.7408)
- elsif currency==:eur
- rounds_to_second_sign_after_comma(value*1.9557)
- elsif currency==:gbp
- rounds_to_second_sign_after_comma(value*2.6415)
+def convert_to_bgn(price, currency)
+ if currency == :usd
+ round_to_second_digit(price * 1.7408)
+ elsif currency == :eur
+ round_to_second_digit(price * 1.9557)
+ elsif currency == :gbp
+ round_to_second_digit(price * 2.6415)
else
- value
+ price
end
end
-def compare_prices(first_value, first_currency, second_value, second_currency)
- first_price=convert_to_bgn(first_value, first_currency)
- second_price=convert_to_bgn(second_value, second_currency)
- first_price-second_price
+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