Skip to content

Fix issue #4429: added java code for Spiral Matrix II #4438

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

Closed
wants to merge 8 commits into from
Closed

Fix issue #4429: added java code for Spiral Matrix II #4438

wants to merge 8 commits into from

Conversation

zaidiyazdan
Copy link

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.

@zaidiyazdan
Copy link
Author

Can some one please help me with Clang format ?

@vil02
Copy link
Member

vil02 commented Sep 29, 2023

Can some one please help me with Clang format ?

You can format your code with the command (run it somewhere inside this project directory):

clang-format -i --style=file path/to/your/file.java

If you do not have the correct version of clang-format (i.e. < 16.0), you can remove this line:

InsertNewlineAtEOF: true

and add the new line symbol at the end of your file manually (but do not commit the changed .clang-format file).

If you use gitpod, everything should run smoothly there.

Comment on lines 40 to 47
public static void main(String[] args) {
SpiralMatrixII solution = new SpiralMatrixII();
int n = 3;
int[][] result = solution.generateMatrix(n);

for (int i = 0; i < n; i++) {
System.out.println(Arrays.toString(result[i]));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the main method and add some proper tests. Good examples to look at:

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay i will change it

import java.util.Arrays;

public class SpiralMatrixII {
public int[][] generateMatrix(int n) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public int[][] generateMatrix(int n) {
static public int[][] generateMatrix(int n) {

I would l suggest to rename the variable n to something like size.

What happens if n is negative?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay i will look after it


import java.util.Arrays;

public class SpiralMatrixII {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not to make a proper utility class out of this?

Suggested change
public class SpiralMatrixII {
public final class SpiralMatrixII {
private SpiralMatrixII() {
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do it

@@ -0,0 +1,49 @@
package com.thealgorithms.others;

import java.util.Arrays;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import is not used. Is that right?

Suggested change
import java.util.Arrays;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh yup ... Sorry i would have missed it

@vil02 vil02 self-assigned this Sep 29, 2023
@zaidiyazdan
Copy link
Author

@vil02 done with all the changes please check it out

Comment on lines +39 to +46
public static void main(String[] args) {
SpiralMatrixII solution = new SpiralMatrixII();
int size = 3;
int[][] result = solution.generateMatrix(size);
for (int i = 0; i < size; i++) {
System.out.println(Arrays.toString(result[i]));
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static void main(String[] args) {
SpiralMatrixII solution = new SpiralMatrixII();
int size = 3;
int[][] result = solution.generateMatrix(size);
for (int i = 0; i < size; i++) {
System.out.println(Arrays.toString(result[i]));
}
}

public final class SpiralMatrixII {
private SpiralMatrixII() {
}
static public int[][] generateMatrix(int size) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static public int[][] generateMatrix(int size) {
public static int[][] generateMatrix(final int size) {

Comment on lines +12 to +36
while (num <= size * size) {
// Traverse right
for (int i = left; i <= right && num <= size * size; i++) {
result[top][i] = num++;
}
top++;

// Traverse down
for (int i = top; i <= bottom && num <= size * size; i++) {
result[i][right] = num++;
}
right--;

// Traverse left
for (int i = right; i >= left && num <= size * size; i--) {
result[bottom][i] = num++;
}
bottom--;

// Traverse up
for (int i = bottom; i >= top && num <= size * size; i--) {
result[i][left] = num++;
}
left++;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be simplified.

Comment on lines +9 to +17
@Test
public void testGeneratedMatrixSize() {
int size = 3; // Change this to test different matrix sizes
int[][] result = SpiralMatrixII.generateMatrix(size);
assertEquals(size, result.length);
for (int i = 0; i < size; i++) {
assertEquals(size, result[i].length);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test does not add any value: if testGeneratedMatrixCorrectness passes, this will also pass.

Suggested change
@Test
public void testGeneratedMatrixSize() {
int size = 3; // Change this to test different matrix sizes
int[][] result = SpiralMatrixII.generateMatrix(size);
assertEquals(size, result.length);
for (int i = 0; i < size; i++) {
assertEquals(size, result[i].length);
}
}

@Prabhat-Kumar-42 Prabhat-Kumar-42 mentioned this pull request Oct 1, 2023
5 tasks
@vil02
Copy link
Member

vil02 commented Oct 21, 2023

@zaidiyazdan are you still working on this? if not, please close this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants