From f055e5a627493a772b7e1758c481ba31762ce097 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 26 Sep 2017 03:24:34 +0300 Subject: [PATCH 1/7] init commit --- Spiral/.gitignore | 49 ++++++ Spiral/.idea/compiler.xml | 16 ++ Spiral/.idea/misc.xml | 13 ++ Spiral/.idea/modules.xml | 8 + Spiral/SpiralMatrix.iml | 20 +++ Spiral/pom.xml | 38 +++++ .../kazakov/SpiralMatrix/SpiralMatrix.java | 101 ++++++++++++ .../SpiralMatrix/SpiralMatrixTest.java | 151 ++++++++++++++++++ 8 files changed, 396 insertions(+) create mode 100644 Spiral/.gitignore create mode 100644 Spiral/.idea/compiler.xml create mode 100644 Spiral/.idea/misc.xml create mode 100644 Spiral/.idea/modules.xml create mode 100644 Spiral/SpiralMatrix.iml create mode 100644 Spiral/pom.xml create mode 100644 Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrix.java create mode 100644 Spiral/src/test/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrixTest.java diff --git a/Spiral/.gitignore b/Spiral/.gitignore new file mode 100644 index 0000000..345e61a --- /dev/null +++ b/Spiral/.gitignore @@ -0,0 +1,49 @@ +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties diff --git a/Spiral/.idea/compiler.xml b/Spiral/.idea/compiler.xml new file mode 100644 index 0000000..f76440d --- /dev/null +++ b/Spiral/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Spiral/.idea/misc.xml b/Spiral/.idea/misc.xml new file mode 100644 index 0000000..56c64d0 --- /dev/null +++ b/Spiral/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/Spiral/.idea/modules.xml b/Spiral/.idea/modules.xml new file mode 100644 index 0000000..97c24d4 --- /dev/null +++ b/Spiral/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Spiral/SpiralMatrix.iml b/Spiral/SpiralMatrix.iml new file mode 100644 index 0000000..7fb4c39 --- /dev/null +++ b/Spiral/SpiralMatrix.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Spiral/pom.xml b/Spiral/pom.xml new file mode 100644 index 0000000..6592802 --- /dev/null +++ b/Spiral/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + ru.spbau.mit.kazakov.SpiralMatrix + SpiralMatrix + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + jar + + + + junit + junit + 4.0 + test + + + org.junit.jupiter + junit-jupiter-api + RELEASE + + + + \ No newline at end of file diff --git a/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrix.java b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrix.java new file mode 100644 index 0000000..6962fcc --- /dev/null +++ b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrix.java @@ -0,0 +1,101 @@ +package ru.spbau.mit.kazakov.SpiralMatrix; + +import java.util.Arrays; +import java.util.Comparator; + + +/** + * Stores transpose square matrix with odd dimensions, sorts matrix's columns by first elements, prints matrix spirally. + */ +public class SpiralMatrix { + private int[][] matrix; + + private enum Direction {RIGHT, UP, LEFT, DOWN} + + /** + * Initialize matrix with given data. + * + * @param data initializing matrix + */ + public SpiralMatrix(int[][] data) { + matrix = new int[data.length][data.length]; + + for (int i = 0; i < data.length; i++) { + for (int j = 0; j < data.length; j++) { + matrix[i][j] = data[j][i]; + } + } + } + + /** + * Prints matrix: each element separated by space symbol, each row separated by new line symbol. + */ + public void printMatrix() { + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix.length; j++) { + System.out.print(matrix[j][i] + " "); + } + System.out.println(); + } + } + + /** + * Sorts columns by first elements. + */ + public void sortMatrix() { + Arrays.sort(matrix, Comparator.comparingInt(col -> col[0])); + } + + /** + * Prints matrix in spiral order. Elements separated by space symbol. + */ + public void printSpiralMatrix() { + int steps = 0; + int iCurrent = matrix.length / 2; + int jCurrent = matrix.length / 2; + + while (true) { + for (Direction direction : Direction.values()) { + switch (direction) { + case RIGHT: + steps++; + for (int step = 1; step <= steps; step++) { + System.out.print(matrix[iCurrent][jCurrent] + " "); + iCurrent++; + } + + if (steps == matrix.length) { + return; + } + break; + + case UP: + for (int step = 1; step <= steps; step++) { + System.out.print(matrix[iCurrent][jCurrent] + " "); + jCurrent--; + } + break; + + case LEFT: + steps++; + for (int step = 1; step <= steps; step++) { + System.out.print(matrix[iCurrent][jCurrent] + " "); + iCurrent--; + } + + if (steps == matrix.length) { + return; + } + break; + + case DOWN: + for (int step = 1; step <= steps; step++) { + System.out.print(matrix[iCurrent][jCurrent] + " "); + jCurrent++; + } + break; + } + } + } + } +} diff --git a/Spiral/src/test/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrixTest.java b/Spiral/src/test/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrixTest.java new file mode 100644 index 0000000..7d344a6 --- /dev/null +++ b/Spiral/src/test/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrixTest.java @@ -0,0 +1,151 @@ +package ru.spbau.mit.kazakov.SpiralMatrix; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class SpiralMatrixTest { + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + + @Before + public void setUpStreams() { + System.setOut(new PrintStream(outContent)); + } + + @After + public void cleanUpStreams() { + System.setOut(null); + } + + @Test + public void testConstructor() { + int[][] matrix = new int[][]{{3, 1, 5}, + {54, 43, 32}, + {21, 53, 76}}; + SpiralMatrix spiral = new SpiralMatrix(matrix); + } + + @Test + public void testPrintMatrix3x3() { + int[][] matrix = new int[][]{{3, 1, 5}, + {54, 43, 32}, + {21, 53, 76}}; + + SpiralMatrix spiral = new SpiralMatrix(matrix); + spiral.printMatrix(); + + assertEquals("3 1 5 \n54 43 32 \n21 53 76", outContent.toString().trim()); + } + + @Test + public void testPrintMatrix15x5() { + int[][] matrix = new int[][]{{3, 1, 5, -1, 0}, + {54, 43, 32, 3, 2}, + {21, 53, 76, 7, 2}, + {21, 87, 32, 98, 23}, + {1, 4, 6, 4, 3}}; + + SpiralMatrix spiral = new SpiralMatrix(matrix); + spiral.printMatrix(); + + assertEquals("3 1 5 -1 0 \n" + + "54 43 32 3 2 \n" + + "21 53 76 7 2 \n" + + "21 87 32 98 23 \n" + + "1 4 6 4 3", outContent.toString().trim()); + } + + @Test + public void testPrintMatrix1x1() { + int[][] matrix = new int[][]{{3}}; + + SpiralMatrix spiral = new SpiralMatrix(matrix); + spiral.printMatrix(); + + assertEquals("3", outContent.toString().trim()); + } + + @Test + public void testSortMatrix3x3() { + int[][] matrix = new int[][]{{3, 1, 5}, + {54, 43, 32}, + {21, 53, 76}}; + + SpiralMatrix spiral = new SpiralMatrix(matrix); + spiral.sortMatrix(); + spiral.printMatrix(); + + assertEquals("1 3 5 \n43 54 32 \n53 21 76", outContent.toString().trim()); + } + + @Test + public void testPrintSortMatrix15x5() { + int[][] matrix = new int[][]{{3, 1, 5, -1, 0}, + {54, 43, 32, 3, 2}, + {21, 53, 76, 7, 2}, + {21, 87, 32, 98, 23}, + {1, 4, 6, 4, 3}}; + + SpiralMatrix spiral = new SpiralMatrix(matrix); + spiral.sortMatrix(); + spiral.printMatrix(); + + assertEquals("-1 0 1 3 5 \n" + + "3 2 43 54 32 \n" + + "7 2 53 21 76 \n" + + "98 23 87 21 32 \n" + + "4 3 4 1 6", outContent.toString().trim()); + } + + @Test + public void testSortMatrix1x1() { + int[][] matrix = new int[][]{{3}}; + + SpiralMatrix spiral = new SpiralMatrix(matrix); + spiral.sortMatrix(); + spiral.printMatrix(); + + assertEquals("3", outContent.toString().trim()); + } + + @Test + public void testPrintSpiralMatrix13x3() { + int[][] matrix = new int[][]{{3, 1, 5}, + {54, 43, 32}, + {21, 53, 76}}; + + SpiralMatrix spiral = new SpiralMatrix(matrix); + spiral.printSpiralMatrix(); + + assertEquals("43 32 5 1 3 54 21 53 76", outContent.toString().trim()); + } + + @Test + public void testPrintSpiralMatrix15x5() { + int[][] matrix = new int[][]{{3, 1, 5, 6, 7}, + {54, 43, 32, 3, 2}, + {21, 53, 76, 7, 2}, + {21, 87, 32, 98, 23}, + {1, 4, 6, 4, 3}}; + + SpiralMatrix spiral = new SpiralMatrix(matrix); + spiral.printSpiralMatrix(); + + assertEquals("76 7 3 32 43 53 87 32 98 23 2 2 7 6 5 1 3 54 21 21 1 4 6 4 3", outContent.toString().trim()); + } + + @Test + public void testPrintSpiralMatrix1x1() { + int[][] matrix = new int[][]{{3}}; + + SpiralMatrix spiral = new SpiralMatrix(matrix); + spiral.printSpiralMatrix(); + + assertEquals("3", outContent.toString().trim()); + } +} \ No newline at end of file From 35da5516a6ef2d79a459a30f7703d46d461e83f1 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 16 Oct 2017 17:58:41 +0300 Subject: [PATCH 2/7] added exception situations handling --- Spiral/SpiralMatrix.iml | 6 +- Spiral/pom.xml | 5 ++ .../SpiralMatrix/EvenDimensionException.java | 4 ++ .../NotSquareMatrixException.java | 4 ++ .../SpiralMatrix/NullRowException.java | 4 ++ .../kazakov/SpiralMatrix/SpiralMatrix.java | 31 +++++++++- .../SpiralMatrix/SpiralMatrixTest.java | 58 +++++++++++++++---- 7 files changed, 96 insertions(+), 16 deletions(-) create mode 100644 Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/EvenDimensionException.java create mode 100644 Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NotSquareMatrixException.java create mode 100644 Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NullRowException.java diff --git a/Spiral/SpiralMatrix.iml b/Spiral/SpiralMatrix.iml index 7fb4c39..0c047ef 100644 --- a/Spiral/SpiralMatrix.iml +++ b/Spiral/SpiralMatrix.iml @@ -12,9 +12,9 @@ - - + - + + \ No newline at end of file diff --git a/Spiral/pom.xml b/Spiral/pom.xml index 6592802..3bde29e 100644 --- a/Spiral/pom.xml +++ b/Spiral/pom.xml @@ -33,6 +33,11 @@ junit-jupiter-api RELEASE + + org.jetbrains + annotations + 13.0 + \ No newline at end of file diff --git a/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/EvenDimensionException.java b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/EvenDimensionException.java new file mode 100644 index 0000000..28e0816 --- /dev/null +++ b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/EvenDimensionException.java @@ -0,0 +1,4 @@ +package ru.spbau.mit.kazakov.SpiralMatrix; + +public class EvenDimensionException extends Exception { +} diff --git a/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NotSquareMatrixException.java b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NotSquareMatrixException.java new file mode 100644 index 0000000..9a4dcbe --- /dev/null +++ b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NotSquareMatrixException.java @@ -0,0 +1,4 @@ +package ru.spbau.mit.kazakov.SpiralMatrix; + +public class NotSquareMatrixException extends Exception { +} diff --git a/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NullRowException.java b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NullRowException.java new file mode 100644 index 0000000..7d19334 --- /dev/null +++ b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NullRowException.java @@ -0,0 +1,4 @@ +package ru.spbau.mit.kazakov.SpiralMatrix; + +public class NullRowException extends Exception { +} diff --git a/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrix.java b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrix.java index 6962fcc..d758547 100644 --- a/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrix.java +++ b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrix.java @@ -1,5 +1,7 @@ package ru.spbau.mit.kazakov.SpiralMatrix; +import org.jetbrains.annotations.NotNull; + import java.util.Arrays; import java.util.Comparator; @@ -8,7 +10,7 @@ * Stores transpose square matrix with odd dimensions, sorts matrix's columns by first elements, prints matrix spirally. */ public class SpiralMatrix { - private int[][] matrix; + private final int[][] matrix; private enum Direction {RIGHT, UP, LEFT, DOWN} @@ -17,7 +19,8 @@ private enum Direction {RIGHT, UP, LEFT, DOWN} * * @param data initializing matrix */ - public SpiralMatrix(int[][] data) { + public SpiralMatrix(@NotNull int[][] data) throws NotSquareMatrixException, NullRowException, EvenDimensionException { + checkMatrix(data); matrix = new int[data.length][data.length]; for (int i = 0; i < data.length; i++) { @@ -27,6 +30,30 @@ public SpiralMatrix(int[][] data) { } } + /** + * @param data + * @throws NotSquareMatrixException + * @throws NullRowException + * @throws EvenDimensionException + */ + private void checkMatrix(@NotNull int[][] data) throws NotSquareMatrixException, NullRowException, EvenDimensionException { + int rows = data.length; + + if (rows % 2 != 1) { + throw new EvenDimensionException(); + } + + for (int[] col : data) { + if (col == null) { + throw new NullRowException(); + } + if (col.length != rows) { + throw new NotSquareMatrixException(); + } + } + } + + /** * Prints matrix: each element separated by space symbol, each row separated by new line symbol. */ diff --git a/Spiral/src/test/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrixTest.java b/Spiral/src/test/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrixTest.java index 7d344a6..45a4369 100644 --- a/Spiral/src/test/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrixTest.java +++ b/Spiral/src/test/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrixTest.java @@ -23,15 +23,45 @@ public void cleanUpStreams() { } @Test - public void testConstructor() { + public void testConstructor() throws NotSquareMatrixException, NullRowException, EvenDimensionException { int[][] matrix = new int[][]{{3, 1, 5}, {54, 43, 32}, {21, 53, 76}}; - SpiralMatrix spiral = new SpiralMatrix(matrix); + new SpiralMatrix(matrix); + } + + @Test(expected = EvenDimensionException.class) + public void testThrowsEvenDimensionException() throws NotSquareMatrixException, NullRowException, EvenDimensionException { + int[][] matrix = new int[][]{{3, 1}, + {54, 43}}; + new SpiralMatrix(matrix); + } + + @Test(expected = NotSquareMatrixException.class) + public void testRaggedMatrixThrowsNotSquareMatrixException() throws NotSquareMatrixException, NullRowException, EvenDimensionException { + int[][] matrix = new int[][]{{3, 1, 5}, + {54, 43}, + {21, 53, 76}}; + new SpiralMatrix(matrix); + } + + @Test(expected = NotSquareMatrixException.class) + public void testRectangleMatrixThrowsNotSquareMatrixException() throws NotSquareMatrixException, NullRowException, EvenDimensionException { + int[][] matrix = new int[][]{{3, 1, 5}, + {21, 53, 76}}; + new SpiralMatrix(matrix); + } + + @Test(expected = NullRowException.class) + public void testThrowsNullRowException() throws NotSquareMatrixException, NullRowException, EvenDimensionException { + int[][] matrix = new int[][]{{3, 1, 5}, + null, + {21, 53, 76}}; + new SpiralMatrix(matrix); } @Test - public void testPrintMatrix3x3() { + public void testPrintMatrix3x3() throws NotSquareMatrixException, NullRowException, EvenDimensionException { int[][] matrix = new int[][]{{3, 1, 5}, {54, 43, 32}, {21, 53, 76}}; @@ -43,7 +73,7 @@ public void testPrintMatrix3x3() { } @Test - public void testPrintMatrix15x5() { + public void testPrintMatrix5x5() throws NotSquareMatrixException, NullRowException, EvenDimensionException { int[][] matrix = new int[][]{{3, 1, 5, -1, 0}, {54, 43, 32, 3, 2}, {21, 53, 76, 7, 2}, @@ -61,7 +91,7 @@ public void testPrintMatrix15x5() { } @Test - public void testPrintMatrix1x1() { + public void testPrintMatrix1x1() throws NotSquareMatrixException, NullRowException, EvenDimensionException { int[][] matrix = new int[][]{{3}}; SpiralMatrix spiral = new SpiralMatrix(matrix); @@ -71,7 +101,7 @@ public void testPrintMatrix1x1() { } @Test - public void testSortMatrix3x3() { + public void testSortMatrix3x3() throws NotSquareMatrixException, NullRowException, EvenDimensionException { int[][] matrix = new int[][]{{3, 1, 5}, {54, 43, 32}, {21, 53, 76}}; @@ -84,7 +114,7 @@ public void testSortMatrix3x3() { } @Test - public void testPrintSortMatrix15x5() { + public void testPrintSortMatrix5x5() throws NotSquareMatrixException, NullRowException, EvenDimensionException { int[][] matrix = new int[][]{{3, 1, 5, -1, 0}, {54, 43, 32, 3, 2}, {21, 53, 76, 7, 2}, @@ -103,7 +133,7 @@ public void testPrintSortMatrix15x5() { } @Test - public void testSortMatrix1x1() { + public void testSortMatrix1x1() throws NotSquareMatrixException, NullRowException, EvenDimensionException { int[][] matrix = new int[][]{{3}}; SpiralMatrix spiral = new SpiralMatrix(matrix); @@ -114,7 +144,7 @@ public void testSortMatrix1x1() { } @Test - public void testPrintSpiralMatrix13x3() { + public void testPrintSpiralMatrix3x3() throws NotSquareMatrixException, NullRowException, EvenDimensionException { int[][] matrix = new int[][]{{3, 1, 5}, {54, 43, 32}, {21, 53, 76}}; @@ -126,7 +156,7 @@ public void testPrintSpiralMatrix13x3() { } @Test - public void testPrintSpiralMatrix15x5() { + public void testPrintSpiralMatrix5x5() throws NotSquareMatrixException, NullRowException, EvenDimensionException { int[][] matrix = new int[][]{{3, 1, 5, 6, 7}, {54, 43, 32, 3, 2}, {21, 53, 76, 7, 2}, @@ -140,7 +170,7 @@ public void testPrintSpiralMatrix15x5() { } @Test - public void testPrintSpiralMatrix1x1() { + public void testPrintSpiralMatrix1x1() throws NotSquareMatrixException, NullRowException, EvenDimensionException { int[][] matrix = new int[][]{{3}}; SpiralMatrix spiral = new SpiralMatrix(matrix); @@ -148,4 +178,10 @@ public void testPrintSpiralMatrix1x1() { assertEquals("3", outContent.toString().trim()); } + + @Test + public void test() { + int[][] matrix = new int[][]{{1, 2}, {4, 5, 6}}; + assertEquals(3, matrix[1].length); + } } \ No newline at end of file From 4a42a992c2e6fc7669d8c6b815a0e3c8b84f4aea Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 16 Oct 2017 22:28:42 +0300 Subject: [PATCH 3/7] init commit --- Tree/.gitignore | 49 +++++++++ Tree/.idea/compiler.xml | 16 +++ Tree/.idea/misc.xml | 13 +++ Tree/.idea/modules.xml | 8 ++ Tree/Tree.iml | 17 ++++ Tree/pom.xml | 24 +++++ .../main/java/ru/srbau/mit/kazakov/Tree.java | 99 +++++++++++++++++++ .../java/ru/srbau/mit/kazakov/TreeTest.java | 80 +++++++++++++++ 8 files changed, 306 insertions(+) create mode 100644 Tree/.gitignore create mode 100644 Tree/.idea/compiler.xml create mode 100644 Tree/.idea/misc.xml create mode 100644 Tree/.idea/modules.xml create mode 100644 Tree/Tree.iml create mode 100644 Tree/pom.xml create mode 100644 Tree/src/main/java/ru/srbau/mit/kazakov/Tree.java create mode 100644 Tree/src/test/java/ru/srbau/mit/kazakov/TreeTest.java diff --git a/Tree/.gitignore b/Tree/.gitignore new file mode 100644 index 0000000..345e61a --- /dev/null +++ b/Tree/.gitignore @@ -0,0 +1,49 @@ +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties diff --git a/Tree/.idea/compiler.xml b/Tree/.idea/compiler.xml new file mode 100644 index 0000000..8fac1a3 --- /dev/null +++ b/Tree/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Tree/.idea/misc.xml b/Tree/.idea/misc.xml new file mode 100644 index 0000000..e8942bd --- /dev/null +++ b/Tree/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/Tree/.idea/modules.xml b/Tree/.idea/modules.xml new file mode 100644 index 0000000..2691d43 --- /dev/null +++ b/Tree/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Tree/Tree.iml b/Tree/Tree.iml new file mode 100644 index 0000000..188419f --- /dev/null +++ b/Tree/Tree.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Tree/pom.xml b/Tree/pom.xml new file mode 100644 index 0000000..738fb27 --- /dev/null +++ b/Tree/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + ru.spbau.mit.kazakov + Tree + 1.0-SNAPSHOT + + + + junit + junit + 4.8 + + + org.jetbrains + annotations + 13.0 + + + + \ No newline at end of file diff --git a/Tree/src/main/java/ru/srbau/mit/kazakov/Tree.java b/Tree/src/main/java/ru/srbau/mit/kazakov/Tree.java new file mode 100644 index 0000000..c652d77 --- /dev/null +++ b/Tree/src/main/java/ru/srbau/mit/kazakov/Tree.java @@ -0,0 +1,99 @@ +package ru.srbau.mit.kazakov; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Generic binary search tree. + * + * @param type of stored data + */ +public class Tree> { + private int size = 0; + @Nullable + private Node root = null; + + /** + * Node for binary search tree. Stores left, right children and data. + */ + private class Node { + private Node left = null; + private Node right = null; + private T data; + + /** + * Initializes data with specified value. + * + * @param value to initialize with + */ + public Node(@NotNull T value) { + data = value; + } + } + + /** + * Returns number of stored elements in the tree. + */ + public int size() { + return size; + } + + /** + * Checks if there is a value equals to specified value. + * + * @param value to check + * @return true if there is such value, and false otherwise + */ + public boolean contains(@NotNull T value) { + Node current = root; + + while (current != null) { + if (current.data.compareTo(value) > 0) { + current = current.left; + } else if (current.data.compareTo(value) < 0) { + current = current.right; + } else if (current.data.compareTo(value) == 0) { + return true; + } + } + + return false; + } + + /** + * Adds new Node storing specified value to the tree. + * + * @param value to add + * @return true if there was specified value, and false otherwise + */ + public boolean add(@NotNull T value) { + if (contains(value)) + return false; + if (root == null) { + root = new Node(value); + size++; + return true; + } + + Node current = root; + Node parent = null; + while (current != null) { + parent = current; + if (current.data.compareTo(value) > 0) { + current = current.left; + } else if (current.data.compareTo(value) < 0) { + current = current.right; + } + + } + + if (parent.data.compareTo(value) < 0) { + parent.right = new Node(value); + } else if (parent.data.compareTo(value) > 0) { + parent.left = new Node(value); + } + + size++; + return true; + } +} diff --git a/Tree/src/test/java/ru/srbau/mit/kazakov/TreeTest.java b/Tree/src/test/java/ru/srbau/mit/kazakov/TreeTest.java new file mode 100644 index 0000000..acaa775 --- /dev/null +++ b/Tree/src/test/java/ru/srbau/mit/kazakov/TreeTest.java @@ -0,0 +1,80 @@ +package ru.srbau.mit.kazakov; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class TreeTest { + @Test + public void testConstructor() { + new Tree(); + } + + @Test + public void testAddNotExisting() { + Tree tree = new Tree(); + assertTrue(tree.add(false)); + assertTrue(tree.contains(false)); + } + + @Test + public void testAddExisting() { + Tree tree = new Tree(); + tree.add(1); + assertFalse(tree.add(1)); + assertTrue(tree.contains(1)); + } + + @Test + public void testAddOneHundred() { + Tree tree = new Tree(); + for (int i = 0; i < 100; i++) { + assertTrue(tree.add(i)); + } + + assertTrue(tree.contains(50)); + } + + @Test + public void sizeEmpty() { + Tree tree = new Tree(); + assertEquals(0, tree.size()); + } + + @Test + public void sizeOne() { + Tree tree = new Tree(); + tree.add("abacaba"); + assertEquals(1, tree.size()); + } + + @Test + public void testSizeOneHundred() { + Tree tree = new Tree(); + for (int i = 0; i < 100; i++) { + assertTrue(tree.add(i)); + } + + assertEquals(100, tree.size()); + } + + @Test + public void testContainsEmpty() { + Tree tree = new Tree(); + assertFalse(tree.contains('z')); + } + + @Test + public void testContainsNotExisting() { + Tree tree = new Tree(); + tree.add('a'); + assertFalse(tree.contains('z')); + } + + @Test + public void testContainsExisting() { + Tree tree = new Tree(); + tree.add('a'); + assertTrue(tree.contains('a')); + } +} \ No newline at end of file From e7c307d73aef1f397fd4bcd8e59058efabf5d66f Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 16 Oct 2017 22:33:27 +0300 Subject: [PATCH 4/7] init commit --- .../main/java/ru/srbau/mit/kazakov/Tree.java | 2 +- .../java/ru/srbau/mit/kazakov/TreeTest.java | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Tree/src/main/java/ru/srbau/mit/kazakov/Tree.java b/Tree/src/main/java/ru/srbau/mit/kazakov/Tree.java index c652d77..e4246fe 100644 --- a/Tree/src/main/java/ru/srbau/mit/kazakov/Tree.java +++ b/Tree/src/main/java/ru/srbau/mit/kazakov/Tree.java @@ -26,7 +26,7 @@ private class Node { * * @param value to initialize with */ - public Node(@NotNull T value) { + private Node(@NotNull T value) { data = value; } } diff --git a/Tree/src/test/java/ru/srbau/mit/kazakov/TreeTest.java b/Tree/src/test/java/ru/srbau/mit/kazakov/TreeTest.java index acaa775..5bb541f 100644 --- a/Tree/src/test/java/ru/srbau/mit/kazakov/TreeTest.java +++ b/Tree/src/test/java/ru/srbau/mit/kazakov/TreeTest.java @@ -12,14 +12,14 @@ public void testConstructor() { @Test public void testAddNotExisting() { - Tree tree = new Tree(); + Tree tree = new Tree(); assertTrue(tree.add(false)); assertTrue(tree.contains(false)); } @Test public void testAddExisting() { - Tree tree = new Tree(); + Tree tree = new Tree(); tree.add(1); assertFalse(tree.add(1)); assertTrue(tree.contains(1)); @@ -27,7 +27,7 @@ public void testAddExisting() { @Test public void testAddOneHundred() { - Tree tree = new Tree(); + Tree tree = new Tree(); for (int i = 0; i < 100; i++) { assertTrue(tree.add(i)); } @@ -37,20 +37,20 @@ public void testAddOneHundred() { @Test public void sizeEmpty() { - Tree tree = new Tree(); + Tree tree = new Tree(); assertEquals(0, tree.size()); } @Test public void sizeOne() { - Tree tree = new Tree(); + Tree tree = new Tree(); tree.add("abacaba"); assertEquals(1, tree.size()); } @Test public void testSizeOneHundred() { - Tree tree = new Tree(); + Tree tree = new Tree(); for (int i = 0; i < 100; i++) { assertTrue(tree.add(i)); } @@ -60,20 +60,20 @@ public void testSizeOneHundred() { @Test public void testContainsEmpty() { - Tree tree = new Tree(); + Tree tree = new Tree(); assertFalse(tree.contains('z')); } @Test public void testContainsNotExisting() { - Tree tree = new Tree(); + Tree tree = new Tree(); tree.add('a'); assertFalse(tree.contains('z')); } @Test public void testContainsExisting() { - Tree tree = new Tree(); + Tree tree = new Tree(); tree.add('a'); assertTrue(tree.contains('a')); } From 139a48117a8371483b68813dc5242edad97d7016 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 16 Oct 2017 22:35:26 +0300 Subject: [PATCH 5/7] delete Spiral --- Spiral/.gitignore | 49 ----- Spiral/.idea/compiler.xml | 16 -- Spiral/.idea/misc.xml | 13 -- Spiral/.idea/modules.xml | 8 - Spiral/SpiralMatrix.iml | 20 -- Spiral/pom.xml | 43 ---- .../SpiralMatrix/EvenDimensionException.java | 4 - .../NotSquareMatrixException.java | 4 - .../SpiralMatrix/NullRowException.java | 4 - .../kazakov/SpiralMatrix/SpiralMatrix.java | 128 ------------ .../SpiralMatrix/SpiralMatrixTest.java | 187 ------------------ 11 files changed, 476 deletions(-) delete mode 100644 Spiral/.gitignore delete mode 100644 Spiral/.idea/compiler.xml delete mode 100644 Spiral/.idea/misc.xml delete mode 100644 Spiral/.idea/modules.xml delete mode 100644 Spiral/SpiralMatrix.iml delete mode 100644 Spiral/pom.xml delete mode 100644 Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/EvenDimensionException.java delete mode 100644 Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NotSquareMatrixException.java delete mode 100644 Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NullRowException.java delete mode 100644 Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrix.java delete mode 100644 Spiral/src/test/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrixTest.java diff --git a/Spiral/.gitignore b/Spiral/.gitignore deleted file mode 100644 index 345e61a..0000000 --- a/Spiral/.gitignore +++ /dev/null @@ -1,49 +0,0 @@ -# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm -# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 - -# User-specific stuff: -.idea/**/workspace.xml -.idea/**/tasks.xml -.idea/dictionaries - -# Sensitive or high-churn files: -.idea/**/dataSources/ -.idea/**/dataSources.ids -.idea/**/dataSources.xml -.idea/**/dataSources.local.xml -.idea/**/sqlDataSources.xml -.idea/**/dynamic.xml -.idea/**/uiDesigner.xml - -# Gradle: -.idea/**/gradle.xml -.idea/**/libraries - -# CMake -cmake-build-debug/ - -# Mongo Explorer plugin: -.idea/**/mongoSettings.xml - -## File-based project format: -*.iws - -## Plugin-specific files: - -# IntelliJ -out/ - -# mpeltonen/sbt-idea plugin -.idea_modules/ - -# JIRA plugin -atlassian-ide-plugin.xml - -# Cursive Clojure plugin -.idea/replstate.xml - -# Crashlytics plugin (for Android Studio and IntelliJ) -com_crashlytics_export_strings.xml -crashlytics.properties -crashlytics-build.properties -fabric.properties diff --git a/Spiral/.idea/compiler.xml b/Spiral/.idea/compiler.xml deleted file mode 100644 index f76440d..0000000 --- a/Spiral/.idea/compiler.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Spiral/.idea/misc.xml b/Spiral/.idea/misc.xml deleted file mode 100644 index 56c64d0..0000000 --- a/Spiral/.idea/misc.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/Spiral/.idea/modules.xml b/Spiral/.idea/modules.xml deleted file mode 100644 index 97c24d4..0000000 --- a/Spiral/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/Spiral/SpiralMatrix.iml b/Spiral/SpiralMatrix.iml deleted file mode 100644 index 0c047ef..0000000 --- a/Spiral/SpiralMatrix.iml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Spiral/pom.xml b/Spiral/pom.xml deleted file mode 100644 index 3bde29e..0000000 --- a/Spiral/pom.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - 4.0.0 - - ru.spbau.mit.kazakov.SpiralMatrix - SpiralMatrix - 1.0-SNAPSHOT - - - - org.apache.maven.plugins - maven-compiler-plugin - - 1.8 - 1.8 - - - - - jar - - - - junit - junit - 4.0 - test - - - org.junit.jupiter - junit-jupiter-api - RELEASE - - - org.jetbrains - annotations - 13.0 - - - - \ No newline at end of file diff --git a/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/EvenDimensionException.java b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/EvenDimensionException.java deleted file mode 100644 index 28e0816..0000000 --- a/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/EvenDimensionException.java +++ /dev/null @@ -1,4 +0,0 @@ -package ru.spbau.mit.kazakov.SpiralMatrix; - -public class EvenDimensionException extends Exception { -} diff --git a/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NotSquareMatrixException.java b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NotSquareMatrixException.java deleted file mode 100644 index 9a4dcbe..0000000 --- a/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NotSquareMatrixException.java +++ /dev/null @@ -1,4 +0,0 @@ -package ru.spbau.mit.kazakov.SpiralMatrix; - -public class NotSquareMatrixException extends Exception { -} diff --git a/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NullRowException.java b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NullRowException.java deleted file mode 100644 index 7d19334..0000000 --- a/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NullRowException.java +++ /dev/null @@ -1,4 +0,0 @@ -package ru.spbau.mit.kazakov.SpiralMatrix; - -public class NullRowException extends Exception { -} diff --git a/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrix.java b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrix.java deleted file mode 100644 index d758547..0000000 --- a/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrix.java +++ /dev/null @@ -1,128 +0,0 @@ -package ru.spbau.mit.kazakov.SpiralMatrix; - -import org.jetbrains.annotations.NotNull; - -import java.util.Arrays; -import java.util.Comparator; - - -/** - * Stores transpose square matrix with odd dimensions, sorts matrix's columns by first elements, prints matrix spirally. - */ -public class SpiralMatrix { - private final int[][] matrix; - - private enum Direction {RIGHT, UP, LEFT, DOWN} - - /** - * Initialize matrix with given data. - * - * @param data initializing matrix - */ - public SpiralMatrix(@NotNull int[][] data) throws NotSquareMatrixException, NullRowException, EvenDimensionException { - checkMatrix(data); - matrix = new int[data.length][data.length]; - - for (int i = 0; i < data.length; i++) { - for (int j = 0; j < data.length; j++) { - matrix[i][j] = data[j][i]; - } - } - } - - /** - * @param data - * @throws NotSquareMatrixException - * @throws NullRowException - * @throws EvenDimensionException - */ - private void checkMatrix(@NotNull int[][] data) throws NotSquareMatrixException, NullRowException, EvenDimensionException { - int rows = data.length; - - if (rows % 2 != 1) { - throw new EvenDimensionException(); - } - - for (int[] col : data) { - if (col == null) { - throw new NullRowException(); - } - if (col.length != rows) { - throw new NotSquareMatrixException(); - } - } - } - - - /** - * Prints matrix: each element separated by space symbol, each row separated by new line symbol. - */ - public void printMatrix() { - for (int i = 0; i < matrix.length; i++) { - for (int j = 0; j < matrix.length; j++) { - System.out.print(matrix[j][i] + " "); - } - System.out.println(); - } - } - - /** - * Sorts columns by first elements. - */ - public void sortMatrix() { - Arrays.sort(matrix, Comparator.comparingInt(col -> col[0])); - } - - /** - * Prints matrix in spiral order. Elements separated by space symbol. - */ - public void printSpiralMatrix() { - int steps = 0; - int iCurrent = matrix.length / 2; - int jCurrent = matrix.length / 2; - - while (true) { - for (Direction direction : Direction.values()) { - switch (direction) { - case RIGHT: - steps++; - for (int step = 1; step <= steps; step++) { - System.out.print(matrix[iCurrent][jCurrent] + " "); - iCurrent++; - } - - if (steps == matrix.length) { - return; - } - break; - - case UP: - for (int step = 1; step <= steps; step++) { - System.out.print(matrix[iCurrent][jCurrent] + " "); - jCurrent--; - } - break; - - case LEFT: - steps++; - for (int step = 1; step <= steps; step++) { - System.out.print(matrix[iCurrent][jCurrent] + " "); - iCurrent--; - } - - if (steps == matrix.length) { - return; - } - break; - - case DOWN: - for (int step = 1; step <= steps; step++) { - System.out.print(matrix[iCurrent][jCurrent] + " "); - jCurrent++; - } - break; - } - } - } - } -} diff --git a/Spiral/src/test/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrixTest.java b/Spiral/src/test/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrixTest.java deleted file mode 100644 index 45a4369..0000000 --- a/Spiral/src/test/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrixTest.java +++ /dev/null @@ -1,187 +0,0 @@ -package ru.spbau.mit.kazakov.SpiralMatrix; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class SpiralMatrixTest { - private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); - - @Before - public void setUpStreams() { - System.setOut(new PrintStream(outContent)); - } - - @After - public void cleanUpStreams() { - System.setOut(null); - } - - @Test - public void testConstructor() throws NotSquareMatrixException, NullRowException, EvenDimensionException { - int[][] matrix = new int[][]{{3, 1, 5}, - {54, 43, 32}, - {21, 53, 76}}; - new SpiralMatrix(matrix); - } - - @Test(expected = EvenDimensionException.class) - public void testThrowsEvenDimensionException() throws NotSquareMatrixException, NullRowException, EvenDimensionException { - int[][] matrix = new int[][]{{3, 1}, - {54, 43}}; - new SpiralMatrix(matrix); - } - - @Test(expected = NotSquareMatrixException.class) - public void testRaggedMatrixThrowsNotSquareMatrixException() throws NotSquareMatrixException, NullRowException, EvenDimensionException { - int[][] matrix = new int[][]{{3, 1, 5}, - {54, 43}, - {21, 53, 76}}; - new SpiralMatrix(matrix); - } - - @Test(expected = NotSquareMatrixException.class) - public void testRectangleMatrixThrowsNotSquareMatrixException() throws NotSquareMatrixException, NullRowException, EvenDimensionException { - int[][] matrix = new int[][]{{3, 1, 5}, - {21, 53, 76}}; - new SpiralMatrix(matrix); - } - - @Test(expected = NullRowException.class) - public void testThrowsNullRowException() throws NotSquareMatrixException, NullRowException, EvenDimensionException { - int[][] matrix = new int[][]{{3, 1, 5}, - null, - {21, 53, 76}}; - new SpiralMatrix(matrix); - } - - @Test - public void testPrintMatrix3x3() throws NotSquareMatrixException, NullRowException, EvenDimensionException { - int[][] matrix = new int[][]{{3, 1, 5}, - {54, 43, 32}, - {21, 53, 76}}; - - SpiralMatrix spiral = new SpiralMatrix(matrix); - spiral.printMatrix(); - - assertEquals("3 1 5 \n54 43 32 \n21 53 76", outContent.toString().trim()); - } - - @Test - public void testPrintMatrix5x5() throws NotSquareMatrixException, NullRowException, EvenDimensionException { - int[][] matrix = new int[][]{{3, 1, 5, -1, 0}, - {54, 43, 32, 3, 2}, - {21, 53, 76, 7, 2}, - {21, 87, 32, 98, 23}, - {1, 4, 6, 4, 3}}; - - SpiralMatrix spiral = new SpiralMatrix(matrix); - spiral.printMatrix(); - - assertEquals("3 1 5 -1 0 \n" + - "54 43 32 3 2 \n" + - "21 53 76 7 2 \n" + - "21 87 32 98 23 \n" + - "1 4 6 4 3", outContent.toString().trim()); - } - - @Test - public void testPrintMatrix1x1() throws NotSquareMatrixException, NullRowException, EvenDimensionException { - int[][] matrix = new int[][]{{3}}; - - SpiralMatrix spiral = new SpiralMatrix(matrix); - spiral.printMatrix(); - - assertEquals("3", outContent.toString().trim()); - } - - @Test - public void testSortMatrix3x3() throws NotSquareMatrixException, NullRowException, EvenDimensionException { - int[][] matrix = new int[][]{{3, 1, 5}, - {54, 43, 32}, - {21, 53, 76}}; - - SpiralMatrix spiral = new SpiralMatrix(matrix); - spiral.sortMatrix(); - spiral.printMatrix(); - - assertEquals("1 3 5 \n43 54 32 \n53 21 76", outContent.toString().trim()); - } - - @Test - public void testPrintSortMatrix5x5() throws NotSquareMatrixException, NullRowException, EvenDimensionException { - int[][] matrix = new int[][]{{3, 1, 5, -1, 0}, - {54, 43, 32, 3, 2}, - {21, 53, 76, 7, 2}, - {21, 87, 32, 98, 23}, - {1, 4, 6, 4, 3}}; - - SpiralMatrix spiral = new SpiralMatrix(matrix); - spiral.sortMatrix(); - spiral.printMatrix(); - - assertEquals("-1 0 1 3 5 \n" + - "3 2 43 54 32 \n" + - "7 2 53 21 76 \n" + - "98 23 87 21 32 \n" + - "4 3 4 1 6", outContent.toString().trim()); - } - - @Test - public void testSortMatrix1x1() throws NotSquareMatrixException, NullRowException, EvenDimensionException { - int[][] matrix = new int[][]{{3}}; - - SpiralMatrix spiral = new SpiralMatrix(matrix); - spiral.sortMatrix(); - spiral.printMatrix(); - - assertEquals("3", outContent.toString().trim()); - } - - @Test - public void testPrintSpiralMatrix3x3() throws NotSquareMatrixException, NullRowException, EvenDimensionException { - int[][] matrix = new int[][]{{3, 1, 5}, - {54, 43, 32}, - {21, 53, 76}}; - - SpiralMatrix spiral = new SpiralMatrix(matrix); - spiral.printSpiralMatrix(); - - assertEquals("43 32 5 1 3 54 21 53 76", outContent.toString().trim()); - } - - @Test - public void testPrintSpiralMatrix5x5() throws NotSquareMatrixException, NullRowException, EvenDimensionException { - int[][] matrix = new int[][]{{3, 1, 5, 6, 7}, - {54, 43, 32, 3, 2}, - {21, 53, 76, 7, 2}, - {21, 87, 32, 98, 23}, - {1, 4, 6, 4, 3}}; - - SpiralMatrix spiral = new SpiralMatrix(matrix); - spiral.printSpiralMatrix(); - - assertEquals("76 7 3 32 43 53 87 32 98 23 2 2 7 6 5 1 3 54 21 21 1 4 6 4 3", outContent.toString().trim()); - } - - @Test - public void testPrintSpiralMatrix1x1() throws NotSquareMatrixException, NullRowException, EvenDimensionException { - int[][] matrix = new int[][]{{3}}; - - SpiralMatrix spiral = new SpiralMatrix(matrix); - spiral.printSpiralMatrix(); - - assertEquals("3", outContent.toString().trim()); - } - - @Test - public void test() { - int[][] matrix = new int[][]{{1, 2}, {4, 5, 6}}; - assertEquals(3, matrix[1].length); - } -} \ No newline at end of file From 859db699c555a6546070d3828f7767b0565e9510 Mon Sep 17 00:00:00 2001 From: DmitryAu Date: Mon, 13 Nov 2017 01:39:40 +0300 Subject: [PATCH 6/7] Added forgotten brackets. --- Tree/src/main/java/ru/srbau/mit/kazakov/Tree.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tree/src/main/java/ru/srbau/mit/kazakov/Tree.java b/Tree/src/main/java/ru/srbau/mit/kazakov/Tree.java index e4246fe..c7ce0ff 100644 --- a/Tree/src/main/java/ru/srbau/mit/kazakov/Tree.java +++ b/Tree/src/main/java/ru/srbau/mit/kazakov/Tree.java @@ -67,8 +67,9 @@ public boolean contains(@NotNull T value) { * @return true if there was specified value, and false otherwise */ public boolean add(@NotNull T value) { - if (contains(value)) + if (contains(value)) { return false; + } if (root == null) { root = new Node(value); size++; From 124b19389ea76a8db4b8868c7f61c29b22441e0b Mon Sep 17 00:00:00 2001 From: DmitryAu Date: Mon, 13 Nov 2017 01:52:21 +0300 Subject: [PATCH 7/7] Corrected comments. --- Tree/src/main/java/ru/srbau/mit/kazakov/Tree.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tree/src/main/java/ru/srbau/mit/kazakov/Tree.java b/Tree/src/main/java/ru/srbau/mit/kazakov/Tree.java index c7ce0ff..7d2d778 100644 --- a/Tree/src/main/java/ru/srbau/mit/kazakov/Tree.java +++ b/Tree/src/main/java/ru/srbau/mit/kazakov/Tree.java @@ -64,7 +64,7 @@ public boolean contains(@NotNull T value) { * Adds new Node storing specified value to the tree. * * @param value to add - * @return true if there was specified value, and false otherwise + * @return false if there was specified value, and true otherwise */ public boolean add(@NotNull T value) { if (contains(value)) {