Ruby

Day 17 of 30 - Ruby Coding Challenge - Decent Ruby Way to Sum Even Numbers in a Fibonacci Sequence

Day 17 of 30 - Ruby Coding Challenge - Decent Ruby Way to Sum Even Numbers in a Fibonacci Sequence

Hey friends!

This is the blog post version of the Youtube video from the 30 Ruby Coding Challenges in 30 Days series

Today we want to solve the previous problem in a more Ruby Way

I want to sum all even numbers in a Fibonacci sequence

Last Algorithm Version

This was the last algorithm version of the problem:

def fibonacci_sum(count)
  sum = 0
	number = 0
  sequence = []
  (0..count).each do |item|
    number = item if item <= 1
    number = sequence[-1] + sequence[-2] if item > 1
    sequence << number

    sum += number if number % 2 == 0
  end
  sum
end

The code is not that great for a couple of reasons:

  • too many local variables to manipulate
  • two main responsibilities: create the Fibonacci sequence AND validate even numbers

The 2 main reasons above leave us with a difficult code to read, therefore, a difficult code to maintain. Fewer friends in our team, right? 😅

I’m going to try to get rid of these problems. Let’s get into it!

Better Algorithm Version - Ruby Way

I’m going to break the refactoring into a few small steps

Step 1 - Splitting Responsibilities

  • let’s create a new method do generate only the Fibonacci sequence
def fibonacci(count)
  sequence = []
  (0..count).each do |number|
    sequence << number if number <= 1
    sequence << sequence[-1] + sequence[-2] if number > 1
  end
  sequence
end
  • then we’re going to create the method to sum even numbers based on the generated sequence
def fibonacci(count)
  sequence = []
  (0..count).each do |number|
    sequence << number if number <= 1
    sequence << sequence[-1] + sequence[-2] if number > 1
  end
  sequence
end

def sum(array)
  # magic here
end

puts sum(fibonacci(10))

Step 2 - Sum of Even Numbers

Now, we just need to sum all the even numbers, given an array of numbers

As we did previously here, I’m going to use a Ruby symbol, which allows us to reduce a list of items into a single number by applying an operation

 def sum(array)
  array.select { |number| number % 2 == 0 }.reduce(:+)
end

The complete code would be:

def fibonacci(count)
  sequence = []
  (0..count).each do |number|
    sequence << number if number <= 1
    sequence << sequence[-1] + sequence[-2] if number > 1
  end
  sequence
end

def sum(array)
  array.select { |number| number % 2 == 0 }.reduce(:+)
end

puts sum(fibonacci(10))

That’s it! A little bit better version of the previous problem and I hope you liked it!

Do you have a better/different version? Drop that in the Youtube comments and bring more healthy discussions 😄

Thanks for the visit and see you in the next coding challenge!

Don’t forget to come by and say hi Alex

Courses Twitter Youtube Instagram Linkedin GitHub

Join My Newsletter

You’ll receive coding courses, tutorials and personal posts! I’ll be happy to send them to you, weekly :)