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

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

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

Резултати

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

Код

def convert_to_bgn(amount, currency)
bgn_value = case currency
when :usd then amount * 1.7408
when :eur then amount * 1.9557
else amount * 2.6415
end
bgn_value.round(2)
end
def compare_prices (amount_first, currency_first,
amount_second, currency_second)
convert_to_bgn(amount_first, currency_first) <=>
convert_to_bgn(amount_second, currency_second)
end

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

...F..F.

Failures:

  1) #convert_to_bgn converts bgn
     Failure/Error: expect(convert_to_bgn(333, :bgn)).to eq 333
       
       expected: 333
            got: 879.62
       
       (compared using ==)
     # /tmp/d20151012-23382-uthuzc/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 usd and bgn
     Failure/Error: expect(compare_prices(1000, :usd, 1740.8, :bgn)).to eq 0
       
       expected: 0
            got: -1
       
       (compared using ==)
     # /tmp/d20151012-23382-uthuzc/spec.rb:33: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.00721 seconds
8 examples, 2 failures

Failed examples:

rspec /tmp/d20151012-23382-uthuzc/spec.rb:14 # #convert_to_bgn converts bgn
rspec /tmp/d20151012-23382-uthuzc/spec.rb:31 # #compare_prices compares usd and bgn

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

Димитър обнови решението на 11.10.2015 16:39 (преди над 8 години)

+def convert_to_bgn(amount, currency)
+ if currency == :usd
+ result = amount * 1.7408
+ elsif currency == :eur
+ result = amount * 1.9557
+ else
+ result = amount * 2.6415
+ end
+ result.to_s(2)
+end
+
+def compare_prices (amount_f, currency_f, amount_s, currency_s)
+ convert_to_bgn(amount_f, currency_f) <=> convert_to_bgn(amount_s, currency_s)
+end
  • Опитай да пуснеш примерните тестове. Ще имаш малка изненада с това to_s(2)
  • Може да искаш да погледнеш case ("switch"-ът в Ruby)
  • Измисли мако по-добри имена. result обикновено не значи нищо. Всичко е някакъв вид резултат. Не прави съкращения като amount_f.

Димитър обнови решението на 11.10.2015 23:44 (преди над 8 години)

def convert_to_bgn(amount, currency)
- if currency == :usd
- result = amount * 1.7408
- elsif currency == :eur
- result = amount * 1.9557
- else
- result = amount * 2.6415
- end
- result.to_s(2)
+bgn_value = case currency
+ when :usd then amount * 1.7408
+ when :eur then amount * 1.9557
+ else amount * 2.6415
+ end
+ bgn_value.round(2)
end
-def compare_prices (amount_f, currency_f, amount_s, currency_s)
- convert_to_bgn(amount_f, currency_f) <=> convert_to_bgn(amount_s, currency_s)
+def compare_prices (amount_first, currency_first,
+ amount_second, currency_second)
+ convert_to_bgn(amount_first, currency_first) <=>
+ convert_to_bgn(amount_second, currency_second)
end

Димитър обнови решението на 11.10.2015 23:51 (преди над 8 години)

def convert_to_bgn(amount, currency)
bgn_value = case currency
when :usd then amount * 1.7408
when :eur then amount * 1.9557
else amount * 2.6415
- end
+ end
bgn_value.round(2)
end
def compare_prices (amount_first, currency_first,
amount_second, currency_second)
convert_to_bgn(amount_first, currency_first) <=>
convert_to_bgn(amount_second, currency_second)
end