Решение на Първа задача от Пламен Никифоров

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

Към профила на Пламен Никифоров

Резултати

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

Код

def convert_to_bgn(amount, currency)
currency_hash = {usd: 1.7408, eur: 1.9557, gbp: 2.6415, bgn: 1}
(currency_hash[currency]*amount).round(2)
end
def compare_prices(first_price, first_currency, second_price, second_currency)
first_price_in_leva = convert_to_bgn(first_price,first_currency)
second_price_in_leva = convert_to_bgn(second_price,second_currency)
first_price_in_leva <=> second_price_in_leva
end

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

........

Finished in 0.00638 seconds
8 examples, 0 failures

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

Пламен обнови решението на 08.10.2015 22:03 (преди около 9 години)

+def convert_to_bgn(amount, currency)
+ hash = {:usd => 1.7408, :eur => 1.9557, :gbp => 2.6415, :bgn => 1}
+ (hash[currency]*amount).round(2)
+end
+
+def compare_prices(first_price, first_currency, second_price, second_currency)
+ first_price_in_leva = convert_to_bgn(first_price,first_currency)
+ second_price_in_leva = convert_to_bgn(second_price,second_currency)
+ first_price_in_leva <=> second_price_in_leva
+end

Като цяло добре, но прочети style guide-а:

  • Индентирай с два space-a
  • Слагай space-ове между оператори (x*y vs x * y)
  • Опитай се да измислиш по-добро име от hash. Какво всъщност значи тази стойност в контекста на проблема (а не имплементационно)?
  • Има опростен синтаксис за hash-ове, когато ключовете са символи ({foo: 1, bar: 2} vs {:foo => 1, :bar => 2})

Пламен обнови решението на 08.10.2015 22:11 (преди около 9 години)

def convert_to_bgn(amount, currency)
- hash = {:usd => 1.7408, :eur => 1.9557, :gbp => 2.6415, :bgn => 1}
+ hash = {:usd => 1.7408, :eur => 1.9557, :gbp => 2.6415, :bgn => 1}
(hash[currency]*amount).round(2)
end
def compare_prices(first_price, first_currency, second_price, second_currency)
- first_price_in_leva = convert_to_bgn(first_price,first_currency)
- second_price_in_leva = convert_to_bgn(second_price,second_currency)
- first_price_in_leva <=> second_price_in_leva
+ first_price_in_leva = convert_to_bgn(first_price,first_currency)
+ second_price_in_leva = convert_to_bgn(second_price,second_currency)
+ first_price_in_leva <=> second_price_in_leva
end

Пламен обнови решението на 08.10.2015 22:11 (преди около 9 години)

def convert_to_bgn(amount, currency)
hash = {:usd => 1.7408, :eur => 1.9557, :gbp => 2.6415, :bgn => 1}
- (hash[currency]*amount).round(2)
+ (hash[currency]*amount).round(2)
end
def compare_prices(first_price, first_currency, second_price, second_currency)
first_price_in_leva = convert_to_bgn(first_price,first_currency)
second_price_in_leva = convert_to_bgn(second_price,second_currency)
first_price_in_leva <=> second_price_in_leva
end

Пламен обнови решението на 08.10.2015 22:12 (преди около 9 години)

def convert_to_bgn(amount, currency)
- hash = {:usd => 1.7408, :eur => 1.9557, :gbp => 2.6415, :bgn => 1}
- (hash[currency]*amount).round(2)
+ currency_hash = {:usd => 1.7408, :eur => 1.9557, :gbp => 2.6415, :bgn => 1}
+ (hash[currency] * amount).round(2)
end
def compare_prices(first_price, first_currency, second_price, second_currency)
first_price_in_leva = convert_to_bgn(first_price,first_currency)
second_price_in_leva = convert_to_bgn(second_price,second_currency)
first_price_in_leva <=> second_price_in_leva
end

Пламен обнови решението на 09.10.2015 18:55 (преди около 9 години)

def convert_to_bgn(amount, currency)
- currency_hash = {:usd => 1.7408, :eur => 1.9557, :gbp => 2.6415, :bgn => 1}
- (hash[currency] * amount).round(2)
+ currency_hash = {usd: 1.7408, eur: 1.9557, gbp: 2.6415, bgn: 1}
+ (currency_hash[currency]*amount).round(2)
end
def compare_prices(first_price, first_currency, second_price, second_currency)
first_price_in_leva = convert_to_bgn(first_price,first_currency)
second_price_in_leva = convert_to_bgn(second_price,second_currency)
first_price_in_leva <=> second_price_in_leva
end