coding for food

[Ruby] exercise _ Fibonacci

silveryen 2022. 8. 7. 05:55
# my code

def fibonacci(length)
  arr = []
  
  i = 0
  while i < length
    if i < 2
    	arr << 1
    else
       arr << arr[-1] + arr[-2]
    end   
    i += 1
  end

  return arr

end

print fibonacci(0) # => []
puts
print fibonacci(1) # => [1]
puts
print fibonacci(6) # => [1, 1, 2, 3, 5, 8]
puts
print fibonacci(8) # => [1, 1, 2, 3, 5, 8, 13, 21]
puts
#solution

    return []
  elsif length == 1
    return [1]
  end

  seq = [1, 1]

  while seq.length < length
    last = seq[-1]
    second_to_last = seq[-2]
    next_ele = last + second_to_last
    seq << next_ele
  end

  return seq
end

print fibonacci(0) # => []
puts
print fibonacci(1) # => [1]
puts
print fibonacci(6) # => [1, 1, 2, 3, 5, 8]
puts
print fibonacci(8) # => [1, 1, 2, 3, 5, 8, 13, 21]
puts