Решение на Първа задача от Светослав Кузманов

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

Към профила на Светослав Кузманов

Резултати

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

Код

def fibonacci(element_index)
return element_index if element_index <= 1
fibonacci(element_index - 1) + fibonacci(element_index - 2)
end
def lucas(element_index)
return 2 if element_index == 1
return 1 if element_index == 2
lucas(element_index - 1) + lucas(element_index - 2)
end
def series(name, element_index)
case name
when 'fibonacci' then fibonacci(element_index)
when 'lucas' then lucas(element_index)
when 'summed' then lucas(element_index) + fibonacci(element_index)
end
end

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

............

Finished in 0.02609 seconds
12 examples, 0 failures

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

Светослав обнови решението на 12.10.2014 23:54 (преди над 9 години)

+def fibonacci(n)
+ return n if n <= 1
+ fibonacci(n - 1) + fibonacci(n - 2)
+end
+
+def lucas(n)
+ return 2 if n == 1
+ return 1 if n == 2
+ lucas(n - 1) + lucas(n - 2)
+end
+
+def series(name, n)
+ return fibonacci(n) if name == 'fibonacci'
+ return lucas(n) if name == 'lucas'
+ return lucas(n) + fibonacci(n) if name == 'summed'
+end

Здравей :)

  • В series според мен много по-четимо би било да се ползва case вместо 3 if-a. Това е switch-a в Руби.
  • В fibonacci и lucas след return и преди if имаш по 2 интервала, а трябва да имаш по 1 :)
  • Съща така за именуването - тук е добре да даваш хубави, описателни имена на променливи и аргументи, и не 'n' не е такова. Няколко символа повече няма да навредят и ще е по-четимо и разбираемо. (Прочети style guide-a)

Като махнем горните - добре решение :)

Светослав обнови решението на 15.10.2014 15:34 (преди над 9 години)

-def fibonacci(n)
- return n if n <= 1
- fibonacci(n - 1) + fibonacci(n - 2)
+def fibonacci(element_index)
+ return element_index if element_index <= 1
+ fibonacci(element_index - 1) + fibonacci(element_index - 2)
end
-def lucas(n)
- return 2 if n == 1
- return 1 if n == 2
- lucas(n - 1) + lucas(n - 2)
+def lucas(element_index)
+ return 2 if element_index == 1
+ return 1 if element_index == 2
+ lucas(element_index - 1) + lucas(element_index - 2)
end
-def series(name, n)
- return fibonacci(n) if name == 'fibonacci'
- return lucas(n) if name == 'lucas'
- return lucas(n) + fibonacci(n) if name == 'summed'
+def series(name, element_index)
+ case name
+ when 'fibonacci' then fibonacci(element_index)
+ when 'lucas' then lucas(element_index)
+ when 'summed' then lucas(element_index) + fibonacci(element_index)
+ end
end