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

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

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

Резултати

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

Код

def series(series_calculate , number)
fibonacci = (1..number).inject([0 , 1]) { |(a , b) , _| [b , a + b]}[0]
lucas = (1..number).inject([2 , 1]) { |(a , b) , _| [b , a + b] }[0]
if series_calculate == "fibonacci" then fibonacci
elsif series_calculate == "lucas" then lucas
elsif series_calculate == "sum" then fibonacci + lucas
end
end

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

....FFFFFFFF

Failures:

  1) series handles lucas series for base cases
     Failure/Error: series('lucas', 1).should eq 2
       
       expected: 2
            got: 1
       
       (compared using ==)
     # /tmp/d20141023-2426-qzpubh/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)>'

  2) series handles lucas series for odd numbers
     Failure/Error: series('lucas', 7).should eq 18
       
       expected: 18
            got: 29
       
       (compared using ==)
     # /tmp/d20141023-2426-qzpubh/spec.rb:30: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) series handles lucas series for even numbers
     Failure/Error: series('lucas', 8).should eq 29
       
       expected: 29
            got: 47
       
       (compared using ==)
     # /tmp/d20141023-2426-qzpubh/spec.rb:35: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) series handles lucas series for bigger numbers
     Failure/Error: series('lucas', 15).should eq 843
       
       expected: 843
            got: 1364
       
       (compared using ==)
     # /tmp/d20141023-2426-qzpubh/spec.rb:40: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) series handles summed series for base cases
     Failure/Error: series('summed', 1).should eq 3
       
       expected: 3
            got: nil
       
       (compared using ==)
     # /tmp/d20141023-2426-qzpubh/spec.rb:45: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) series handles summed series for odd numbers
     Failure/Error: series('summed', 7).should eq 31
       
       expected: 31
            got: nil
       
       (compared using ==)
     # /tmp/d20141023-2426-qzpubh/spec.rb:50: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) series handles summed series for even numbers
     Failure/Error: series('summed', 8).should eq 50
       
       expected: 50
            got: nil
       
       (compared using ==)
     # /tmp/d20141023-2426-qzpubh/spec.rb:55: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) series handles summed series for bigger numbers
     Failure/Error: series('summed', 15).should eq 1453
       
       expected: 1453
            got: nil
       
       (compared using ==)
     # /tmp/d20141023-2426-qzpubh/spec.rb:60: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.01421 seconds
12 examples, 8 failures

Failed examples:

rspec /tmp/d20141023-2426-qzpubh/spec.rb:24 # series handles lucas series for base cases
rspec /tmp/d20141023-2426-qzpubh/spec.rb:29 # series handles lucas series for odd numbers
rspec /tmp/d20141023-2426-qzpubh/spec.rb:34 # series handles lucas series for even numbers
rspec /tmp/d20141023-2426-qzpubh/spec.rb:39 # series handles lucas series for bigger numbers
rspec /tmp/d20141023-2426-qzpubh/spec.rb:44 # series handles summed series for base cases
rspec /tmp/d20141023-2426-qzpubh/spec.rb:49 # series handles summed series for odd numbers
rspec /tmp/d20141023-2426-qzpubh/spec.rb:54 # series handles summed series for even numbers
rspec /tmp/d20141023-2426-qzpubh/spec.rb:59 # series handles summed series for bigger numbers

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

Александър обнови решението на 15.10.2014 14:00 (преди около 10 години)

+def series(series_calculate , number)
+ fibonacci = (1..number).inject([0 , 1]) { |(a , b) , _| [b , a + b]}[0]
+ lucas = (1..number).inject([2 , 1]) { |(a , b) , _| [b , a + b] }[0]
+ if series_calculate == "fibonacci" then fibonacci
+ elsif series_calculate == "lucas" then lucas
+ elsif series_calculate == "sum" then fibonacci + lucas
+ end
+end

Супер е че си видял inject и си реализирал бърз алгоритъм (въпреки че няма да оценяваме сложността на кода), но функцията series прави твърде много неща и има повторение на код в себе си.

Ще стане доста по-добре, ако отделиш една функция само с логиката , която ти пресмята рекурентната редица. На тази функция можеш да подаваш началните стойности от редицата. А в series просто ще я викаш, както правиш сега от ред 4 надолу. Също може да замениш тaзи if-elsif конструкция с един case (това е switch statement в руби)

Имаш и нещо неконсистентно в кода. За Фибоначи започваш да смяташ от нулевия и първия елемент, а за Лукас от първия и втория.

P.S Винаги си пускай примерните тестове, преди да предаваш !