Lesson 4
Traversing Arrays from the Middle
Introduction

Greetings! Welcome to this unit's lesson, where we’ll dive into a fascinating aspect of array manipulation. Have you ever considered traversing an array from the middle, alternating toward both ends simultaneously? That’s exactly what we’ll explore today.

By the end of this lesson, you’ll understand how to implement this unique traversal method in Ruby. Let’s jump right in!

Task Statement

You are tasked with creating a method that takes an array of integers and returns a new array. The new array is built by starting at the middle of the original array and alternating between elements to the left and right of the middle.

Here’s the specific behavior:

  • If the array has an odd length, the middle element is added first, followed by alternating elements to the left and right.
  • If the array has an even length, alternation begins with the left middle element, then the right middle element, and continues outward.

For example:

  • Given numbers = [1, 2, 3, 4, 5], the output should be [3, 2, 4, 1, 5].
  • For numbers = [1, 2, 3, 4, 5, 6], the output should be [3, 4, 2, 5, 1, 6].

This approach works for arrays of varying lengths, from 1 to 100,000. Let’s break down the solution step by step.

Step 1: Find the Middle

To start, we need to determine the middle of the array. Ruby’s integer division (/) makes it simple to calculate the middle index. If the array length is odd, the middle element will be included in the output first. Otherwise, we’ll prepare for alternation between the two middle elements.

Here’s how we identify the middle:

Ruby
1def traverse_from_middle(numbers) 2 mid = numbers.length / 2 3 new_order = if numbers.length.odd? 4 [numbers[mid]] # Start with the middle element for odd-length arrays 5 else 6 [] # Start with an empty array for even-length arrays 7 end

This initializes the new_order array based on whether the input array’s length is odd or even.

Step 2: Initialize Pointers

Next, we initialize two pointers, left and right, to keep track of the elements on either side of the middle. These pointers will guide us as we alternate between elements to the left and right of the middle.

Here’s how we set up the pointers:

Ruby
1 left = mid - 1 2 right = mid 3 if numbers.length.odd? 4 right += 1 # Adjust right pointer for odd-length arrays 5 end

The left pointer starts just before the middle, and the right pointer starts just after the middle for odd-length arrays, or at the second middle element for even-length arrays.

Step 3: Traverse the Array

With our pointers set, we traverse the array and construct the new_order array. Using a while loop, we continue until both pointers move out of bounds. In each iteration:

  1. Add the element at the left pointer to new_order.
  2. Move the left pointer one step to the left.
  3. Add the element at the right pointer to new_order.
  4. Move the right pointer one step to the right.

Here’s the complete traversal:

Ruby
1 while left >= 0 || right < numbers.length 2 new_order << numbers[left] if left >= 0 # Add left element if in bounds 3 new_order << numbers[right] if right < numbers.length # Add right element if in bounds 4 left -= 1 5 right += 1 6 end

This ensures we alternate between left and right elements until all elements are included in the new array.

Step 4: Return the Result

Finally, we return the new_order array containing the elements in the desired order.

Here’s the complete method:

Ruby
1def traverse_from_middle(numbers) 2 mid = numbers.length / 2 3 new_order = if numbers.length.odd? 4 [numbers[mid]] # Start with the middle element for odd-length arrays 5 else 6 [] # Start with an empty array for even-length arrays 7 end 8 9 left = mid - 1 10 right = mid 11 right += 1 if numbers.length.odd? 12 13 while left >= 0 || right < numbers.length 14 new_order << numbers[left] if left >= 0 15 new_order << numbers[right] if right < numbers.length 16 left -= 1 17 right += 1 18 end 19 20 new_order 21end 22 23# Example usage: 24numbers_odd = [1, 2, 3, 4, 5] 25result_odd = traverse_from_middle(numbers_odd) 26puts result_odd.inspect # Output: [3, 2, 4, 1, 5] 27 28numbers_even = [1, 2, 3, 4, 5, 6] 29result_even = traverse_from_middle(numbers_even) 30puts result_even.inspect # Output: [3, 4, 2, 5, 1, 6]

This method captures the essence of traversing an array from the middle, alternating toward the ends.

Lesson Summary

Congratulations! You’ve learned how to traverse an array in a unique manner, starting from the middle and alternating outward. This approach highlights the flexibility of Ruby for solving interesting problems.

Keep practicing, and don’t hesitate to experiment with variations of this technique. With each challenge, you’ll strengthen your understanding and skills in array manipulation. Keep up the great work!

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