Ruby

Day 18 of 30 - Ruby Coding Challenge - Finding the Missing Number Game

Day 18 of 30 - Ruby Coding Challenge - Finding the Missing Number Game

Hey friends!

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

Let’s go straight to the point

Coding Challenge

Create a function that takes an array of numbers between 1 and 10 (excluding one number) and returns the missing number.

A few points:

  • The array of numbers will be unsorted (not in order).
  • Only one number will be missing.

Examples

find_missing_num([1, 2, 3, 4, 6, 7, 8, 9, 10])  5

find_missing_num([7, 2, 3, 6, 5, 9, 1, 4, 8])  10

find_missing_num([10, 5, 1, 2, 4, 6, 8, 3, 9])  7

Pretty simple, isn’t it?

Algorithm in Ruby

Let’s break the algorithm down to a few small steps:

Step 1

  • We need to go through a list of items (an array), going from 1 to 10
def find_missing_num(array)
  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].each do |item|
	end
end

array = [1, 2, 3, 4, 6, 7, 8, 9, 10]
find_missing_num(array)

Ruby provides us a really better way to do this:

def find_missing_num(array)
  (1..10).each do |item|
	end
end

array = [1, 2, 3, 4, 6, 7, 8, 9, 10]
find_missing_num(array)

Step 2

  • We’re going to compare each number (from 1 to 10) with the numbers in the given array
def find_missing_num(array)
  (1..10).each do |item|
    array.each do |number|
      if item == number
        break # breaking the execution
      end      
    end
  end
end

array = [1, 2, 3, 4, 6, 7, 8, 9, 10]
find_missing_num(array)

We break the execution once if find the desired number because we don’t need to continue looking for the number

Step 3

  • Because we want to return the number that wasn’t found in the array, I’m going to create a local variable to indicate that
def find_missing_num(array)
  (1..10).each do |item|
    found = false
    array.each do |number|
      if item == number
        found = true
        break
      end      
    end
    if found = false
      return item
    end
  end
end

array = [1, 2, 3, 4, 6, 7, 8, 9, 10]
find_missing_num(array)

That’s it!

I know, that’s not beautiful but it works : )

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

Thanks for the visit and in the next coding challenge we’re going to explore a more decent way to solve this puzzle, see you there!

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 :)