Lesson 1
Mastering Nested Loops with Arrays
Introduction

Welcome to this programming practice session! In this lesson, we’ll focus on using nested loops to work with arrays. Specifically, you’ll learn how to compare elements from two arrays and identify pairs of numbers that satisfy a specific condition.

This is a hands-on opportunity to sharpen your Ruby skills while tackling an interesting and practical challenge. Let’s dive in!

Task Statement

You are given two arrays of integers. Your goal is to write a method, find_pairs, that identifies and returns pairs of integers where:

  • The first element of the pair comes from the first array.
  • The second element of the pair comes from the second array.
  • The first element is strictly less than the second.

The pairs should appear in the result in the order they are encountered in the input arrays. If there are no valid pairs, or if either input array is empty, the method should return an empty array.

For the input arrays [1, 3, 7] and [2, 8, 9], the method should return:

Ruby
1[[1, 2], [1, 8], [1, 9], [3, 8], [3, 9], [7, 8], [7, 9]]

In this example, the method compares each number in the first array with every number in the second array to find pairs where the first number is smaller than the second.

Now let’s break the solution into manageable steps.

Step 1: Setting Up the Method

To start, define the method find_pairs and initialize an empty array called result. This array will store the valid pairs of numbers we find.

Ruby
1def find_pairs(array1, array2) 2 result = []

Initializing your storage (result) upfront is a good practice to keep your code clean and structured.

Step 2: Creating Nested Loops

Next, set up nested loops to compare every element from array1 with every element from array2. Use Ruby’s each method for readability and clarity.

Ruby
1def find_pairs(array1, array2) 2 result = [] 3 array1.each do |i| 4 array2.each do |j| 5 # Logic will go here 6 end 7 end 8 result 9end

Here, the outer loop iterates through each element i in array1, and the inner loop iterates through each element j in array2.

Step 3: Adding the Condition

Inside the inner loop, include a condition to check if i (from array1) is less than j (from array2). If this condition is met, add the pair [i, j] to the result array using the << operator.

Ruby
1def find_pairs(array1, array2) 2 result = [] 3 array1.each do |i| 4 array2.each do |j| 5 result << [i, j] if i < j 6 end 7 end 8 result 9end

This concise use of an inline if statement follows Ruby’s best practices for clean and readable code.

Complete Method

Here’s the full implementation of the method:

Ruby
1def find_pairs(array1, array2) 2 result = [] 3 array1.each do |i| 4 array2.each do |j| 5 result << [i, j] if i < j 6 end 7 end 8 result 9end 10 11# Example Usage: 12pairs = find_pairs([1, 3, 7], [2, 8, 9]) 13puts pairs.inspect 14# Output: [[1, 2], [1, 8], [1, 9], [3, 8], [3, 9], [7, 8], [7, 9]]

This implementation ensures that the condition is checked for every possible combination of elements between the two arrays.

Lesson Summary

In this lesson, you learned how to use nested loops to compare elements from two arrays and build a result based on specific conditions. The method find_pairs demonstrates how to effectively traverse two arrays and construct a new array of paired elements. By mastering this concept, you’ve gained a key skill for working with complex data structures and solving array-based challenges.

Keep practicing these techniques, and you’ll find yourself writing cleaner, more efficient code in no time. Great work, and happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.