Introduction

Hello, aspiring Ruby programmer! Are you ready to journey into the world of matrices? Today, we will explore the realm of 2D matrices with a unique traversal order that promises excitement and intrigue. Fasten your seatbelt and let's dive in!

Task Statement

Imagine we have a 2D matrix where each cell contains a distinct symbol or integer. Our task is to decode this matrix by reading the cells in a particular pattern.

The decoding begins from the top-left cell of the matrix. We move in a diagonal direction toward the bottom-left until we reach the left boundary. Upon hitting the left boundary, we move one cell down (unless we're at the bottom-left corner, in which case we move one cell to the right) and begin moving diagonally upward toward the upper-right.

While moving diagonally upward, if we hit the top boundary, we move one cell to the right and start going downwards diagonally. If we hit the right boundary while moving upwards, we move one cell down and resume a bottom-left diagonal path. In essence, we zigzag across the matrix diagonally until every cell is traversed.

Once we finish this zigzag traversal, we'll process the list of visited cell values to uncover the indices of perfect square numbers. The diagonal_traverse_and_squares(matrix) function should implement this traversal and return a list of positions of perfect squares in the traversed sequence.

Take a 3x4 matrix, for example:

Following the diagonal traversal, we get the list: [1, 5, 2, 3, 6, 9, 10, 7, 4, 8, 11, 12]. From this list, 1, 9, and 4 are the perfect squares found at the 1st, 6th, and 9th positions. Thus, our function returns: [0, 5, 8].

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal