Lesson 4
Center-Outward Slice Traversal in Go
Introduction

Hello and welcome to our lesson today! We are about to dive into an intriguing aspect of slice manipulation using Go. Imagine traversing a slice not from the start to the end or from the end to the start, but from the center outward in both directions. Today’s lesson will explore this concept. Get ready for an exciting journey into Go's slice manipulation and traversal capabilities.

Task Statement

Our task is to produce a new slice from a given slice of integers. This new slice will start from the center of the original slice and alternate direction towards both ends. The first element of our new slice will be the middle element (if the length is odd) or the element to the left of the center (if the length is even). From this starting point, we'll alternate between elements to the left and right until all elements have been included.

For example:

  • Given an odd-length slice like numbers := []int{1, 2, 3, 4, 5}, the output would be []int{3, 2, 4, 1, 5}. Here, 3 is the middle element.
  • Given an even-length slice like numbers := []int{1, 2, 3, 4, 5, 6}, the output would be []int{3, 4, 2, 5, 1, 6}. Here, 3 and 4 are considered as elements around the midpoint, starting with 3 from the left.

The length of the slice, denoted as n, can range from 1 to 100,000, inclusive.

Solution Building: Step 1

First, let's determine the midpoint of our slice. Our task requires us to expand outwards from the center, so we divide its length by 2 using integer division. If the slice length is odd, we add the middle element to the newOrder slice because it has no counterpart. If the length is even, newOrder starts empty.

Here's how it looks:

Go
1func iterateMiddleToEnd(numbers []int) []int { 2 mid := len(numbers) / 2 // index of the left middle element 3 var newOrder []int // slice to store new order 4 5 if len(numbers)%2 == 1 { 6 newOrder = append(newOrder, numbers[mid]) // Adding middle element if length is odd 7 } 8 // newOrder remains empty for now if length is even
Solution Building: Step 2

Next, let's set up our two pointers: left and right. These pointers will help us navigate the elements to the left and right of the middle element, respectively.

Here's how this step looks:

Go
1func iterateMiddleToEnd(numbers []int) []int { 2 mid := len(numbers) / 2 3 var left, right int 4 var newOrder []int 5 6 if len(numbers)%2 == 1 { 7 left = mid - 1 // Pointing to the element on the left of the middle 8 right = mid + 1 // Pointing to the element on the right of the middle 9 newOrder = append(newOrder, numbers[mid]) // Adding middle element 10 } else { 11 left = mid - 1 // Pointing to the left middle element 12 right = mid // Pointing to the right middle element 13 }
Solution Building: Step 3

With our pointers initialized, we can now construct our new order. In Go, we use a for loop to iterate from the center of the slice towards both ends. In each iteration, we append the elements at indices left and right to newOrder, then decrement left and increment right.

Here's the full function:

Go
1package main 2 3import "fmt" 4 5func iterateMiddleToEnd(numbers []int) []int { 6 mid := len(numbers) / 2 7 var left, right int 8 var newOrder []int 9 10 if len(numbers)%2 == 1 { 11 left = mid - 1 12 right = mid + 1 13 newOrder = append(newOrder, numbers[mid]) 14 } else { 15 left = mid - 1 16 right = mid 17 } 18 19 for left >= 0 && right < len(numbers) { 20 newOrder = append(newOrder, numbers[left]) 21 left-- 22 newOrder = append(newOrder, numbers[right]) 23 right++ 24 } 25 26 return newOrder 27} 28 29func main() { 30 numbers := []int{1, 2, 3, 4, 5} 31 fmt.Println(iterateMiddleToEnd(numbers)) // Output: [3 2 4 1 5] 32}

By implementing this approach, we've successfully created a new slice starting from the center and alternating to the left and right ends of the original slice, fulfilling the task requirements!

Lesson Summary

Congratulations on completing this lesson! You've discovered an intriguing method of traversing and manipulating slices in Go. Take a moment to appreciate your understanding and implementation of this concept. Remember, practice is the key to mastering slice manipulation in Go, so try applying this concept to similar problems. Your journey in mastering Go is just beginning. Embrace the challenge and happy coding!

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