Guest User

Untitled

a guest
Jul 16th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. QUESTION
  2. Prompt: Write a function that takes the input, gives the output, and meets the conditions below.
  3.  
  4. 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.
  5.  
  6. Output: The number of carrots Bunny eats before falling asleep.
  7.  
  8. 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.
  9.  
  10. Example test cases:
  11. >>> garden1 = [[5, 7, 8, 6, 3],
  12. [0, 0, 7, 0, 4],
  13. [4, 6, 3, 4, 9],
  14. [3, 1, 0, 5, 8]]
  15.  
  16. ==========================================================================================
  17. // assumption: 1. arrays are of same/equal length
  18. // 2. bunny will walk l, r, u and down
  19.  
  20. // look for center of garden
  21. // if garden has even number of middle cells
  22. // calcuate length of array
  23. //pick the middle cell for the first array
  24. // pick the middle cell for the second array
  25. // else if garden has odd number of middle cells
  26. //count total number of arrays in garden
  27. //select the middle array
  28. //pick the middle cell of the array
  29.  
  30. //now we have a starting point (helper function)
  31.  
  32. //eat the carrots at current cell
  33. //increment carrots variable with value at current position
  34. //change/set the value to 0
  35.  
  36. //look for next spot in garden (helper function)
  37.  
  38. //have a dictionary keep track of carrots and their positions
  39. //look r,l,u,d from current position and store values in dict.
  40. //pick position with highest value
  41. //call helper fn
  42.  
  43. ============================================================================================
  44. const whiteRabbit = (garden) => {
  45. // init variables
  46. let isOdd
  47. let midArr
  48. let midVal
  49.  
  50.  
  51. // check for dimenions
  52. garden.length % 2 === 0 ? isOdd = false : isOdd = true
  53.  
  54. if (!isOdd) {
  55. // look at the highest value in the middle two arrays
  56. } else {
  57. // find the middle array
  58. midArr = garden[Math.floor(garden.length / 2)]
  59. // find the middle value
  60. midVal = midArr[Math.floor(midArr.length / 2)]
  61. // call munch()
  62. }
  63.  
  64.  
  65. }
  66.  
  67. const munch = (index, garden) => {
  68. let carrotsEaten = 0 // incase we want to return no. of carrots consumed
  69.  
  70.  
  71. }
  72.  
  73. // an odd garden
  74. const garden1 =
  75. [[5, 7, 8, 6, 3],
  76. [0, 0, 7, 0, 4],
  77. [4, 6, 3, 4, 9],
  78. [3, 1, 0, 5, 8]]
  79.  
  80. // an even garden
  81. const garden2 =
  82. [[5, 7, 8, 6, 3],
  83. [0, 0, 7, 0, 4],
  84. [4, 6, 3, 4, 9],
  85. [3, 1, 0, 5, 8],
  86. [5, 7, 8, 6, 3]]
  87.  
  88.  
  89. console.log(whiteRabbit(garden2))
Add Comment
Please, Sign In to add comment