From 1c41ea38a24a1d5e512b905401f8258803c3d450 Mon Sep 17 00:00:00 2001 From: Arefe Talebi Date: Tue, 21 Apr 2026 01:03:04 +0330 Subject: [PATCH] Implement intPartitions method --- src/main/java/BonusExercises.java | 36 +++++++++- src/main/java/MainExercises.java | 109 +++++++++++++++++++++++++----- 2 files changed, 124 insertions(+), 21 deletions(-) diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index d02a746..65a45ea 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -65,11 +65,41 @@ public class BonusExercises { note: your implementation should be case-insensitive, e.g. Aba -> is palindrome */ public List findPalindromes(String string) { - List list = new ArrayList<>(); - // todo - return list; + List result = new ArrayList<>(); + String[] words = string.split("\\s+"); + for (int i = 0; i < words.length; i++) { + + String word = words[i]; + word = word.replaceAll("[^a-zA-Z]", ""); + + if (word.length() < 3) { + continue; + } + + String lower = word.toLowerCase(); + int left = 0; + int right = lower.length() - 1; + + boolean isPalindrome = true; + while (left < right) { + + if (lower.charAt(left) != lower.charAt(right)) { + isPalindrome = false; + break; + } + left++; + right--; + } + + if (isPalindrome) { + result.add(word); + } + } + + return result; } + public static void main(String[] args) { // you can test your code here } diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..9ee2189 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -1,5 +1,6 @@ -public class MainExercises -{ +import javax.xml.transform.Result; + +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 " " @@ -18,15 +19,31 @@ 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) { + public static char[][] generateTriangle(int n) { - // todo - return null; + char[][] triangle = new char[n][n]; + int i = 0; + while (i < n) { + int j = 0; + + while (j < n) { + if (j == 0 || j == i || i == n - 1) { + triangle[i][j] = '*'; + } else { + triangle[i][j] = ' '; + } + + j = j + 1; + } + + i = i + 1; + } + + return triangle; } - /* SPIRAL TRAVERSAL OF A RECTANGULAR MATRIX @@ -58,8 +75,49 @@ public class MainExercises - Number of columns: matrix[0].length (if rectangular) */ public int[] spiralTraversal(int[][] matrix) { - // todo - return null; + int rows = matrix.length; + int cols = matrix[0].length; + + int[] result = new int[rows * cols]; + int index = 0; + + int top = 0; + int bottom = rows - 1; + int left = 0; + int right = cols - 1; + + while (top <= bottom && left <= right) { + + // left → right + for (int j = left; j <= right; j++) { + result[index++] = matrix[top][j]; + } + top++; + + // top → bottom + for (int i = top; i <= bottom; i++) { + result[index++] = matrix[i][right]; + } + right--; + + // right → left + if (top <= bottom) { + for (int j = right; j >= left; j--) { + result[index++] = matrix[bottom][j]; + } + bottom--; + } + + // bottom → top + if (left <= right) { + for (int i = bottom; i >= top; i--) { + result[index++] = matrix[i][left]; + } + left++; + } + } + + return result; } /* @@ -89,15 +147,30 @@ public class MainExercises 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 - return null; + java.util.ArrayList result = new java.util.ArrayList<>(); + generate(n, n, new java.util.ArrayList(), result); + return result.toArray(new int[result.size()][]); + } + private void generate(int remaining, int maxValue, + java.util.ArrayList current, + java.util.ArrayList result) { + + if (remaining == 0) { + int[] arr = new int[current.size()]; + for (int i = 0; i < current.size(); i++) { + arr[i] = current.get(i); + } + + result.add(arr); + return; + } + + for (int i = Math.min(remaining, maxValue); i >= 1; i--) { + current.add(i); + generate(remaining - i, i, current, result); + current.remove(current.size() - 1); + } } - - - public static void main() - { - - } -} \ No newline at end of file +}