1 Commits
Author SHA1 Message Date
Mobina f879c0c827 HW-02 2026-05-09 10:16:22 +03:30
+168 -81
View File
@@ -1,103 +1,190 @@
public class MainExercises public class MainExercises {
{
/* /*
you should create a triangle with "*" and return a two-dimensional array of characters based on that * you should create a triangle with "*" and return a two-dimensional array of
the triangle's area is empty, which means some characters should be " " * characters based on that
* the triangle's area is empty, which means some characters should be " "
example 1, input = 3: *
* * example 1, input = 3:
** *
*** **
***
example 2, input = 5: *
* * example 2, input = 5:
** *
* * **
* * * *
***** * *
*****
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) {
// todo // todo
return null; char[][] triangle = new char[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j == 0 || j == i)
triangle[i][j] = "*";
else
triangle[i][j] = " ";
}
}
return triangle;
} }
/* /*
SPIRAL TRAVERSAL OF A RECTANGULAR MATRIX * SPIRAL TRAVERSAL OF A RECTANGULAR MATRIX
*
Given a rectangular matrix (2D array) of integers, this method traverses * Given a rectangular matrix (2D array) of integers, this method traverses
it in a spiral order (clockwise from outside to inside) and returns * it in a spiral order (clockwise from outside to inside) and returns
the elements as a 1D array. * the elements as a 1D array.
*
EXAMPLE: * EXAMPLE:
Input matrix: * Input matrix:
1 2 3 * 1 2 3
4 5 6 * 4 5 6
7 8 9 * 7 8 9
*
Spiral order: start at top-left (1), go right →, then down ↓, * Spiral order: start at top-left (1), go right →, then down ↓,
then left ←, then up ↑, then repeat inward. * then left ←, then up ↑, then repeat inward.
*
Result: {1, 2, 3, 6, 9, 8, 7, 4, 5} * Result: {1, 2, 3, 6, 9, 8, 7, 4, 5}
*
so you should walk in that matrix in a curl and then add the numbers in order you've seen them in a 1D array * so you should walk in that matrix in a curl and then add the numbers in order
* you've seen them in a 1D array
RECTANGULAR MATRIX ASSUMPTION: *
This method assumes the input matrix is RECTANGULAR (all rows have * RECTANGULAR MATRIX ASSUMPTION:
the same number of columns). In Java, we can verify this because * This method assumes the input matrix is RECTANGULAR (all rows have
2D arrays might be jagged (rows of different lengths). * the same number of columns). In Java, we can verify this because
* 2D arrays might be jagged (rows of different lengths).
IMPORTANT: In Java, we do NOT need to pass rows and cols! *
The 2D array 'matrix' knows its own dimensions: * IMPORTANT: In Java, we do NOT need to pass rows and cols!
- Number of rows: matrix.length * The 2D array 'matrix' knows its own dimensions:
- Number of columns: matrix[0].length (if rectangular) * - Number of rows: matrix.length
*/ * - Number of columns: matrix[0].length (if rectangular)
*/
public int[] spiralTraversal(int[][] matrix) { public int[] spiralTraversal(int[][] matrix) {
// todo // todo
return null; int rows = matrix.length;
int cols = matrix[0].length;
int[] arr = new int[rows * cols];
int top = 0;
int bottom = rows - 1;
int left = 0;
int right = cols - 1;
int index = 0;
while (top <= bottom && left <= right) {
for (int i = left; i <= right; i++) {
arr[index++] = matrix[top][i];
}
top++;
for (int i = top; i <= bottom; i++) {
arr[index++] = matrix[i][right];
}
right--;
if (top <= bottom) {
for (int i = right; i >= left; i--) {
arr[index++] = matrix[bottom][i];
}
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--) {
arr[index++] = matrix[i][left];
}
left++;
}
}
return arr;
} }
/* /*
integer partitioning is a combinatorics problem in discreet maths * integer partitioning is a combinatorics problem in discreet maths
the problem is to generate sum numbers which their summation is the input number * the problem is to generate sum numbers which their summation is the input
* number
e.g. 1 -> all partitions of integer 3 are: *
3 * e.g. 1 -> all partitions of integer 3 are:
2, 1 * 3
1, 1, 1 * 2, 1
* 1, 1, 1
e.g. 2 -> for number 4 goes as: *
4 * e.g. 2 -> for number 4 goes as:
3, 1 * 4
2, 2 * 3, 1
2, 1, 1 * 2, 2
1, 1, 1, 1 * 2, 1, 1
* 1, 1, 1, 1
Note: As you can see in the examples, we want to generate distinct partitions, *
which means 1,2 and 2,1 are not different — they count as the same combination. * Note: As you can see in the examples, we want to generate distinct
* partitions,
You should generate all partitions of the input number. * which means 1,2 and 2,1 are not different — they count as the same
* combination.
Hint: You can determine the size and order of the arrays by finding the pattern *
of partitions and their count. Trust me, this one's fun and easy :) * You should generate all partitions of the input number.
*
If you're familiar with Lists and ArrayLists, you can also edit the method's * Hint: You can determine the size and order of the arrays by finding the
body to use them instead of arrays. * pattern
*/ * of partitions and their count. Trust me, this one's fun and easy :)
*
* If you're familiar with Lists and ArrayLists, you can also edit the method's
* body to use them instead of arrays.
*/
public int[][] intPartitions(int n) { public int[][] intPartitions(int n) {
// todo // todo
return null; if (n <= 0) {
return new int[0][0];
}
int[] current = new int[n];
int[][] allPartitions = new int[1000][n];
int[] counter = new int[1];
findPartitions(n, n, 0, current, allPartitions, counter);
int[][] result = new int[counter[0]][];
for (int i = 0; i < counter[0]; i++) {
int len = 0;
while (len < n && allPartitions[i][len] != 0) {
len++;
}
result[i] = new int[len];
for (int j = 0; j < len; j++) {
result[i][j] = allPartitions[i][j];
}
}
return result;
} }
private void findPartitions(int remaining, int maxVal, int position,
int[] current, int[][] allPartitions, int[] counter) {
if (remaining == 0) {
for (int i = 0; i < position; i++) {
allPartitions[counter[0]][i] = current[i];
}
counter[0]++;
return;
}
public static void main() for (int i = maxVal; i >= 1; i--) {
{ if (i <= remaining) {
current[position] = i;
findPartitions(remaining - i, i, position + 1,
current, allPartitions, counter);
}
}
}
public static void main() {
} }
} }