Merge pull request '"Complete assignment - ready for grading"' (#1) from develop into main

Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
2026-07-16 18:01:47 +00:00
2 changed files with 103 additions and 6 deletions
+100 -6
View File
@@ -1,3 +1,5 @@
import java.util.ArrayList;
public class MainExercises public class MainExercises
{ {
/* /*
@@ -21,7 +23,26 @@ public class MainExercises
public char[][] generateTriangle(int n) { public char[][] generateTriangle(int n) {
// todo // todo
return null; char[][] triangle = new char[n][];
for (int i = 0; i < n; i++) {
if (i == 0) {
triangle[0][0] = '*';
}
else if (i < (n - 1) && i > 0) {
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) {
triangle[i][j] = '*';
}
else triangle[i][j] = ' ';
}
}
else if (i == (n - 1)) {
for (int k = 0; k < n; k++) {
triangle[i][k] = '*';
}
}
}
return triangle;
} }
@@ -59,7 +80,49 @@ public class MainExercises
*/ */
public int[] spiralTraversal(int[][] matrix) { public int[] spiralTraversal(int[][] matrix) {
// todo // todo
return null; if (matrix == null || matrix.length == 0) {
return new int[0];
}
int rows = matrix.length;
int cols = matrix[0].length;
int[] result = new int[rows * cols];
int index = 0;
int top = 0;
int bottom = rows - 1;
int left = 0;
int right = cols - 1;
while (top <= bottom && left <= right) {
for (int i = left; i <= right; i++) {
result[index++] = matrix[top][i];
}
top++;
for (int i = top; i <= bottom; i++) {
result[index++] = matrix[i][right];
}
right--;
if (top <= bottom) {
for (int i = right; i >= left; i--) {
result[index++] = matrix[bottom][i];
}
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--) {
result[index++] = matrix[i][left];
}
left++;
}
}
return result;
} }
/* /*
@@ -90,13 +153,44 @@ public class MainExercises
body to use them instead of arrays. body to use them instead of arrays.
*/ */
public int[][] intPartitions(int n) { public static int[][] intPartitions(int n) {
// todo ArrayList<ArrayList<Integer>> result = new ArrayList<>();
return null; backtrack(n, n, new ArrayList<>(), result);
ArrayList<int[]> resLst = new ArrayList<int[]>();
for (int i = 0; i < result.size(); i++) {
int[] row = new int[result.get(i).size()];
for (int j = 0; j < result.get(i).size(); j++) {
row[j] = result.get(i).get(j);
}
resLst.add(row);
} }
int[][] res = new int[resLst.size()][];
for (int k = 0; k < resLst.size(); k++) {
res[k] = resLst.get(k);
}
public static void main() return res;
}
private static void backtrack(int remaining, int max,
ArrayList<Integer> current,
ArrayList<ArrayList<Integer>> result) {
if (remaining == 0) {
result.add(new ArrayList<>(current));
return;
}
for (int i = Math.min(max, remaining); i >= 1; i--) {
current.add(i);
backtrack(remaining - i, i, current, result);
current.remove(current.size() - 1); // backtrack
}
}
static void main()
{ {
} }
+3
View File
@@ -3,6 +3,7 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
public class MainTestPartitions { public class MainTestPartitions {
static MainExercises ex; static MainExercises ex;
@@ -63,3 +64,5 @@ public class MainTestPartitions {
assertArrayEquals(expected, ex.intPartitions(6)); assertArrayEquals(expected, ex.intPartitions(6));
} }
} }