Magic Square Checker
Function Name: isMagicSquare
Description
A magic square is a grid of numbers where the sum of the numbers in each row, column, and diagonal are all equal. Write a function isMagicSquare that accepts a two-dimensional array (n x n) representing a square matrix, and determines if the matrix is a magic square. The function should return a boolean value - true if the matrix is a magic square, false otherwise. An empty square or a non-square matrix should return false.
Requirements
- The function must accept a single parameter - a two-dimensional array representing the square matrix.
- The matrix may contain any positive integer.
- The function must handle matrices of any size, but can assume that the input is always a square (i.e., the number of rows is equal to the number of columns).
- An empty square or a non-square matrix should return false.
- Do not use any built-in functions for calculating sums or checking matrix properties.
Examples
Given a matrix: [[2, 7, 6], [9, 5, 1], [4, 3, 8]], the function should return true because it is a magic square.Given a matrix: [[16, 23, 17], [78, 32, 21], [17, 16, 15]], the function should return false because it is not a magic square.
Flatten Nested List
Wed Dec 04 2024
Write a function 'flattenList' that takes a single parameter which is a nested list with an unknown level of nesting (i.e., a list where some of the elements can be lists themselves). The function should flatten the nested list, that is, return a single list containing all the elements in the nested structure in their original order, without any nested lists.The function should handle various data types within the nested list, including integers, strings, and other lists. However, the flattened list only needs to preserve the order and the type of the elements; no other operations are required.The input list can have multiple levels of nested lists, but you can assume there are no circular references.Do not use any libraries for this function and ensure that it is written to handle recursive list structures.
Pathfinder
Fri Dec 06 2024
Implement a function 'findShortestPath' that takes a grid (2D array) consisting of '0's (empty spaces), '1's (walls), 'S' (start), and 'E' (end).The function should return an integer representing the length of the shortest path from 'S' to 'E' that only passes through empty spaces ('0').If no path exists, the function should return -1.The path can only move horizontally or vertically, not diagonally.