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

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

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

Резултати

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

Код

def convert_to_bgn (price, currency)
if currency == :usd
price*1.7408
elsif currency == :eur
price*1.9557
elsif currency == :gbp
price*2.6415
else
price
end
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
if first_price > first_price
1
elsif second_price > first_price
-1
else
0
end
end

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

....FFFF

Failures:

  1) #convert_to_bgn rounds to 2 digits after the point
     Failure/Error: expect(convert_to_bgn(123, :usd)).to eq 214.12
       
       expected: 214.12
            got: 214.11839999999998
       
       (compared using ==)
     # /tmp/d20151012-23382-c6io2v/spec.rb:19: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 prices of the same currency
     Failure/Error: expect(compare_prices(10, :gbp, 8, :gbp)).to be > 0
       expected: > 0
            got:   0
     # /tmp/d20151012-23382-c6io2v/spec.rb:27: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)>'

  3) #compare_prices compares usd and bgn
     Failure/Error: expect(compare_prices(100, :usd, 10, :bgn)).to be > 0
       expected: > 0
            got:   0
     # /tmp/d20151012-23382-c6io2v/spec.rb:34: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)>'

  4) #compare_prices compares eur and gbp
     Failure/Error: expect(compare_prices(10, :usd, 2, :gbp)).to be > 0
       expected: > 0
            got:   0
     # /tmp/d20151012-23382-c6io2v/spec.rb:39: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.00652 seconds
8 examples, 4 failures

Failed examples:

rspec /tmp/d20151012-23382-c6io2v/spec.rb:18 # #convert_to_bgn rounds to 2 digits after the point
rspec /tmp/d20151012-23382-c6io2v/spec.rb:24 # #compare_prices compares prices of the same currency
rspec /tmp/d20151012-23382-c6io2v/spec.rb:31 # #compare_prices compares usd and bgn
rspec /tmp/d20151012-23382-c6io2v/spec.rb:37 # #compare_prices compares eur and gbp

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

Милена обнови решението на 12.10.2015 00:27 (преди над 8 години)

+def convert_to_bgn (price, currency)
+ if currency == :usd
+ price*1.7408
+ elsif currency == :eur
+ price*1.9557
+ elsif currency == :gbp
+ price*2.6415
+ else
+ price
+ end
+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
+ if first_price > first_price
+ 1
+ elsif second_price > first_price
+ -1
+ else
+ 0
+ end
+end

Какъв ли е шансът да си пред компютъра :)

  • индентирай с две шпации
  • трябва да закръгляш резултата от convert_to_bgn
  • махни спайсовете между името на методите и параметрите def convert_to_bgn(price, currency)

Ако имаш време:

  • в compare_prices си си написала <=>, погледни какво е
  • в convert_to_bgn може да стане по-кратко с hash-ове