Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Problem: You have been given a contract by a water company to scout out
  3.  * new water sources for pumping. They need you to write a function that takes in
  4.  * a 2D-bit array and returns an integer N representing the largest body of water found
  5.  * in the 2D-bit array.
  6.  *
  7.  * 1 = Water
  8.  * 0 = Land
  9.  *
  10.  * A body of water is represented by a cluster of connecting 1's.
  11.  * Water can connect up, down, left, or right, but cannot connect diagonally.
  12.  *
  13.  * Ex:
  14.  * [1, 1, 0, 0],
  15.  * [1, 1, 0, 0],
  16.  * [0, 0, 1, 1]
  17.  * [0, 0, 0, 1]
  18.  *
  19.  * The number representing the largest body of water in this 2D-bit array is 4.
  20.  *
  21.  * Ex2:
  22.  * [1, 1, 1, 1],
  23.  * [1, 0, 0, 1],
  24.  * [1, 0, 0, 1]
  25.  * [1, 1, 1, 1]
  26.  *
  27.  * The number representing the largest body of water in this 2D-bit array is 12.
  28.  */
  29.  
  30. const map1 = [
  31.   [1, 1, 0, 0],
  32.   [1, 1, 0, 0],
  33.   [0, 0, 1, 1],
  34.   [0, 0, 0, 1]
  35. ];
  36.  
  37. const map2 = [
  38.   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  39.   [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0],
  40.   [0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0],
  41.   [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0],
  42.   [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  43.   [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0],
  44.   [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0],
  45.   [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0]
  46. ];
  47.  
  48. const map3 = [
  49.   [1, 1, 1, 1, 1, 1, 1, 1],
  50.   [0, 0, 0, 0, 0, 0, 0, 1],
  51.   [1, 1, 1, 1, 1, 1, 1, 1],
  52.   [0, 1, 0, 0, 0, 0, 0, 0],
  53.   [0, 1, 0, 1, 0, 1, 1, 0],
  54.   [0, 1, 0, 1, 0, 1, 1, 0],
  55.   [0, 1, 0, 1, 0, 0, 0, 0]
  56. ];
  57.  
  58. const map4 = [
  59.   [1, 1, 1, 1, 1, 1, 1, 1],
  60.   [1, 1, 1, 1, 1, 1, 1, 1],
  61.   [1, 1, 1, 1, 1, 1, 1, 1],
  62.   [1, 1, 1, 1, 1, 1, 1, 1],
  63.   [0, 1, 0, 1, 0, 1, 1, 0],
  64.   [0, 1, 0, 1, 0, 1, 1, 0],
  65.   [0, 1, 0, 1, 0, 0, 0, 0]
  66. ];
  67.  
  68. const map5 = [
  69.   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  70.   [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0],
  71.   [0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0],
  72.   [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0],
  73.   [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  74.   [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0],
  75.   [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1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0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  95.   [0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  96.   [0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  97.   [0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  98.   [0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  99.   [0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  100. ];
  101.  
  102. const map7 = [
  103.   [1, 1, 1, 1],
  104.   [1, 0, 0, 1],
  105.   [1, 0, 0, 1],
  106.   [1, 1, 1, 1]
  107. ];
  108.  
  109. const expect = val => ({
  110.   to: {
  111.     be: val2 => {
  112.       if (val !== val2) {
  113.         throw new Error(`Expected: ${val}, Received: ${val2}`);
  114.       } else {
  115.         console.log(`${val} === ${val2}`);
  116.       }
  117.     }
  118.   }
  119. });
  120.  
  121. const driver = () => {
  122.   expect(findLargestBodyOfWater(map1)).to.be(4);
  123.   expect(findLargestBodyOfWater(map2)).to.be(16);
  124.   expect(findLargestBodyOfWater(map3)).to.be(21);
  125.   expect(findLargestBodyOfWater(map4)).to.be(42);
  126.   expect(findLargestBodyOfWater(map5)).to.be(34);
  127.   expect(findLargestBodyOfWater(map6)).to.be(33);
  128.   expect(findLargestBodyOfWater(map7)).to.be(12);
  129.   console.log("All Tests Passed!");
  130. };
  131.  
  132. const findLargestBodyOfWater = grid => {
  133.   /**
  134.    * YOUR CODE HERE
  135.    */
  136. };
  137.  
  138. driver();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement