Решение на Първа задача от Димитър Стефанов

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

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

Резултати

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

Код

def convert_to_bgn(amount, currency)
case
when (currency==:usd)
(amount*1.7408).round(2)
when (currency==:eur)
(amount*1.9557).round(2)
when (currency==:psd)
(amount*2.6415).round(2)
else
amount.round(2)
end
end
def compare_prices(first_price, first_currency ,second_price ,second_currency)
first_comparison=convert_to_bgn(first_amount, first_currency)
second_comparison=convert_to_bgn(second_anount, second_currency)
first_comparison<=>second_comparison
end

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

..F..FFF

Failures:

  1) #convert_to_bgn converts gbp
     Failure/Error: expect(convert_to_bgn(1000, :gbp)).to eq 2641.5
       
       expected: 2641.5
            got: 1000.0
       
       (compared using ==)
     # /tmp/d20151012-23382-cykisi/spec.rb:11: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, :usd, 13, :usd)).to be < 0
     NameError:
       undefined local variable or method `first_amount' for #<RSpec::Core::ExampleGroup::Nested_2:0x007f86be21e7c8>
     # /tmp/d20151012-23382-cykisi/solution.rb:14:in `compare_prices'
     # /tmp/d20151012-23382-cykisi/spec.rb:25: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(5, :usd, 10, :bgn)).to be < 0
     NameError:
       undefined local variable or method `first_amount' for #<RSpec::Core::ExampleGroup::Nested_2:0x007f86be203a68>
     # /tmp/d20151012-23382-cykisi/solution.rb:14:in `compare_prices'
     # /tmp/d20151012-23382-cykisi/spec.rb:32: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(5, :usd, 10, :gbp)).to be < 0
     NameError:
       undefined local variable or method `first_amount' for #<RSpec::Core::ExampleGroup::Nested_2:0x007f86be201fd8>
     # /tmp/d20151012-23382-cykisi/solution.rb:14:in `compare_prices'
     # /tmp/d20151012-23382-cykisi/spec.rb:38: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.00401 seconds
8 examples, 4 failures

Failed examples:

rspec /tmp/d20151012-23382-cykisi/spec.rb:10 # #convert_to_bgn converts gbp
rspec /tmp/d20151012-23382-cykisi/spec.rb:24 # #compare_prices compares prices of the same currency
rspec /tmp/d20151012-23382-cykisi/spec.rb:31 # #compare_prices compares usd and bgn
rspec /tmp/d20151012-23382-cykisi/spec.rb:37 # #compare_prices compares eur and gbp

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

Димитър обнови решението на 12.10.2015 04:28 (преди около 9 години)

+def convert_to_bgn(amount, currency)
+ case
+ when (currency==:usd)
+ (amount*1.7408).round(2)
+ when (currency==:eur)
+ (amount*1.9557).round(2)
+ when (currency==:psd)
+ (amount*2.6415).round(2)
+ else
+ amount.round(2)
+ end
+end
+def compare_prices(first_price, first_currency ,second_price ,second_currency)
+ first_comparison=convert_to_bgn(first_amount, first_currency)
+ second_comparison=convert_to_bgn(second_anount, second_currency)
+ first_comparison<=>second_comparison
+end