From b9998a6e8cf494f78faebe780f33fb22d7cf370c Mon Sep 17 00:00:00 2001 From: Kasun Thennakoon Date: Thu, 14 Jul 2022 10:19:07 +0530 Subject: [PATCH 1/3] Wrapping the example matrix with outer array Without the outer array , given arrays are passed as 4 arguments to the `island` function --- Graphs/NumberOfIslands.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Graphs/NumberOfIslands.js b/Graphs/NumberOfIslands.js index 84b666d373..5d8880a4ad 100644 --- a/Graphs/NumberOfIslands.js +++ b/Graphs/NumberOfIslands.js @@ -81,8 +81,10 @@ const islands = (matrixGrid) => { export { islands } // islands( -// ['1', '1', '0', '0', '0'], -// ['1', '1', '0', '0', '0'], -// ['0', '0', '1', '0', '0'], -// ['0', '0', '0', '1', '1'] +// [ +// ['1', '1', '0', '0', '0'], +// ['1', '1', '0', '0', '0'], +// ['0', '0', '1', '0', '0'], +// ['0', '0', '0', '1', '1'] +// ] // ) From 959aeeeb1035e554c0e4caf08cffc7d5b1a5678b Mon Sep 17 00:00:00 2001 From: Kasun Thennakoon Date: Tue, 19 Jul 2022 09:33:40 +0530 Subject: [PATCH 2/3] add test cases for NumberOfIslands --- Graphs/NumberOfIslands.js | 9 -------- Graphs/test/NumberOfIslands.test.js | 34 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 Graphs/test/NumberOfIslands.test.js diff --git a/Graphs/NumberOfIslands.js b/Graphs/NumberOfIslands.js index 5d8880a4ad..62891a4814 100644 --- a/Graphs/NumberOfIslands.js +++ b/Graphs/NumberOfIslands.js @@ -79,12 +79,3 @@ const islands = (matrixGrid) => { } export { islands } - -// islands( -// [ -// ['1', '1', '0', '0', '0'], -// ['1', '1', '0', '0', '0'], -// ['0', '0', '1', '0', '0'], -// ['0', '0', '0', '1', '1'] -// ] -// ) diff --git a/Graphs/test/NumberOfIslands.test.js b/Graphs/test/NumberOfIslands.test.js new file mode 100644 index 0000000000..1e106a2654 --- /dev/null +++ b/Graphs/test/NumberOfIslands.test.js @@ -0,0 +1,34 @@ +import { islands } from '../NumberOfIslands' + +describe('Number of Islands', () => { + test('Test Case 1', () => { + const graph = [ + ['1', '1', '0', '0', '0'], + ['1', '1', '0', '0', '0'], + ['0', '0', '1', '0', '0'], + ['0', '0', '0', '1', '1'] + ] + const result = islands(graph) + expect(result).toBe(3) + }) + + test('Test Case 2', () => { + const graph = [ + ['1', '1'], + ['1', '1'], + ['0', '0'], + ['0', '0'] + ] + const result = islands(graph) + expect(result).toBe(1) + }) + + test('Test Case 3', () => { + const graph = [ + ['0', '0'], + ['0', '0'] + ] + const result = islands(graph) + expect(result).toBe(0) + }) +}) From ef9777788908082942f0725b0bf6161805fb4284 Mon Sep 17 00:00:00 2001 From: Kasun Thennakoon Date: Mon, 1 Aug 2022 19:51:46 +0530 Subject: [PATCH 3/3] Address review comments --- Graphs/test/NumberOfIslands.test.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/Graphs/test/NumberOfIslands.test.js b/Graphs/test/NumberOfIslands.test.js index 1e106a2654..3beb1da75a 100644 --- a/Graphs/test/NumberOfIslands.test.js +++ b/Graphs/test/NumberOfIslands.test.js @@ -1,34 +1,31 @@ import { islands } from '../NumberOfIslands' describe('Number of Islands', () => { - test('Test Case 1', () => { + test('Graph with three islands', () => { const graph = [ ['1', '1', '0', '0', '0'], ['1', '1', '0', '0', '0'], ['0', '0', '1', '0', '0'], ['0', '0', '0', '1', '1'] ] - const result = islands(graph) - expect(result).toBe(3) + expect(islands(graph)).toBe(3) }) - test('Test Case 2', () => { + test('Graph with only one island', () => { const graph = [ ['1', '1'], ['1', '1'], ['0', '0'], ['0', '0'] ] - const result = islands(graph) - expect(result).toBe(1) + expect(islands(graph)).toBe(1) }) - test('Test Case 3', () => { + test('No islands', () => { const graph = [ ['0', '0'], ['0', '0'] ] - const result = islands(graph) - expect(result).toBe(0) + expect(islands(graph)).toBe(0) }) })