Решение на Първа задача от Емилия Банчева

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

Към профила на Емилия Банчева

Резултати

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

Код

def convert_to_bgn(price, currency)
case currency
when :usd
(price = price * 1.7408).round(2)
when :eur
(price = price * 1.9557).round(2)
else
(price = price * 2.6415).round(2)
end
end
def compare_prices(price_one, currency_one, price_two, currency_two)
price_in_bgn_one = convert_to_bgn(price_one, currency_one)
price_in_bgn_two = convert_to_bgn(price_two, currency_two)
if(price_in_bgn_one > price_in_bgn_two)
1
elsif(price_in_bgn_one == price_in_bgn_two)
0
else
-1 end
end

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

...F..F.

Failures:

  1) #convert_to_bgn converts bgn
     Failure/Error: expect(convert_to_bgn(333, :bgn)).to eq 333
       
       expected: 333
            got: 879.62
       
       (compared using ==)
     # /tmp/d20151012-23382-d34359/spec.rb:15:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) #compare_prices compares usd and bgn
     Failure/Error: expect(compare_prices(1000, :usd, 1740.8, :bgn)).to eq 0
       
       expected: 0
            got: -1
       
       (compared using ==)
     # /tmp/d20151012-23382-d34359/spec.rb:33:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.0113 seconds
8 examples, 2 failures

Failed examples:

rspec /tmp/d20151012-23382-d34359/spec.rb:14 # #convert_to_bgn converts bgn
rspec /tmp/d20151012-23382-d34359/spec.rb:31 # #compare_prices compares usd and bgn

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

Емилия обнови решението на 10.10.2015 23:33 (преди над 8 години)

+def convert_to_bgn(price, currency)
+ case currency
+ when :usd
+ (new_price = price * 1.7408).round(2)
+ when :eur
+ (new_price = price * 1.9557).round(2)
+ else
+ (new_price = price * 2.6415).round(2)
+ end
+end
+
+def compare_prices(price_one, currency_one, price_two, currency_two)
+ price_in_bgn_one = convert_to_bgn(price_one, currency_one)
+ price_in_bgn_two = convert_to_bgn(price_two, currency_two)
+ if(price_in_bgn_one > price_in_bgn_two)
+ 1 end
+ if(price_in_bgn_one == price_in_bgn_two)
+ 0 end
+ if(price_in_bgn_one < price_in_bgn_two)
+ -1 end
+end

Здравей :)

Имам някои коментари:

  • По принцип използвай elsif и else вместо повтарящи се if-ове. Така няма да ти се налага да слагаш end навсякъде. :)
  • Нямаш нужда от проверката в compare_prices. Прочети за оператора <=>. :)
  • Защо ти е new_price? Присвояваш стойност на променлива, която не използваш никога.
  • Сигурна ли си, че решението ти ще работи с всички валути?
  • В Ruby всичко е израз и връща стойност - дори case. Тоест можеш да присвоиш резултата от case на променлива и после да го използваш. Можеш ли да използваш това знание, за да премахнеш малко дублиране на код? :)

Напомням, че до крайния срок остават цели 23 часа и половина, и можеш да ни изпратиш ново решение :)

Емилия обнови решението на 11.10.2015 22:58 (преди над 8 години)

def convert_to_bgn(price, currency)
case currency
when :usd
- (new_price = price * 1.7408).round(2)
+ (price = price * 1.7408).round(2)
when :eur
- (new_price = price * 1.9557).round(2)
+ (price = price * 1.9557).round(2)
else
- (new_price = price * 2.6415).round(2)
+ (price = price * 2.6415).round(2)
end
end
def compare_prices(price_one, currency_one, price_two, currency_two)
price_in_bgn_one = convert_to_bgn(price_one, currency_one)
price_in_bgn_two = convert_to_bgn(price_two, currency_two)
if(price_in_bgn_one > price_in_bgn_two)
- 1 end
- if(price_in_bgn_one == price_in_bgn_two)
- 0 end
- if(price_in_bgn_one < price_in_bgn_two)
+ 1
+ elsif(price_in_bgn_one == price_in_bgn_two)
+ 0
+ else
-1 end
end