-
Notifications
You must be signed in to change notification settings - Fork 0
Spiral #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Spiral #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> | ||
| <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8"> | ||
| <output url="file://$MODULE_DIR$/target/classes" /> | ||
| <output-test url="file://$MODULE_DIR$/target/test-classes" /> | ||
| <content url="file://$MODULE_DIR$"> | ||
| <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | ||
| <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> | ||
| <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> | ||
| <excludeFolder url="file://$MODULE_DIR$/target" /> | ||
| </content> | ||
| <orderEntry type="inheritedJdk" /> | ||
| <orderEntry type="sourceFolder" forTests="false" /> | ||
| <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.0" level="project" /> | ||
| <orderEntry type="library" name="Maven: org.junit.jupiter:junit-jupiter-api:5.0.1" level="project" /> | ||
| <orderEntry type="library" name="Maven: org.opentest4j:opentest4j:1.0.0" level="project" /> | ||
| <orderEntry type="library" name="Maven: org.junit.platform:junit-platform-commons:1.0.1" level="project" /> | ||
| <orderEntry type="library" name="Maven: org.jetbrains:annotations:13.0" level="project" /> | ||
| </component> | ||
| </module> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>ru.spbau.mit.kazakov.SpiralMatrix</groupId> | ||
| <artifactId>SpiralMatrix</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <configuration> | ||
| <source>1.8</source> | ||
| <target>1.8</target> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.0</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-api</artifactId> | ||
| <version>RELEASE</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.jetbrains</groupId> | ||
| <artifactId>annotations</artifactId> | ||
| <version>13.0</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| </project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package ru.spbau.mit.kazakov.SpiralMatrix; | ||
|
|
||
| public class EvenDimensionException extends Exception { | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package ru.spbau.mit.kazakov.SpiralMatrix; | ||
|
|
||
| public class NotSquareMatrixException extends Exception { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package ru.spbau.mit.kazakov.SpiralMatrix; | ||
|
|
||
| public class NullRowException extends Exception { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| 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]; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это хорошо, если в data лежат данные в правильном формате. Для public-методов public-классов на это никогда не следует рассчитывать |
||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Checks if given matrix is correct. | ||
| * | ||
| * @param data a matrix to check | ||
| * @throws NotSquareMatrixException if matrix isn't square | ||
| * @throws NullRowException if matrix contains null row | ||
| * @throws EvenDimensionException if matrix's dimensions is even | ||
| */ | ||
| 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; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
К исключениям тоже нужны комментарии, например, в каких ситуациях бросается