Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- QUESTION
- Prompt: Write a function that takes the input, gives the output, and meets the conditions below.
- Input: An N x M matrix of a garden. Each cell contains a positive integer representing the number of carrots in that part of the garden.
- Output: The number of carrots Bunny eats before falling asleep.
- Conditions: Bunny starts in the center of the garden. If there are more than one center cell, Bunny starts in the cell with the largest number of carrots. There will never be a tie for the highest number of carrots in a center cell. Bunny eats all of the carrots in his cell, then looks left, right, up, and down for more carrots. Bunny always moves to the adjacent cell with the highest carrot count. If there are no adjacent cells with carrots, Bunny falls asleep.
- Example test cases:
- >>> garden1 = [[5, 7, 8, 6, 3],
- [0, 0, 7, 0, 4],
- [4, 6, 3, 4, 9],
- [3, 1, 0, 5, 8]]
- ==========================================================================================
- // assumption: 1. arrays are of same/equal length
- // 2. bunny will walk l, r, u and down
- // look for center of garden
- // if garden has even number of middle cells
- // calcuate length of array
- //pick the middle cell for the first array
- // pick the middle cell for the second array
- // else if garden has odd number of middle cells
- //count total number of arrays in garden
- //select the middle array
- //pick the middle cell of the array
- //now we have a starting point (helper function)
- //eat the carrots at current cell
- //increment carrots variable with value at current position
- //change/set the value to 0
- //look for next spot in garden (helper function)
- //have a dictionary keep track of carrots and their positions
- //look r,l,u,d from current position and store values in dict.
- //pick position with highest value
- //call helper fn
- ============================================================================================
- const whiteRabbit = (garden) => {
- // init variables
- let isOdd
- let midArr
- let midVal
- // check for dimenions
- garden.length % 2 === 0 ? isOdd = false : isOdd = true
- if (!isOdd) {
- // look at the highest value in the middle two arrays
- } else {
- // find the middle array
- midArr = garden[Math.floor(garden.length / 2)]
- // find the middle value
- midVal = midArr[Math.floor(midArr.length / 2)]
- // call munch()
- }
- }
- const munch = (index, garden) => {
- let carrotsEaten = 0 // incase we want to return no. of carrots consumed
- }
- // an odd garden
- const garden1 =
- [[5, 7, 8, 6, 3],
- [0, 0, 7, 0, 4],
- [4, 6, 3, 4, 9],
- [3, 1, 0, 5, 8]]
- // an even garden
- const garden2 =
- [[5, 7, 8, 6, 3],
- [0, 0, 7, 0, 4],
- [4, 6, 3, 4, 9],
- [3, 1, 0, 5, 8],
- [5, 7, 8, 6, 3]]
- console.log(whiteRabbit(garden2))
Add Comment
Please, Sign In to add comment