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

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

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

Резултати

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

Код

def convert_to_bgn(amount, currency)
currencies = {:usd => 1.7408, :eur => 1.9557, :gbp => 2.6415}
(amount * currencies[currency]).round(2)
end
def compare_prices(price_one, currency_one, price_two, currency_two)
price_one_bgn = convert_to_bgn(price_one, currency_one)
price_two_bgn = convert_to_bgn(price_two, currency_two)
price_one_bgn <=> price_two_bgn
end

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

...F.FF.

Failures:

  1) #convert_to_bgn converts bgn
     Failure/Error: expect(convert_to_bgn(333, :bgn)).to eq 333
     TypeError:
       nil can't be coerced into Fixnum
     # /tmp/d20151012-23382-1cd2qvd/solution.rb:3:in `*'
     # /tmp/d20151012-23382-1cd2qvd/solution.rb:3:in `convert_to_bgn'
     # /tmp/d20151012-23382-1cd2qvd/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 prices of the same currency
     Failure/Error: expect(compare_prices(52, :bgn, 4, :bgn)).to be > 0
     TypeError:
       nil can't be coerced into Fixnum
     # /tmp/d20151012-23382-1cd2qvd/solution.rb:3:in `*'
     # /tmp/d20151012-23382-1cd2qvd/solution.rb:3:in `convert_to_bgn'
     # /tmp/d20151012-23382-1cd2qvd/solution.rb:7:in `compare_prices'
     # /tmp/d20151012-23382-1cd2qvd/spec.rb:28: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
     TypeError:
       nil can't be coerced into Fixnum
     # /tmp/d20151012-23382-1cd2qvd/solution.rb:3:in `*'
     # /tmp/d20151012-23382-1cd2qvd/solution.rb:3:in `convert_to_bgn'
     # /tmp/d20151012-23382-1cd2qvd/solution.rb:8:in `compare_prices'
     # /tmp/d20151012-23382-1cd2qvd/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)>'

Finished in 0.00651 seconds
8 examples, 3 failures

Failed examples:

rspec /tmp/d20151012-23382-1cd2qvd/spec.rb:14 # #convert_to_bgn converts bgn
rspec /tmp/d20151012-23382-1cd2qvd/spec.rb:24 # #compare_prices compares prices of the same currency
rspec /tmp/d20151012-23382-1cd2qvd/spec.rb:31 # #compare_prices compares usd and bgn

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

Иван обнови решението на 11.10.2015 13:38 (преди над 8 години)

+def convert_to_bgn(amount, currency)
+ if currency.equal? :usd
+ (amount * 1.7408).round(2)
+ elsif currency.equal? :eur
+ (amount * 1.9557).round(2)
+ elsif currency.equal? :gbp
+ (amount * 2.6415).round(2)
+ else
+ amount.round(2)
+ end
+end
+
+
+def compare_prices(price_one, currency_one, price_two, currency_two)
+ price_one_bgn = convert_to_bgn(price_one, currency_one)
+ price_two_bgn = convert_to_bgn(price_two, currency_two)
+
+ if price_one_bgn > price_two_bgn
+ 1
+ elsif price_one_bgn < price_two_bgn
+ -1
+ else
+ 0
+ end
+end

Като гледам би трябвало да работи, но нека оправим няколко неща:

  • Това, което правиш с equal?, не е добре. equal? сравнява дали два обекта са еднакви и това, че символите са интернирани в Ruby и кодът ти работи е само случайност. Използвай нормално сравнение ==.
foo = 'baz'
bar = 'baz'

foo.object_id   # => 70282459129120
bar.object_id   # => 70282459099480
foo.equal?(bar) # => false

foo = :baz
bar = :baz

foo.object_id   # => 1087708
bar.object_id   # => 1087708
foo.equal?(bar) # => true
  • В convert_to_bgn всичко се повтаря, ако изкараш обменния курс в променлива, ще можеш да напишеш израза на един ред.
  • След като го направиш погледни за Hash-ове в Ruby и помисли дали няма начин да махнеш и if-а
  • В compare_prices си си написал <=>, та.. погледни за този метод в Ruby

Иван обнови решението на 11.10.2015 21:49 (преди над 8 години)

def convert_to_bgn(amount, currency)
- if currency.equal? :usd
- (amount * 1.7408).round(2)
- elsif currency.equal? :eur
- (amount * 1.9557).round(2)
- elsif currency.equal? :gbp
- (amount * 2.6415).round(2)
- else
- amount.round(2)
- end
+ currencies = {:usd => 1.7408, :eur => 1.9557, :gbp => 2.6415}
+ (amount * currencies[currency]).round(2)
end
-
def compare_prices(price_one, currency_one, price_two, currency_two)
price_one_bgn = convert_to_bgn(price_one, currency_one)
price_two_bgn = convert_to_bgn(price_two, currency_two)
- if price_one_bgn > price_two_bgn
- 1
- elsif price_one_bgn < price_two_bgn
- -1
- else
- 0
- end
+ price_one_bgn <=> price_two_bgn
end