Решение на Първа задача от Александър Сариков

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

Към профила на Александър Сариков

Резултати

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

Код

def convert_to_bgn(price, currency)
if currency == :usd
bgn = price * 1.7408
elsif currency == :eur
bgn = price * 1.9557
elsif currency == :gbp
bgn = price * 2.6415
end
puts bgn.round(2)
end
def compare_prices(price_1, currency_1, price_2, currency_2)
def convert_bgn(price, currency)
if currency == :usd
bgn = price * 1.7408
elsif currency == :eur
bgn = price * 1.9557
elsif currency == :gbp
bgn = price * 2.6415
else
bgn = price
end
end
puts convert_bgn(price_1, currency_1) - convert_bgn(price_2, currency_2)
end

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

1740.8
F1955.7
F2641.5
FF214.12
F-5.2224
F-1.2960000000000012
F-17.711000000000006
F

Failures:

  1) #convert_to_bgn converts usd
     Failure/Error: expect(convert_to_bgn(1000, :usd)).to eq 1740.8
       
       expected: 1740.8
            got: nil
       
       (compared using ==)
     # /tmp/d20151012-23382-1atrb6y/spec.rb:3: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) #convert_to_bgn converts eur
     Failure/Error: expect(convert_to_bgn(1000, :eur)).to eq 1955.7
       
       expected: 1955.7
            got: nil
       
       (compared using ==)
     # /tmp/d20151012-23382-1atrb6y/spec.rb:7: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) #convert_to_bgn converts gbp
     Failure/Error: expect(convert_to_bgn(1000, :gbp)).to eq 2641.5
       
       expected: 2641.5
            got: nil
       
       (compared using ==)
     # /tmp/d20151012-23382-1atrb6y/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)>'

  4) #convert_to_bgn converts bgn
     Failure/Error: expect(convert_to_bgn(333, :bgn)).to eq 333
     NoMethodError:
       undefined method `round' for nil:NilClass
     # /tmp/d20151012-23382-1atrb6y/solution.rb:9:in `convert_to_bgn'
     # /tmp/d20151012-23382-1atrb6y/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)>'

  5) #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: nil
       
       (compared using ==)
     # /tmp/d20151012-23382-1atrb6y/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)>'

  6) #compare_prices compares prices of the same currency
     Failure/Error: expect(compare_prices(10, :usd, 13, :usd)).to be < 0
     NoMethodError:
       undefined method `<' for nil:NilClass
     # /tmp/d20151012-23382-1atrb6y/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)>'

  7) #compare_prices compares usd and bgn
     Failure/Error: expect(compare_prices(5, :usd, 10, :bgn)).to be < 0
     NoMethodError:
       undefined method `<' for nil:NilClass
     # /tmp/d20151012-23382-1atrb6y/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)>'

  8) #compare_prices compares eur and gbp
     Failure/Error: expect(compare_prices(5, :usd, 10, :gbp)).to be < 0
     NoMethodError:
       undefined method `<' for nil:NilClass
     # /tmp/d20151012-23382-1atrb6y/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.00699 seconds
8 examples, 8 failures

Failed examples:

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

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

Александър обнови решението на 12.10.2015 13:57 (преди около 9 години)

+def convert_to_bgn(price, currency)
+ if currency == :usd
+ bgn = price * 1.7408
+ elsif currency == :eur
+ bgn = price * 1.9557
+ elsif currency == :gbp
+ bgn = price * 2.6415
+ end
+ puts bgn.round(2)
+end
+
+def compare_prices(price_1, currency_1, price_2, currency_2)
+ def convert_bgn(price, currency)
+ if currency == :usd
+ bgn = price * 1.7408
+ elsif currency == :eur
+ bgn = price * 1.9557
+ elsif currency == :gbp
+ bgn = price * 2.6415
+ end
+ end
+ puts convert_bgn(price_1, currency_1) - convert_bgn(price_2, currency_2)
+end

Александър обнови решението на 12.10.2015 13:58 (преди около 9 години)

def convert_to_bgn(price, currency)
if currency == :usd
bgn = price * 1.7408
elsif currency == :eur
bgn = price * 1.9557
elsif currency == :gbp
bgn = price * 2.6415
end
puts bgn.round(2)
end
def compare_prices(price_1, currency_1, price_2, currency_2)
def convert_bgn(price, currency)
if currency == :usd
bgn = price * 1.7408
elsif currency == :eur
bgn = price * 1.9557
elsif currency == :gbp
bgn = price * 2.6415
+ else
+ bgn = price
end
end
puts convert_bgn(price_1, currency_1) - convert_bgn(price_2, currency_2)
end

Това не би трябвало да минава тестовете.. Не трябва да принтираш на екрана, а да връщаш резултат от методите.

  • Pешението ти е счупено.
  • Това, което си направил с метод в метод, не го прави.

Ако си някъде около компютър оправи тези неща и когато излезнат решенията погледни другите как са се справили.