Lesson 1
Pairing Opposite Elements in Arrays
Lesson Overview

Welcome! In this lesson, we’re diving into an engaging task involving arrays: pairing up 'opposite' elements.

This is a perfect opportunity to enhance your skills in accessing and manipulating elements in Ruby arrays. Are you ready to jump in? Let’s get started!

Task Statement and Description

Our task is to pair up 'opposite' elements in a given array of integers. For an array of n elements, the first and last elements are considered 'opposite', the second and second-to-last are 'opposite', and so forth. If the array length is odd, the middle element is paired with itself.

You are provided with an array of integers, with the size n ranging from 1 to 100, inclusive. The goal is to return an array of arrays, where each sub-array consists of a pair of an element and its 'opposite' element.

For instance, given numbers = [1, 2, 3, 4, 5, 6, 7], the output should be pair_opposite_elements(numbers) = [[1, 7], [2, 6], [3, 5], [4, 4], [5, 3], [6, 2], [7, 1]].

Solution Building: Step 1

Before writing the code, let’s review how to access elements in Ruby arrays.

In Ruby, the i-th element of an array numbers can be accessed using numbers[i], where indexing starts at 0. Therefore:

  • The first element is numbers[0].
  • The second element is numbers[1].
  • The last element can be accessed using numbers[numbers.size - 1].

Now that we understand basic indexing, let’s figure out how to access each element’s 'opposite'.

Solution Building: Step 2

The 'opposite' of the i-th element in the array is the numbers.size - i - 1-th element. To picture this, think of standing at one end of a seesaw and your friend at the other—both of you are 'opposites'.

For example:

  • The opposite of numbers[0] is numbers[numbers.size - 1].
  • The opposite of numbers[1] is numbers[numbers.size - 2].

We begin our implementation by initializing an empty array to store the 'opposite' pairs and calculating the size of the array for reference.

Ruby
1def pair_opposite_elements(numbers) 2 result = [] 3 n = numbers.size

Next, we loop through all elements in the array using each_with_index.

Ruby
1def pair_opposite_elements(numbers) 2 result = [] 3 n = numbers.size 4 numbers.each_with_index do |number, i| 5 # next step comes here
Solution Building: Step 3

Within the loop, we create an array for each pair of 'opposite' elements. This pair includes the i-th element and its opposite numbers[n - i - 1]. We append this pair to our result array. Here’s the updated code:

Ruby
1def pair_opposite_elements(numbers) 2 result = [] 3 n = numbers.size 4 numbers.each_with_index do |number, i| 5 result << [number, numbers[n - i - 1]] 6 end 7 result 8end

This function iterates over all elements of the array, forming a pair with its 'opposite' for each, and stores the pair in the result array.

Solution Building: Step 4

Now, let’s call the function and interpret the output. Using the array numbers = [1, 2, 3, 4, 5], let’s see how our function behaves.

Ruby
1def pair_opposite_elements(numbers) 2 result = [] 3 n = numbers.size 4 numbers.each_with_index do |number, i| 5 result << [number, numbers[n - i - 1]] 6 end 7 result 8end 9 10numbers = [1, 2, 3, 4, 5] 11result = pair_opposite_elements(numbers) 12puts result.inspect # Output: [[1, 5], [2, 4], [3, 3], [4, 2], [5, 1]]

Notice how each element is paired with its 'opposite', and the middle element (3) is paired with itself since the array has an odd number of elements.

Lesson Summary

Brilliant work! You’ve just explored the concept of 'opposite' pairs and array indexing in Ruby. By now, you should be comfortable accessing and pairing elements in an array based on their positions. This knowledge is a fundamental step toward mastering array manipulation in Ruby.

Next, you’ll have a chance to apply and practice these concepts with hands-on exercises. Keep up the momentum, and happy coding!

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