Matrix Spiral Copy
Function Name: spiralCopy
Description
Write a function that receives a two-dimensional array (matrix) of integers and returns a list of the matrix's elements in spiral order. Spiral order should start from the top-left corner and progress clockwise until all elements have been visited.
Requirements
- The function `spiralCopy` must take a single parameter, a two-dimensional array `matrix`.
- Assume the `matrix` has at least one element.
- The resulting one-dimensional list should contain all elements of the matrix in the order they were visited.
- Work in-place, without using additional data structures to store intermediate results.
- Optimize the function for time complexity.
Examples
Given the following matrix: [[1, 2, 3], [4, 5, 6], [7, 8, 9]], Calling spiralCopy(matrix) should return [1, 2, 3, 6, 9, 8, 7, 4, 5].For the matrix: [[1, 2], [3, 4]], spiralCopy(matrix) should return [1, 2, 4, 3].
Links
Treasure Hunt Grid
Thu Nov 21 2024
Write a function named 'findTreasure' that navigates through a grid to find a treasure. The grid is represented as a two-dimensional array, where each cell contains a clue to the next cell's position. Each clue is a string in the format 'x,y', indicating the row and column indices of the next cell to check. The treasure is found when a cell points to itself. The function should return the path taken as a list of coordinates (each coordinate being a pair of integers), starting from the top-left cell (0,0). If the treasure cannot be found after visiting all cells, the function should return an empty list.
Elevator Simulation
Sat Nov 23 2024
Write a function that simulates the basic operation of an elevator dispatch system. The function's goal is to determine which elevator should be sent to a requested floor based on the current status of each elevator and a series of pick-up requests.Each elevator's status and the pick-up requests are defined by two parameters: a list of elevator objects with their current floor and direction of movement ('up', 'down', or 'idle'), and a list of pick-up requests as pairs (tuples) of integers representing the requested floor and desired direction ('1' for up and '-1' for down).The function must select the most appropriate elevator for each request based on proximity and direction. If multiple elevators are equally suitable, prioritize the one with the lower index in the elevator list.