From afa8dca1a18f0496f97724ed46b88730ffc4579c Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 30 Oct 2026 13:27:31 +0200 Subject: [PATCH 1/2] Improved tasks 37, 382, 384, 398 --- .../s0037_sudoku_solver/SolutionTest.java | 56 +++++++++++++++++++ .../SolutionTest.java | 44 +++++++++++++++ .../s0384_shuffle_an_array/SolutionTest.java | 19 +++++++ .../s0398_random_pick_index/SolutionTest.java | 29 ++++++++++ 4 files changed, 148 insertions(+) diff --git a/src/test/java/g0001_0100/s0037_sudoku_solver/SolutionTest.java b/src/test/java/g0001_0100/s0037_sudoku_solver/SolutionTest.java index 69be188de..4455cb4f6 100644 --- a/src/test/java/g0001_0100/s0037_sudoku_solver/SolutionTest.java +++ b/src/test/java/g0001_0100/s0037_sudoku_solver/SolutionTest.java @@ -33,4 +33,60 @@ void solveSudoku() { new Solution().solveSudoku(board); assertThat(board, equalTo(expected)); } + + @Test + void solveSudoku2() { + char[][] expected = { + {'5', '3', '4', '6', '7', '8', '9', '1', '2'}, + {'6', '7', '2', '1', '9', '5', '3', '4', '8'}, + {'1', '9', '8', '3', '4', '2', '5', '6', '7'}, + {'8', '5', '9', '7', '6', '1', '4', '2', '3'}, + {'4', '2', '6', '8', '5', '3', '7', '9', '1'}, + {'7', '1', '3', '9', '2', '4', '8', '5', '6'}, + {'9', '6', '1', '5', '3', '7', '2', '8', '4'}, + {'2', '8', '7', '4', '1', '9', '6', '3', '5'}, + {'3', '4', '5', '2', '8', '6', '1', '7', '9'} + }; + char[][] board = { + {'5', '3', '4', '6', '7', '8', '9', '1', '2'}, + {'6', '7', '2', '1', '9', '5', '3', '4', '8'}, + {'1', '9', '8', '3', '4', '2', '5', '6', '7'}, + {'8', '5', '9', '7', '6', '1', '4', '2', '3'}, + {'4', '2', '6', '8', '5', '3', '7', '9', '1'}, + {'7', '1', '3', '9', '2', '4', '8', '5', '6'}, + {'9', '6', '1', '5', '3', '7', '2', '8', '4'}, + {'2', '8', '7', '4', '1', '9', '6', '3', '5'}, + {'3', '4', '5', '2', '8', '6', '1', '7', '9'} + }; + new Solution().solveSudoku(board); + assertThat(board, equalTo(expected)); + } + + @Test + void solveSudoku3() { + char[][] expected = { + {'5', '3', '4', '6', '7', '8', '9', '1', '2'}, + {'6', '7', '2', '1', '9', '5', '3', '4', '8'}, + {'1', '9', '8', '3', '4', '2', '5', '6', '7'}, + {'8', '5', '9', '7', '6', '1', '4', '2', '3'}, + {'4', '2', '6', '8', '5', '3', '7', '9', '1'}, + {'7', '1', '3', '9', '2', '4', '8', '5', '6'}, + {'9', '6', '1', '5', '3', '7', '2', '8', '4'}, + {'2', '8', '7', '4', '1', '9', '6', '3', '5'}, + {'3', '4', '5', '2', '8', '6', '1', '7', '9'} + }; + char[][] board = { + {'.', '.', '.', '.', '.', '.', '.', '.', '.'}, + {'6', '.', '2', '1', '9', '5', '3', '4', '8'}, + {'1', '9', '.', '3', '4', '2', '5', '6', '7'}, + {'8', '5', '9', '.', '6', '1', '4', '2', '3'}, + {'4', '2', '6', '8', '.', '3', '7', '9', '1'}, + {'7', '1', '3', '9', '2', '.', '8', '5', '6'}, + {'9', '6', '1', '5', '3', '7', '.', '8', '4'}, + {'2', '8', '7', '4', '1', '9', '6', '.', '5'}, + {'3', '4', '5', '2', '8', '6', '1', '7', '.'} + }; + new Solution().solveSudoku(board); + assertThat(board, equalTo(expected)); + } } diff --git a/src/test/java/g0301_0400/s0382_linked_list_random_node/SolutionTest.java b/src/test/java/g0301_0400/s0382_linked_list_random_node/SolutionTest.java index 5cf601f6c..41dd1f6bf 100644 --- a/src/test/java/g0301_0400/s0382_linked_list_random_node/SolutionTest.java +++ b/src/test/java/g0301_0400/s0382_linked_list_random_node/SolutionTest.java @@ -32,4 +32,48 @@ void solutionTest() { } assertThat(contain, equalTo(true)); } + + @Test + void solutionTest2() { + List result = new ArrayList<>(); + ListNode listNode = new ListNode(7); + Solution solution = new Solution(listNode); + result.add(solution.getRandom()); + result.add(solution.getRandom()); + result.add(solution.getRandom()); + result.add(solution.getRandom()); + result.add(solution.getRandom()); + List expected = new ArrayList<>(Arrays.asList(7, 7, 7, 7, 7)); + assertThat(result, equalTo(expected)); + } + + @Test + void solutionTest3() { + List result = new ArrayList<>(); + ListNode listNode4 = new ListNode(0); + ListNode listNode3 = new ListNode(-5, listNode4); + ListNode listNode2 = new ListNode(10, listNode3); + ListNode listNode = new ListNode(-1, listNode2); + Solution solution = new Solution(listNode); + for (int i = 0; i < 1000; i++) { + result.add(solution.getRandom()); + } + boolean contain = true; + List expected = new ArrayList<>(Arrays.asList(-1, 10, -5, 0)); + for (Integer i : result) { + if (!expected.contains(i)) { + contain = false; + break; + } + } + boolean allReturned = true; + for (Integer value : expected) { + if (!result.contains(value)) { + allReturned = false; + break; + } + } + assertThat(contain, equalTo(true)); + assertThat(allReturned, equalTo(true)); + } } diff --git a/src/test/java/g0301_0400/s0384_shuffle_an_array/SolutionTest.java b/src/test/java/g0301_0400/s0384_shuffle_an_array/SolutionTest.java index 4cf2a3160..07d657191 100644 --- a/src/test/java/g0301_0400/s0384_shuffle_an_array/SolutionTest.java +++ b/src/test/java/g0301_0400/s0384_shuffle_an_array/SolutionTest.java @@ -15,4 +15,23 @@ void solutionTest() { assertThat(shuffled, equalTo(new int[] {1, 2, 3})); assertThat(solution.reset(), equalTo(new int[] {1, 2, 3})); } + + @Test + void solutionTest2() { + Solution solution = new Solution(new int[] {5}); + int[] shuffled = solution.shuffle(); + assertThat(shuffled, equalTo(new int[] {5})); + assertThat(solution.reset(), equalTo(new int[] {5})); + } + + @Test + void solutionTest3() { + Solution solution = new Solution(new int[] {4, -1, 4, 0, -7}); + for (int i = 0; i < 100; i++) { + int[] shuffled = solution.shuffle(); + Arrays.sort(shuffled); + assertThat(shuffled, equalTo(new int[] {-7, -1, 0, 4, 4})); + } + assertThat(solution.reset(), equalTo(new int[] {4, -1, 4, 0, -7})); + } } diff --git a/src/test/java/g0301_0400/s0398_random_pick_index/SolutionTest.java b/src/test/java/g0301_0400/s0398_random_pick_index/SolutionTest.java index 5b3589042..8bd79b8c5 100644 --- a/src/test/java/g0301_0400/s0398_random_pick_index/SolutionTest.java +++ b/src/test/java/g0301_0400/s0398_random_pick_index/SolutionTest.java @@ -4,6 +4,9 @@ import static org.hamcrest.MatcherAssert.assertThat; import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import org.junit.jupiter.api.Test; class SolutionTest { @@ -15,4 +18,30 @@ void pick() { assertThat(solution.pick(1), equalTo(0)); assertThat(Arrays.asList(2, 3, 4).contains(solution.pick(3)), equalTo(true)); } + + @Test + void pick2() { + int[] input = new int[] {7, -2, 5, 0}; + Solution solution = new Solution(input); + assertThat(solution.pick(7), equalTo(0)); + assertThat(solution.pick(-2), equalTo(1)); + assertThat(solution.pick(5), equalTo(2)); + assertThat(solution.pick(0), equalTo(3)); + } + + @Test + void pick3() { + int[] input = new int[] {4, 1, 4, 2, 4, 4}; + Solution solution = new Solution(input); + List expected = Arrays.asList(0, 2, 4, 5); + Set picked = new HashSet<>(); + for (int i = 0; i < 1000; i++) { + int index = solution.pick(4); + assertThat(expected.contains(index), equalTo(true)); + picked.add(index); + } + assertThat(picked, equalTo(new HashSet<>(expected))); + assertThat(solution.pick(1), equalTo(1)); + assertThat(solution.pick(2), equalTo(3)); + } } From ff8954ff42b7f6b21063676d1cfd581d4e587297 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 30 Oct 2026 13:35:34 +0200 Subject: [PATCH 2/2] Fixed sonar --- .../g0301_0400/s0398_random_pick_index/SolutionTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/g0301_0400/s0398_random_pick_index/SolutionTest.java b/src/test/java/g0301_0400/s0398_random_pick_index/SolutionTest.java index 8bd79b8c5..7531104aa 100644 --- a/src/test/java/g0301_0400/s0398_random_pick_index/SolutionTest.java +++ b/src/test/java/g0301_0400/s0398_random_pick_index/SolutionTest.java @@ -12,7 +12,7 @@ class SolutionTest { @Test void pick() { - int[] input = new int[] {1, 2, 3, 3, 3}; + int[] input = {1, 2, 3, 3, 3}; Solution solution = new Solution(input); assertThat(Arrays.asList(2, 3, 4).contains(solution.pick(3)), equalTo(true)); assertThat(solution.pick(1), equalTo(0)); @@ -21,7 +21,7 @@ void pick() { @Test void pick2() { - int[] input = new int[] {7, -2, 5, 0}; + int[] input = {7, -2, 5, 0}; Solution solution = new Solution(input); assertThat(solution.pick(7), equalTo(0)); assertThat(solution.pick(-2), equalTo(1)); @@ -31,7 +31,7 @@ void pick2() { @Test void pick3() { - int[] input = new int[] {4, 1, 4, 2, 4, 4}; + int[] input = {4, 1, 4, 2, 4, 4}; Solution solution = new Solution(input); List expected = Arrays.asList(0, 2, 4, 5); Set picked = new HashSet<>();