DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Evaluate Coding Agents With Harbor Framework, Python, Docker
  • After the AI Commit: What Eight Retrospectives Taught Me About Working With Coding Agents
  • AI Coding Agent Guardrails: Finding Gaps in Tool Permissions
  • When a Coding Agent Fixes the Test Instead of the Bug

Trending

  • Your Event Storming Is Not Your Software Design
  • Idempotency Under Concurrency: Why Your Application Check Isn't the Last Line of Defense
  • Prompt Caching for LLMs, Part 3: Model-Side Caching and Production Rollout
  • When Feature Flags Become Workflows: Configuring Multi-Tenant State Machines

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

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

We're going to play a game: find the missing number in a given array. This first solution will be not that great but we'll get the job done!

By 
Alexandre Gama
·
Updated Jul. 22, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
8.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Ruby
xxxxxxxxxx
1
 
1
find_missing_num([1, 2, 3, 4, 6, 7, 8, 9, 10]) ➞ 5
2
3
find_missing_num([7, 2, 3, 6, 5, 9, 1, 4, 8]) ➞ 10
4
5
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.

Ruby
xxxxxxxxxx
1
 
1
def find_missing_num(array)
2
  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].each do |item|
3
    end
4
end
5
6
array = [1, 2, 3, 4, 6, 7, 8, 9, 10]
7
find_missing_num(array)


Ruby provides us a really better way to do this:

Ruby
xxxxxxxxxx
1
 
1
def find_missing_num(array)
2
  (1..10).each do |item|
3
    end
4
end
5
6
array = [1, 2, 3, 4, 6, 7, 8, 9, 10]
7
find_missing_num(array)


Step 2

We’re going to compare each number (from 1 to 10) with the numbers in the given array.

Ruby
xxxxxxxxxx
1
12
 
1
def find_missing_num(array)
2
  (1..10).each do |item|
3
    array.each do |number|
4
      if item == number
5
        break # breaking the execution
6
      end      
7
    end
8
  end
9
end
10
11
array = [1, 2, 3, 4, 6, 7, 8, 9, 10]
12
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.

Ruby
xxxxxxxxxx
1
17
 
1
def find_missing_num(array)
2
  (1..10).each do |item|
3
    found = false
4
    array.each do |number|
5
      if item == number
6
        found = true
7
        break
8
      end      
9
    end
10
    if found = false
11
      return item
12
    end
13
  end
14
end
15
16
array = [1, 2, 3, 4, 6, 7, 8, 9, 10]
17
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 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!

Courses Twitter Youtube Instagram Linkedin GitHub

Coding (social sciences)

Published at DZone with permission of Alexandre Gama. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Evaluate Coding Agents With Harbor Framework, Python, Docker
  • After the AI Commit: What Eight Retrospectives Taught Me About Working With Coding Agents
  • AI Coding Agent Guardrails: Finding Gaps in Tool Permissions
  • When a Coding Agent Fixes the Test Instead of the Bug

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook