From 2d4f09c315d7295256ab1e7bca17c80b5dfec5cd Mon Sep 17 00:00:00 2001 From: navid Date: Fri, 24 Apr 2026 17:40:32 +0330 Subject: [PATCH] second exersises finished --- src/main/java/MainExercises.java | 177 +++++++++++++++++++++++++++++-- 1 file changed, 169 insertions(+), 8 deletions(-) diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..43e0467 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -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=1 ){ + if(j == (i)){ + gt[i][j]= '*'; + } + } + if(j!= 0 && j = 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 a = new ArrayList<>(); + ArrayList a1 = new ArrayList<>(); + ArrayList a2 = new ArrayList<>(); + ArrayList a3 = new ArrayList<>(); + ArrayList 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; }