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

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

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

Резултати

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

Код

#!/usr/bin/env ruby
def convert_to_bgn(sum, currency)
currencies = { 'bgn' => 1, 'eur' => 1.9557, 'usd' => 1.7408, 'gbp' => 2.6415 }
result = sum * currencies[currency.id2name]
result = sprintf('%.2f', result)
end
def compare_prices(sum_1, currency_1, sum_2, currency_2)
convert_to_bgn(sum_1, currency_1) <=> convert_to_bgn(sum_2, currency_2)
end

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

FFFFF.FF

Failures:

  1) #convert_to_bgn converts usd
     Failure/Error: expect(convert_to_bgn(1000, :usd)).to eq 1740.8
       
       expected: 1740.8
            got: "1740.80"
       
       (compared using ==)
     # /tmp/d20151012-23382-nspcvw/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: "1955.70"
       
       (compared using ==)
     # /tmp/d20151012-23382-nspcvw/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: "2641.50"
       
       (compared using ==)
     # /tmp/d20151012-23382-nspcvw/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
       
       expected: 333
            got: "333.00"
       
       (compared using ==)
     # /tmp/d20151012-23382-nspcvw/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: "214.12"
       
       (compared using ==)
     # /tmp/d20151012-23382-nspcvw/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 usd and bgn
     Failure/Error: expect(compare_prices(5, :usd, 10, :bgn)).to be < 0
       expected: < 0
            got:   1
     # /tmp/d20151012-23382-nspcvw/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)>'

  7) #compare_prices compares eur and gbp
     Failure/Error: expect(compare_prices(5, :usd, 10, :gbp)).to be < 0
       expected: < 0
            got:   1
     # /tmp/d20151012-23382-nspcvw/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.008 seconds
8 examples, 7 failures

Failed examples:

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

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

Цветомир обнови решението на 12.10.2015 14:22 (преди около 9 години)

+#!/usr/bin/env ruby
+
+def convert_to_bgn(sum, currency)
+ currencies = { 'bgn' => 1, 'eur' => 1.9557, 'usd' => 1.7408, 'gbp' => 2.6415 }
+ result = sum * currencies[currency.id2name]
+ result = sprintf('%.2f', result)
+end
+
+def compare_prices(sum_1, currency_1, sum_2, currency_2)
+ convert_to_bgn(sum_1, currency_1) <=> convert_to_bgn(sum_2, currency_2)
+end
  • Този shebang в началото (#!/usb/bin/...) не трябва да го има. В случая не пречи, но е ненужен. Не предавай решения с това.
  • id2name изобщо не е нужно и реално този метод не се ползва. Когато искаш да обърнеш символ към низ, се прави така: :baba.to_s # => # "baba".
  • Не е нужно ключовете ти в речника да са низове; символите са много подходящи за ключове в речник.
  • Връщаш низ, а не число. sprintf връща низ. Трябва да върнеш закръглено число.
  • Променливата result на 6-ти ред е излишна, а и е лошо именувана.