Matrix Spiral Copy
Function Name: spiralCopy
Description
Write a function that takes a 2D array (matrix) as its parameter and returns a list of its elements in a spiral order.
You should start from the top-left corner of the matrix, proceed to the right, and continue spiraling into the center of the matrix.
The spiral order should proceed with the pattern: right -> down -> left -> up, repeating until all elements have been visited.
Requirements
- The function should be named spiralCopy.
- The function must take one parameter, a 2D array (matrix).
- Assume the matrix is non-empty and consists of only numerical elements.
- The function should return a list containing the elements of the matrix in spiral order.
- Do not use any third-party libraries or modules.
- Optimize for readability and efficiency.
Examples
Given the following 2D array as input:[ [ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12], [13, 14, 15, 16]]The function spiralCopy should return the list:[1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10]
Links
Rainwater Trapping
Wed Oct 09 2024
Write a function called trapRainwater that calculates how much rainwater can be trapped between the given heights of bars. You are given an array that represents an elevation map where the width of each bar is 1. The function should compute how much water is trapped after raining. Bars of width 1 and non-negative integers are represented in the array where each value represents the height of the bar at that position.
String Permutations
Fri Oct 11 2024
Write a function that takes a single string parameter and returns all possible permutations of the characters in the string.The string will contain only unique characters and will not be empty.The permutations can be returned in any order.If the string is a single character, the function should return an array containing just that character.