Doing main exercises

This commit is contained in:
2026-04-21 12:25:10 +03:30
parent 5780e034f1
commit f88049c7c4
5 changed files with 112 additions and 11 deletions
+10
View File
@@ -0,0 +1,10 @@
# Default ignored files
/shelf/
/workspace.xml
# Ignored default folder with query files
/queries/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="25" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
+10
View File
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/main/main.iml" filepath="$PROJECT_DIR$/main/main.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/src.iml" filepath="$PROJECT_DIR$/.idea/src.iml" />
<module fileurl="file://$PROJECT_DIR$/test/test.iml" filepath="$PROJECT_DIR$/test/test.iml" />
</modules>
</component>
</project>
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
+80 -11
View File
@@ -1,3 +1,6 @@
import java.util.ArrayList;
import java.util.List;
public class MainExercises public class MainExercises
{ {
/* /*
@@ -18,11 +21,21 @@ 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)
{
// todo char[][] triangle = new char[n][];
return null; for(int i = 0; i < n; i++)
{
triangle[i] = new char[i+1];
for(int j = 0; j <= i; j++)
{
if(i == 0 || j == 0 || i == j || i == n-1)
triangle[i][j] = '*';
else
triangle[i][j] = ' ';
}
}
return triangle;
} }
@@ -57,9 +70,38 @@ public class MainExercises
- Number of rows: matrix.length - Number of rows: matrix.length
- 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 {
return null;
int m = matrix.length;
int n = matrix[0].length;
int x = 0;
int y = 0;
int idx = 0;
int[] numbers = new int[m * n];
boolean[][] visitedCell = new boolean[m][n];
int[][] dir = {{0,1},{1,0},{0,-1},{-1,0}};
for(int i = 0; i < (m * n); i++)
{
numbers[i] = matrix[x][y];
visitedCell[x][y] = true;
int nextX = x + dir[idx][0];
int nextY = y + dir[idx][1];
if (nextX < 0 || nextX >= m || nextY < 0 || nextY >= n || visitedCell[nextX][nextY] == true)
{
idx = (idx + 1) % 4;
nextX = x + dir[idx][0];
nextY = y + dir[idx][1];
}
x = nextX;
y = nextY;
}
return numbers;
} }
/* /*
@@ -90,11 +132,38 @@ public class MainExercises
body to use them instead of arrays. body to use them instead of arrays.
*/ */
public int[][] intPartitions(int n) { public int[][] intPartitions(int n)
// todo {
return null; List<int[]> result = new ArrayList<>();
findPartitions(n, n, new ArrayList<>(), result);
return result.toArray(new int[result.size()][]);
} }
private void findPartitions(int r, int max, List<Integer> list, List<int[]> finalList)
{
if (r == 0)
{
int[] partition = new int[list.size()];
for (int i = 0; i < list.size(); i++)
{
partition[i] = list.get(i);
}
finalList.add(partition);
return;
}
if (r >= max)
{
list.add(max);
findPartitions(r - max, max, list, finalList);
list.remove(list.size() - 1);
}
if (max > 1)
{
findPartitions(r, max - 1, list, finalList);
}
}
public static void main() public static void main()
{ {