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

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

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

Резултати

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

Код

def fibonacci(number)
if number < 2
number
else
fibonacci(number - 1) + fibonacci(number - 2)
end
end
def lucas(number)
case number
when 1 then 2
when 2 then 1
else lucas(number - 1) + lucas(number - 2)
end
end
def series(name, number)
case name
when "lucas" then series(number)
when "fibonacci" then fibonacci(number)
when "summed" then fibonacci(number) + lucas(number)
end
end

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

....FFFF....

Failures:

  1) series handles lucas series for base cases
     Failure/Error: series('lucas', 1).should eq 2
     ArgumentError:
       wrong number of arguments (1 for 2)
     # /tmp/d20141023-2426-1xxju58/solution.rb:17:in `series'
     # /tmp/d20141023-2426-1xxju58/solution.rb:19:in `series'
     # /tmp/d20141023-2426-1xxju58/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
     ArgumentError:
       wrong number of arguments (1 for 2)
     # /tmp/d20141023-2426-1xxju58/solution.rb:17:in `series'
     # /tmp/d20141023-2426-1xxju58/solution.rb:19:in `series'
     # /tmp/d20141023-2426-1xxju58/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
     ArgumentError:
       wrong number of arguments (1 for 2)
     # /tmp/d20141023-2426-1xxju58/solution.rb:17:in `series'
     # /tmp/d20141023-2426-1xxju58/solution.rb:19:in `series'
     # /tmp/d20141023-2426-1xxju58/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
     ArgumentError:
       wrong number of arguments (1 for 2)
     # /tmp/d20141023-2426-1xxju58/solution.rb:17:in `series'
     # /tmp/d20141023-2426-1xxju58/solution.rb:19:in `series'
     # /tmp/d20141023-2426-1xxju58/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)>'

Finished in 0.0226 seconds
12 examples, 4 failures

Failed examples:

rspec /tmp/d20141023-2426-1xxju58/spec.rb:24 # series handles lucas series for base cases
rspec /tmp/d20141023-2426-1xxju58/spec.rb:29 # series handles lucas series for odd numbers
rspec /tmp/d20141023-2426-1xxju58/spec.rb:34 # series handles lucas series for even numbers
rspec /tmp/d20141023-2426-1xxju58/spec.rb:39 # series handles lucas series for bigger numbers

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

Станимир обнови решението на 13.10.2014 14:36 (преди около 10 години)

+def fibonacci(number)
+ if number < 2
+ number
+ else
+ fibonacci(number - 1) + fibonacci(number - 2)
+ end
+end
+
+def lucas(number)
+ if number < 3
+ if number % 2 == 1 then number + 1
+ else number - 1
+ end
+ else
+ lucas(number - 1) + lucas(number - 2)
+ end
+end
+
+def series(name, number)
+ case name
+ when "fibonacci" then fibonacci(number)
+ when "lucas" then lucas(number)
+ when "summed" then (fibonacci(number) + lucas(number))
+ end
+end

Привет :)

  • Може вместо цялата конструкция if else да ползваш например само if в следната форма x if y. Така ще си спестиш else, няколко реда, и ще стане може би малко по-четимо :)
  • В метода lucas вместо да имаш 2 вложени if-а и някакви сложни условия помисли как може да избегнеш това само като се погрижиш лесно за двете начални условия.
  • Добра практика е когато има съседни редове с подобно съдържание да се подравняват по например = или then.
  • Имаш едни скоби, които може да изпуснеш :)

Иначе добро решение :)

Станимир обнови решението на 15.10.2014 07:41 (преди около 10 години)

def fibonacci(number)
if number < 2
number
else
fibonacci(number - 1) + fibonacci(number - 2)
end
end
def lucas(number)
- if number < 3
- if number % 2 == 1 then number + 1
- else number - 1
- end
- else
- lucas(number - 1) + lucas(number - 2)
+ case number
+ when 1 then 2
+ when 2 then 1
+ else lucas(number - 1) + lucas(number - 2)
end
end
def series(name, number)
case name
+ when "lucas" then series(number)
when "fibonacci" then fibonacci(number)
- when "lucas" then lucas(number)
- when "summed" then (fibonacci(number) + lucas(number))
+ when "summed" then fibonacci(number) + lucas(number)
end
end