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

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

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

Резултати

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

Код

def series(string, int)
if string == 'fibonacci'
return fibonacci(int)
elsif string == 'lucas'
return lucas(int)
end
return fibonacci(int) + lucas(int)
end
def fibonacci(int)
if int == 1
return 1
elsif int == 2
return 1
else
return fibonacci(int+1) + fibonacci(int+2)
end
end
def lucas(int)
if int == 1
return 2
elsif int == 2
return 1
else
return lucas(int+1) + lucas(int+2)
end
end

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

.FFF.FFF.FFF

Failures:

  1) series handles fibonacci series for odd numbers
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20141023-2426-dlf1sc/solution.rb:16

  2) series handles fibonacci series for even numbers
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20141023-2426-dlf1sc/solution.rb:16

  3) series handles fibonacci series for bigger numbers
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20141023-2426-dlf1sc/solution.rb:16

  4) series handles lucas series for odd numbers
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20141023-2426-dlf1sc/solution.rb:16

  5) series handles lucas series for even numbers
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20141023-2426-dlf1sc/solution.rb:16

  6) series handles lucas series for bigger numbers
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20141023-2426-dlf1sc/solution.rb:16

  7) series handles summed series for odd numbers
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20141023-2426-dlf1sc/solution.rb:16

  8) series handles summed series for even numbers
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20141023-2426-dlf1sc/solution.rb:16

  9) series handles summed series for bigger numbers
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20141023-2426-dlf1sc/solution.rb:16

Finished in 0.02539 seconds
12 examples, 9 failures

Failed examples:

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

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

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

+def series(string, int)
+ if string == 'fibonacci'
+ return fibonacci(int)
+ elsif string == 'lucas'
+ return lucas(int)
+ end
+ return fibonacci(int) + lucas(int)
+end
+
+def fibonacci(int)
+ if int == 1
+ return 1
+ elsif int == 2
+ return 1
+ else
+ return fibonacci(int+1) + fibonacci(int+2)
+ end
+end
+
+def lucas(int)
+ if int == 1
+ return 2
+ elsif int == 2
+ return 1
+ else
+ return lucas(int+1) + lucas(int+2)
+ end
+end

Мони, малко бележки:

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

  • Трябва да отместваш кода си само с два интервала. Не с табове, не с друго. Два интервала.
  • Трябва да има точно по един интервал около "операторите", като <=, -, +, ?, ==, : и т.н.
  • Ако ползваш else в series, ще отпадне нуждата да ползваш return. Това ще направи кода по-ясен и прост. А и сега няма нужда от последния return.
  • Във fibonacci няма нужда от return никъде. Същото и за lucas.
  • int не е добро име за променлива в случая. Твърде общо е. number, index, series_index или n са по-добри.

И, най-важното – не предавай в последния момент, за да можем да ти оставим коментари и да реагираш на тях :)

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

def series(string, int)
if string == 'fibonacci'
return fibonacci(int)
elsif string == 'lucas'
return lucas(int)
end
return fibonacci(int) + lucas(int)
end
def fibonacci(int)
if int == 1
return 1
elsif int == 2
return 1
- else
- return fibonacci(int+1) + fibonacci(int+2)
+ else
+ return fibonacci(int+1) + fibonacci(int+2)
end
end
def lucas(int)
if int == 1
return 2
elsif int == 2
return 1
- else
- return lucas(int+1) + lucas(int+2)
+ else
+ return lucas(int+1) + lucas(int+2)
end
end