Merge pull request 'second exersises finished' (#1) from develop into main
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class MainExercises
|
||||
{
|
||||
/*
|
||||
@@ -19,9 +23,29 @@ public class MainExercises
|
||||
the output has to be a two-dimensional array of characters, so don't just print the triangle!
|
||||
*/
|
||||
public char[][] generateTriangle(int n) {
|
||||
|
||||
// todo
|
||||
return null;
|
||||
char[][] gt = new char[n][];
|
||||
for(int i = 0 ; i<n ; i ++){
|
||||
gt[i] = new char[i+1];
|
||||
if (i == n-1){
|
||||
for ( int k= 0 ; k< n ; k++ ){
|
||||
gt[i][k] ='*';
|
||||
}
|
||||
break;
|
||||
}
|
||||
for(int j= 0 ; j<= i; j++){
|
||||
if (j == 0 ){
|
||||
gt[i][j]= '*';
|
||||
}else if (i>=1 ){
|
||||
if(j == (i)){
|
||||
gt[i][j]= '*';
|
||||
}
|
||||
}
|
||||
if(j!= 0 && j <i){
|
||||
gt[i][j] = ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
return gt;
|
||||
|
||||
}
|
||||
|
||||
@@ -58,9 +82,72 @@ public class MainExercises
|
||||
- Number of columns: matrix[0].length (if rectangular)
|
||||
*/
|
||||
public int[] spiralTraversal(int[][] matrix) {
|
||||
// todo
|
||||
return null;
|
||||
}
|
||||
int n1 = matrix.length;
|
||||
int n2 = matrix[0].length;
|
||||
int[] n3;
|
||||
if (n2 != 1 && n1 != 1) {
|
||||
n3 = new int[(2 * n1) + (2 * n2) - 4];
|
||||
} else if (n1 == 1) {
|
||||
n3 = new int[n2];
|
||||
} else {
|
||||
n3 = new int[n1];
|
||||
}
|
||||
int o = 0;
|
||||
if (n2 == 1) {
|
||||
for (int i = 0; i < n1; i++) {
|
||||
n3[o] = matrix[i][0];
|
||||
o += 1;
|
||||
}
|
||||
return (n3);
|
||||
} else if (n1 == 1) {
|
||||
for (int i = 0; i < n2; i++) {
|
||||
n3[o] = matrix[0][i];
|
||||
o += 1;
|
||||
}
|
||||
return (n3);
|
||||
|
||||
}
|
||||
|
||||
for (int i = 0; i < n2; i++) {
|
||||
n3[o] = matrix[0][i];
|
||||
o += 1;
|
||||
}
|
||||
for (int j = 1; j < n1; j++) {
|
||||
|
||||
n3[o] = matrix[j][n2 - 1];
|
||||
o += 1;
|
||||
}
|
||||
for (int k = n2 - 2; k >= 0; k--) {
|
||||
n3[o] = matrix[n1 - 1][k];
|
||||
o += 1;
|
||||
}
|
||||
for (int h = n1 - 2; h >= 1; h--) {
|
||||
|
||||
n3[o] = matrix[h][0];
|
||||
o += 1;
|
||||
}
|
||||
if (n1== 2) {
|
||||
return (n3);
|
||||
} else {
|
||||
int x1 = 0;
|
||||
int x2 = 0;
|
||||
int[][] n4 = new int[n1 - 2][n2 - 2];
|
||||
for (int i = 1; i < n1 - 1; i++) {
|
||||
for (int j = 1; j < n2 - 1; j++) {
|
||||
n4[x1][x2] = matrix[i][j];
|
||||
x2 += 1;
|
||||
|
||||
}
|
||||
x1 += 1;
|
||||
x2 = 0;
|
||||
}
|
||||
int[] n5 = spiralTraversal(n4);
|
||||
int[] c = IntStream.concat(IntStream.of(n3), IntStream.of(n5)).toArray();
|
||||
return (c);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
integer partitioning is a combinatorics problem in discreet maths
|
||||
@@ -91,8 +178,82 @@ public class MainExercises
|
||||
*/
|
||||
|
||||
public int[][] intPartitions(int n) {
|
||||
// todo
|
||||
return null;
|
||||
ArrayList<Integer> a = new ArrayList<>();
|
||||
ArrayList<int[]> a1 = new ArrayList<>();
|
||||
ArrayList<int[]> a2 = new ArrayList<>();
|
||||
ArrayList<int[]> a3 = new ArrayList<>();
|
||||
ArrayList<Integer> a4 = new ArrayList<>();
|
||||
int[][] n1;
|
||||
|
||||
if (n == 1) {
|
||||
n1 = new int[1][1];
|
||||
n1[0][0] = 1;
|
||||
return n1;
|
||||
}
|
||||
|
||||
if (n == 2) {
|
||||
n1 = new int[2][];
|
||||
n1[0] = new int[]{2};
|
||||
n1[1] = new int[]{1, 1};
|
||||
return n1;
|
||||
}
|
||||
|
||||
int[] y = new int[1];
|
||||
y[0] = n;
|
||||
a1.add(y);
|
||||
|
||||
for (int i = n - 1; i >= 1; i--) {
|
||||
int b = n - i;
|
||||
int[][] n22 = intPartitions(b);
|
||||
|
||||
for (int[] n3 : n22) {
|
||||
if (n3[0] <= i) {
|
||||
a.clear();
|
||||
a.add(i);
|
||||
for (int f : n3) {
|
||||
a.add(f);
|
||||
}
|
||||
Collections.sort(a, Collections.reverseOrder());
|
||||
|
||||
int[] n5 = new int[a.size()];
|
||||
for (int k = 0; k < a.size(); k++) {
|
||||
n5[k] = a.get(k);
|
||||
}
|
||||
a1.add(n5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = n; i >= 0; i--) {
|
||||
a3.clear();
|
||||
for (int[] j : a1) {
|
||||
if (j[0] == i) {
|
||||
a3.add(j);
|
||||
}
|
||||
}
|
||||
|
||||
if (a3.size() > 0) {
|
||||
a4.clear();
|
||||
for (int[] k : a3) {
|
||||
a4.add(k.length);
|
||||
}
|
||||
Collections.sort(a4);
|
||||
|
||||
for (int k = 0; k < a4.size(); k++) {
|
||||
for (int[] w : a3) {
|
||||
if (w.length == a4.get(k)) {
|
||||
a2.add(w);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
n1 = new int[a2.size()][];
|
||||
for (int i = 0; i < a2.size(); i++) {
|
||||
n1[i] = a2.get(i);
|
||||
}
|
||||
return n1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user