Files
HW-02-git-and-java-practice/src/main/java/MainExercises.java
T
2026-04-23 04:57:11 +04:30

188 lines
5.1 KiB
Java

import java.util.ArrayList ;
import java.util.List;
public class MainExercises {
/*
you should create a triangle with "*" and return a two-dimensional array of characters based on that
the triangle's area is empty, which means some characters should be " "
example 1, input = 3:
*
**
***
example 2, input = 5:
*
**
* *
* *
*****
the output has to be a two-dimensional array of characters, so don't just print the triangle!
*/
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 (i == 0 || j == 0 || i == j || i == (n - 1)) {
triangle[i][j] = '*';
} else {
triangle[i][j] = ' ';
}
}
}
return triangle;
}
/*
SPIRAL TRAVERSAL OF A RECTANGULAR MATRIX
Given a rectangular matrix (2D array) of integers, this method traverses
it in a spiral order (clockwise from outside to inside) and returns
the elements as a 1D array.
EXAMPLE:
Input matrix:
1 2 3
4 5 6
7 8 9
Spiral order: start at top-left (1), go right →, then down ↓,
then left ←, then up ↑, then repeat inward.
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
RECTANGULAR MATRIX ASSUMPTION:
This method assumes the input matrix is RECTANGULAR (all rows have
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:
- Number of rows: matrix.length
- Number of columns: matrix[0].length (if rectangular)
*/
public int[] spiralTraversal(int[][] matrix) {
int rows = matrix.length;
int col = matrix[0].length;
int size = 0 ;
boolean increase = true;
int range = rows * col ;
int check = 0 ;
int UP = 0 ;
int DOWN = rows-1 ;
int LEFT = 0 ;
int RIGHT = col -1;
int[] newmat = new int[rows*col] ;
while (UP <= DOWN && LEFT<= RIGHT) {
for (int j = LEFT; j <= RIGHT; j++) {
newmat[size] = matrix[UP][j];
size++;
}
UP++;
for (int i = UP; i <= DOWN; i++) {
newmat[size] = matrix[i][RIGHT ];
size++;
}
RIGHT--;
if(UP <= DOWN) {
for (int j = RIGHT ; j >= LEFT; j--) {
newmat[size] = matrix[DOWN ][j];
size++;
}
DOWN--;
}
if(LEFT <= RIGHT) {
for (int i = DOWN ; i >= UP; i--) {
newmat[size] = matrix[i][LEFT];
size++;
}
LEFT++;
}
}
return newmat ;
}
/*
integer partitioning is a combinatorics problem in discreet maths
the problem is to generate sum numbers which their summation is the input number
e.g. 1 -> all partitions of integer 3 are:
3
2, 1
1, 1, 1
e.g. 2 -> for number 4 goes as:
4
3, 1
2, 2
2, 1, 1
1, 1, 1, 1
Note: As you can see in the examples, we want to generate distinct partitions,
which m eans 1,2 and 2,1 are not different — they count as the same combination.
You should generate all partitions of the input number.
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 :)
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) {
ArrayList <int[]> all = new ArrayList<>() ;
ArrayList<Integer>list = new ArrayList<>() ;
part(n , n ,list , all);
return convertTo2DArray(all) ;
}
public static int[][] convertTo2DArray(ArrayList<int[]> all) {
int [][] mat = new int [all.size()][] ;
for (int i = 0 ; i < all.size() ; i ++ )
{
mat[i] = all.get(i) ;
}
return mat;
}
public static void part(int rem , int max , ArrayList<Integer> list , ArrayList<int []> all )
{
if (rem == 0)
{
int[] final1 = new int [list.size()] ;
for (int i = 0 ; i < list.size(); i ++ )
{
final1[i] = list.get(i) ;
}
all.add(final1) ;
return;
}
for (int i = Math.min(rem , max) ; i >=1 ; i -- )
{
list.add(i) ;
part(rem-i , i , list, all );
list.remove(list.size()-1) ;
}
}
public static void main()
{
}
}