3 Commits
Author SHA1 Message Date
ShayanEdalatjoo b754df02ed Adding intPartitions object. 2026-04-25 13:44:15 +03:30
ShayanEdalatjoo 9c2990038d Adding MainTestTraversal method. 2026-04-19 15:12:05 +03:30
ShayanEdalatjoo f15ca18ad2 Adding triangle method. 2026-04-19 13:58:42 +03:30
2 changed files with 78 additions and 10 deletions
Generated
+1
View File
@@ -0,0 +1 @@
MainExercises.java
+77 -10
View File
@@ -1,3 +1,5 @@
import java.util.ArrayList;
public class MainExercises public class MainExercises
{ {
/* /*
@@ -19,10 +21,18 @@ public class MainExercises
the output has to be a two-dimensional array of characters, so don't just print the triangle! the output has to be a two-dimensional array of characters, so don't just print the triangle!
*/ */
public char[][] generateTriangle(int n) { public char[][] generateTriangle(int n) {
char[][] triangle = new char[n][];
for(int i = 0; i < n; i++) {
triangle[i] = new char[i+1];
for(int j = 0; j <= i; j++){
if(j == 0 || i == n - 1 || i == j)
triangle[i][j] = '*';
else
triangle[i][j] = ' ';
}
}
// todo return triangle;
return null;
} }
@@ -58,9 +68,37 @@ public class MainExercises
- Number of columns: matrix[0].length (if rectangular) - Number of columns: matrix[0].length (if rectangular)
*/ */
public int[] spiralTraversal(int[][] matrix) { public int[] spiralTraversal(int[][] matrix) {
// todo int rows = matrix.length;
return null; int cols = matrix[0].length;
int[] result = new int[rows * cols];
int index = 0;
int top = 0, bottom = rows - 1;
int left = 0, right = cols - 1;
while (top <= bottom && left <= right) {
for (int j = left; j <= right; j++) {
result[index++] = matrix[top][j];
} }
top++;
for (int i = top; i <= bottom; i++) {
result[index++] = matrix[i][right];
}
right--;
if (top <= bottom) {
for (int j = right; j >= left; j--) {
result[index++] = matrix[bottom][j];
}
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--) {
result[index++] = matrix[i][left];
}
left++;
}
}
return result;
}
/* /*
integer partitioning is a combinatorics problem in discreet maths integer partitioning is a combinatorics problem in discreet maths
@@ -89,15 +127,44 @@ public class MainExercises
If you're familiar with Lists and ArrayLists, you can also edit the method's If you're familiar with Lists and ArrayLists, you can also edit the method's
body to use them instead of arrays. body to use them instead of arrays.
*/ */
private ArrayList<ArrayList<Integer>> intPartitionsHelper(int n, int biggest) {
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
if (n == 0) {
result.add(new ArrayList<>());
return result;
}
for (int k = 1; k <= Math.min(biggest, n); k++) {
ArrayList<ArrayList<Integer>> subPartitions = intPartitionsHelper(n - k, k);
for (ArrayList<Integer> subPartition : subPartitions) {
ArrayList<Integer> newPartition = new ArrayList<>();
newPartition.add(k);
newPartition.addAll(subPartition);
result.add(newPartition);
}
}
return result;
}
public int[][] intPartitions(int n) { public int[][] intPartitions(int n) {
// todo ArrayList<ArrayList<Integer>> answerList = intPartitionsHelper(n, n);
return null; int[][] answerArray = new int[answerList.size()][];
for(int i = 0; i < answerList.size(); i++){
answerArray[(answerList.size() - i) - 1] = new int[answerList.get(i).size()];
for(int j = 0; j < answerList.get(i).size(); j++){
answerArray[(answerList.size() - i) - 1][j] = answerList.get(i).get(j);
}
}
return answerArray;
} }
public static void main() public void main()
{ {
int n = 4;
int[][] partitions = intPartitions(n);
for (int i = 0; i < partitions.length; i++) {
for(int j = 0; j < partitions[i].length; j++)
System.out.print(partitions[i][j]);
System.out.println();
}
} }
} }