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

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

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

Резултати

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

Код

def series(type, n)
case type
when 'fibonacci' then fibonacci(n)
when 'lucas' then lucas(n)
when 'summed' then fibonacci(n) + lucas(n)
end
end
def fibonacci(n)
series_calculator(1, n)
end
def lucas(n)
series_calculator(2, n)
end
def series_calculator(first_index, n)
array = [first_index,1]
(2..n).each do |index|
array[index] = array[index - 1] + array[index - 2]
end
array[n - 1]
end

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

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

Finished in 0.00946 seconds
12 examples, 0 failures

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

Иван обнови решението на 13.10.2014 11:41 (преди над 9 години)

+def series(type, target_index)
+ if type == 'fibonacci' then fibonacci(target_index)
+ elsif type == 'lucas' then lucas(target_index)
+ elsif type == 'summed' then fibonacci(target_index) + lucas(target_index)
+ end
+end
+
+def fibonacci(target_index)
+ number = [1,1]
+ for index in 2..target_index - 1
+ number[index] = number[index-1] + number[index-2]
+ end
+ number[index-1]
+end
+
+def lucas(target_index)
+ l = [2,1]
+ for index in 2..target_index - 1
+ l[index] = l[index-1] + l[index-2]
+ end
+ l[target_index-1]
+end

Иван обнови решението на 13.10.2014 11:42 (преди над 9 години)

def series(type, target_index)
if type == 'fibonacci' then fibonacci(target_index)
elsif type == 'lucas' then lucas(target_index)
elsif type == 'summed' then fibonacci(target_index) + lucas(target_index)
end
end
def fibonacci(target_index)
- number = [1,1]
+ number = [1,1]
for index in 2..target_index - 1
number[index] = number[index-1] + number[index-2]
end
number[index-1]
end
def lucas(target_index)
l = [2,1]
for index in 2..target_index - 1
l[index] = l[index-1] + l[index-2]
end
l[target_index-1]
end

Иван обнови решението на 13.10.2014 23:22 (преди над 9 години)

-def series(type, target_index)
- if type == 'fibonacci' then fibonacci(target_index)
- elsif type == 'lucas' then lucas(target_index)
- elsif type == 'summed' then fibonacci(target_index) + lucas(target_index)
+def series(type, target)
+ if type == 'fibonacci' then fibonacci(target)
+ elsif type == 'lucas' then lucas(target)
+ elsif type == 'summed' then fibonacci(target) + lucas(target)
end
end
-def fibonacci(target_index)
+def fibonacci(target)
number = [1,1]
- for index in 2..target_index - 1
+ for index in 2..target
number[index] = number[index-1] + number[index-2]
end
- number[index-1]
+ number[target-1]
end
-def lucas(target_index)
- l = [2,1]
- for index in 2..target_index - 1
- l[index] = l[index-1] + l[index-2]
+def lucas(target)
+ number = [2,1]
+ for index in 2..target
+ number[index] = number[index-1] + number[index-2]
end
- l[target_index-1]
+ number[target-1]
end

Здрасти :)

  • Разгледай case statement-a в ruby, той е точно за такива ситуации като if-elsif във series.

  • Функциите fibonacci и lucas изглежда почти еднакво. Изведи повтарящата се логика в отделна функция и я преизползвай.

  • Ако си бил вчера на лекции знаеш, че не харесваме for. Замени го с each, или експериментирай с друга функция за итериране.

  • Оставяй място около - и разгледай style guide-a.

  • target не е много добро име. Може би index, или дори n e по-добре. Като цяло не се кефим на имена от типа n, но при математически обекти може да се направи компромис.

  • number също не е много добро име за този масив от числа. Той ти съхранява членове от редицата. Помисли за по-добро име с такава семантика.

Благодаря за съветите!

Постарах се да променя нещата, които си посочил. Проблемът е, че нямам възможност да тествам програмата със skeptic, тъй като съм на работа.

Замених number със array, тъй като все пак имаме масив :D Във методът series_calculator съм извел общата логика за двете функции, като се изисква first_index.

Относно стилът, не ми стана много ясно за each дали е по-правилно да се записва като

@collection.each do |item|
   # do whatever
end

или като

@collection.each {|item| ... }

Иван обнови решението на 15.10.2014 11:30 (преди над 9 години)

-def series(type, target)
- if type == 'fibonacci' then fibonacci(target)
- elsif type == 'lucas' then lucas(target)
- elsif type == 'summed' then fibonacci(target) + lucas(target)
+def series(type, n)
+ case type
+ when 'fibonacci' then fibonacci(n)
+ when 'lucas' then lucas(n)
+ when 'summed' then fibonacci(n) + lucas(n)
end
end
-def fibonacci(target)
- number = [1,1]
- for index in 2..target
- number[index] = number[index-1] + number[index-2]
- end
- number[target-1]
+def fibonacci(n)
+ series_calculator(1, n)
end
-def lucas(target)
- number = [2,1]
- for index in 2..target
- number[index] = number[index-1] + number[index-2]
+def lucas(n)
+ series_calculator(2, n)
+end
+
+def series_calculator(first_index, n)
+ array = [first_index,1]
+ (2..n).each do |index|
+ array[index] = array[index - 1] + array[index - 2]
end
- number[target-1]
+ array[n - 1]
end