Решение на Първа задача от Радослав Георгиев

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

Към профила на Радослав Георгиев

Резултати

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

Код

def sum_two_series(first, second, index)
for i in 1 .. index - 1
first += second
first, second = second, first
end
first
end
def series(name, index)
if name == 'fibonacci' then sumtwo_series(1, 1, index) end
if name == 'lucas' then sumtwo_series(2, 1, index) end
if name=='summed' then sumtwo_series(1, 1, index)+sumtwo_series(2, 1,index) end
end

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

FFFFFFFFFFFF

Failures:

  1) series handles fibonacci series for base cases
     Failure/Error: series('fibonacci', 1).should eq 1
     NoMethodError:
       undefined method `sumtwo_series' for #<RSpec::Core::ExampleGroup::Nested_1:0xb8fb2418>
     # /tmp/d20141023-2426-fdd8ny/solution.rb:9:in `series'
     # /tmp/d20141023-2426-fdd8ny/spec.rb:5: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 fibonacci series for odd numbers
     Failure/Error: series('fibonacci', 7).should eq 13
     NoMethodError:
       undefined method `sumtwo_series' for #<RSpec::Core::ExampleGroup::Nested_1:0xb8faddc8>
     # /tmp/d20141023-2426-fdd8ny/solution.rb:9:in `series'
     # /tmp/d20141023-2426-fdd8ny/spec.rb:10: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 fibonacci series for even numbers
     Failure/Error: series('fibonacci', 8).should eq 21
     NoMethodError:
       undefined method `sumtwo_series' for #<RSpec::Core::ExampleGroup::Nested_1:0xb8fa900c>
     # /tmp/d20141023-2426-fdd8ny/solution.rb:9:in `series'
     # /tmp/d20141023-2426-fdd8ny/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)>'

  4) series handles fibonacci series for bigger numbers
     Failure/Error: series('fibonacci', 15).should eq 610
     NoMethodError:
       undefined method `sumtwo_series' for #<RSpec::Core::ExampleGroup::Nested_1:0xb8fa70cc>
     # /tmp/d20141023-2426-fdd8ny/solution.rb:9:in `series'
     # /tmp/d20141023-2426-fdd8ny/spec.rb:20: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 lucas series for base cases
     Failure/Error: series('lucas', 1).should eq 2
     NoMethodError:
       undefined method `sumtwo_series' for #<RSpec::Core::ExampleGroup::Nested_1:0xb8fa52b8>
     # /tmp/d20141023-2426-fdd8ny/solution.rb:10:in `series'
     # /tmp/d20141023-2426-fdd8ny/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)>'

  6) series handles lucas series for odd numbers
     Failure/Error: series('lucas', 7).should eq 18
     NoMethodError:
       undefined method `sumtwo_series' for #<RSpec::Core::ExampleGroup::Nested_1:0xb8fa3454>
     # /tmp/d20141023-2426-fdd8ny/solution.rb:10:in `series'
     # /tmp/d20141023-2426-fdd8ny/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)>'

  7) series handles lucas series for even numbers
     Failure/Error: series('lucas', 8).should eq 29
     NoMethodError:
       undefined method `sumtwo_series' for #<RSpec::Core::ExampleGroup::Nested_1:0xb8fa1a00>
     # /tmp/d20141023-2426-fdd8ny/solution.rb:10:in `series'
     # /tmp/d20141023-2426-fdd8ny/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)>'

  8) series handles lucas series for bigger numbers
     Failure/Error: series('lucas', 15).should eq 843
     NoMethodError:
       undefined method `sumtwo_series' for #<RSpec::Core::ExampleGroup::Nested_1:0xb8f9fb10>
     # /tmp/d20141023-2426-fdd8ny/solution.rb:10:in `series'
     # /tmp/d20141023-2426-fdd8ny/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)>'

  9) series handles summed series for base cases
     Failure/Error: series('summed', 1).should eq 3
     NoMethodError:
       undefined method `sumtwo_series' for #<RSpec::Core::ExampleGroup::Nested_1:0xb9a23f64>
     # /tmp/d20141023-2426-fdd8ny/solution.rb:11:in `series'
     # /tmp/d20141023-2426-fdd8ny/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)>'

  10) series handles summed series for odd numbers
     Failure/Error: series('summed', 7).should eq 31
     NoMethodError:
       undefined method `sumtwo_series' for #<RSpec::Core::ExampleGroup::Nested_1:0xb9a234b0>
     # /tmp/d20141023-2426-fdd8ny/solution.rb:11:in `series'
     # /tmp/d20141023-2426-fdd8ny/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)>'

  11) series handles summed series for even numbers
     Failure/Error: series('summed', 8).should eq 50
     NoMethodError:
       undefined method `sumtwo_series' for #<RSpec::Core::ExampleGroup::Nested_1:0xb9a229fc>
     # /tmp/d20141023-2426-fdd8ny/solution.rb:11:in `series'
     # /tmp/d20141023-2426-fdd8ny/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)>'

  12) series handles summed series for bigger numbers
     Failure/Error: series('summed', 15).should eq 1453
     NoMethodError:
       undefined method `sumtwo_series' for #<RSpec::Core::ExampleGroup::Nested_1:0xb9a21f48>
     # /tmp/d20141023-2426-fdd8ny/solution.rb:11:in `series'
     # /tmp/d20141023-2426-fdd8ny/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.00865 seconds
12 examples, 12 failures

Failed examples:

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

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

Радослав обнови решението на 15.10.2014 16:59 (преди над 9 години)

+def sum_two_series(first, second, index)
+ for i in 1 .. index - 1
+ first += second
+ first, second = second, first
+ end
+ first
+end
+def series(name, index)
+ if name == 'fibonacci' then sumtwo_series(1, 1, index) end
+ if name == 'lucas' then sumtwo_series(2, 1, index) end
+ if name=='summed' then sumtwo_series(1, 1, index)+sumtwo_series(2, 1,index) end
+end

Радо,

Непременно се запознай с конвенциите, които ръководството по стил препоръчва. От следващата задача ще отнемаме точки за грубото им неспазване.

Специално за тази задача, дори съм готов да ти отнема точки за употребата на for, понеже сме го казали и натъртили изрично на лекции. Има го и в презентациите. Няма смисъл да го правя, обаче, защото решението ти няма да работи – кръстил си си функцията sum_two_series, а я викаш като sumtwo_series. Освен това,sumtwo_series е лошо име.

Явно е, че времето те е притискало. Поздравления, че все пак успя да предадеш нещо, макар и в последните секунди :) Опитай следващия път да отделиш повече време. Няма да съжаляваш :)

Ако не предаваш в последния момент, ще можем да ти оставим коментари и ще имаш време да реагираш на тях :)

Все пак с гадната (дървовидна) рекурсия ли трябваше да се направи? Защото линейният алгоритъм е доста по-бърз, и затова реших да направя компромис със стила на кода. После се сетих, че е възможно интерпретаторът да оптимизира рекурсията по някакъв начин (на което обикновено се разчита при писане в декларативен стил), но вече беше късно. Май не съм бил прав (за алгоритъма), нали?

Няма оптимизация на този вид рекурсия (виж тук).

Няма проблем да ползваш обикновена рекурсия, за това просто решение щеше да свърши работа. Курсът не е ДАА :)

Все пак, ако държиш да не е с рекурсия, можеш да го направиш интеративно, но не с for. Прегледай презентациите, за да видиш как се прави "цикъл" в Ruby.