Battleship Hit Detector
Function Name: isShipHit
Description
In the game of Battleship, players try to guess the locations of each other's ships on a grid. Given a grid with the location of a single ship marked with 'S' and the rest of the cells filled with 'O', write a function that determines if a given shot hits the ship. The grid will be represented as a 2D array, and the function will take the grid and coordinates of the shot. The function should return 'Hit' if the coordinates have an 'S', otherwise, return 'Miss'.
Requirements
- The function should take three parameters: a 2D array 'grid' representing the game board, an integer 'x' for the row coordinate, and an integer 'y' for the column coordinate.
- Assume the grid is a square of size N x N, where N is in the range of 5 to 10.
- Coordinates will be zero-indexed, meaning (0, 0) is the top left corner of the grid.
- The function should return a string 'Hit' or 'Miss'.
- The board will contain only the characters 'S' and 'O', representing a ship or ocean respectively.
Examples
Let's say our grid is represented as follows, where 'S' marks parts of the ship: [['O', 'O', 'S', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'S', 'O', 'O'], ['O', 'O', 'S', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]Calling isShipHit(grid, 0, 2) should return 'Hit' because grid[0][2] contains an 'S'.Calling isShipHit(grid, 4, 4) should return 'Miss' because grid[4][4] contains an 'O'.
Circular Queue Implementation
Sat Aug 24 2024
Create a CircularQueue class that represents a circular queue for integers with a fixed size.It should support enqueue (add an item), dequeue (remove and return an item), and isEmpty methods.Additionally, the CircularQueue class should have a method to return the current size of the queue.
Custom Stack Min-Element
Mon Aug 26 2024
Write a function that, when implemented, operates a stack data structure allowing push and pop operations, but also includes a function to retrieve the smallest element in the stack at any given time. The stack should only store integers.The getMinFromStack function should work in constant time and with O(1) memory, meaning you should not use any additional data structures to keep track of the minimum element.Push and pop operations should also work in constant time (O(1)). This will require designing the stack in such a way that it can keep track of the minimum element as elements are pushed and popped.