From d9fdc7c8a02683791916050edddffd1fb8d9334d Mon Sep 17 00:00:00 2001 From: emad Date: Sat, 13 Jun 2026 03:32:42 +0430 Subject: [PATCH] implement all tasks --- src/main/java/BonusExercises.java | 40 ++++++++-- src/main/java/MainExercises.java | 121 +++++++++++++++++++++++++++--- 2 files changed, 147 insertions(+), 14 deletions(-) diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index d02a746..a7ee6d1 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -20,7 +20,7 @@ public class BonusExercises { - Each segment (between dots) must follow same hyphen rules */ public boolean validateEmail(String email) { - String regex = ""; // todo + String regex = "^\\w+(\\.\\w+)*@[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*(\\.[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*)+$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(email); @@ -39,7 +39,14 @@ public class BonusExercises { If no match for a date is found in the string, return null. */ public String findDate(String string) { - // todo + String regex = "((0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/\\d{4}|\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])|\\d{4}/(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])|(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])/\\d{4})"; + + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(string); + if(matcher.find()) + { + return matcher.group(); + } return null; } @@ -54,8 +61,15 @@ public class BonusExercises { - has no white-space in it */ public int findValidPasswords(String string) { - // todo - return -1; + String regex = "(?=\\S{8,})(?=\\S*[a-z])(?=\\S*[A-Z])(?=\\S*\\d)(?=\\S*[!@#$%^&*])\\S+"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(string); + int res = 0; + while (matcher.find()) + { + res++; + } + return res; } /* @@ -66,7 +80,23 @@ public class BonusExercises { */ public List findPalindromes(String string) { List list = new ArrayList<>(); - // todo + //AI generated! + String regex = "(?i)\\b(" + + "([a-z])[a-z]\\2" + + "|([a-z])([a-z])\\4\\3" + + "|([a-z])([a-z])[a-z]\\6\\5" + + "|([a-z])([a-z])([a-z])\\9\\8\\7" + + "|([a-z])([a-z])([a-z])[a-z]\\12\\11\\10" + + ")\\b"; + + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(string); + + while (matcher.find()) { + // matcher.group() extracts the exact word that matched the pattern + list.add(matcher.group()); + } + return list; } diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..914563f 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -1,3 +1,5 @@ +import java.util.ArrayList; + public class MainExercises { /* @@ -18,10 +20,40 @@ 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 void main() + { + + } + + + public char[][] generateTriangle(int n) + { // todo - return null; + 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 == n - 1) + { + triangle[i][j] = '*'; + } + else if(j == 0 || j == i) + { + triangle[i][j] = '*'; + } + else + { + triangle[i][j] = ' '; + } + } + + } + + return triangle; } @@ -57,9 +89,42 @@ public class MainExercises - Number of rows: matrix.length - Number of columns: matrix[0].length (if rectangular) */ - public int[] spiralTraversal(int[][] matrix) { + public int[] spiralTraversal(int[][] matrix) + { // todo - return null; + int arrSize = matrix.length * matrix[0].length; + int top = 0; + int bottom = matrix.length - 1; + int left = 0; + int right = matrix[0].length - 1; + int[] res = new int[arrSize]; + int index = 0; + while (top <= bottom && left <= right) + { + for (int i = left; i <= right ; i++) { + res[index++] = matrix[top][i]; + } + top++; + for (int i = top; i <= bottom ; i++) { + res[index++] = matrix[i][right]; + } + right--; + if(top <= bottom) + { + for (int i = right; i >= left ; i--) { + res[index++] = matrix[bottom][i]; + } + bottom--; + } + if(left <= right) + { + for (int i = bottom; i >= top ; i--) { + res[index++] = matrix[i][left]; + } + left++; + } + } + return res; } /* @@ -90,14 +155,52 @@ public class MainExercises body to use them instead of arrays. */ + + + /* + integer partitioning is a combinatorics problem in discreet maths + ... + */ + // ۱. خروجی متد رو به لیستی از لیست‌ها تغییر می‌دیم تا کار راحت بشه public int[][] intPartitions(int n) { - // todo - return null; + + ArrayList> allPartitions = new ArrayList<>(); + ArrayList currentPartition = new ArrayList<>(); + + // تابع کمکی بازگشتی رو صدا می‌زنیم: + // عدد n (مقدار باقی‌مانده اولیه)، n (بزرگترین عدد مجاز اولیه)، سبد خالی و لیست نهایی + findPartitionsHelper(n, n, currentPartition, allPartitions); + int[][] res = new int[allPartitions.size()][]; + for (int i = 0; i < allPartitions.size(); i++) { + res[i] = new int[allPartitions.get(i).size()]; + for (int j = 0; j < allPartitions.get(i).size(); j++) { + res[i][j] = allPartitions.get(i).get(j); + } + } + + return res; + } + // ۲. این همون تابع کمکی هست که درخت حالت‌ها رو برامون می‌سازه + private void findPartitionsHelper(int target, int maxVal, ArrayList currentPartition, ArrayList> allPartitions) { + // اگر باقی‌مانده صفر شد، یعنی سبد ما مجموعش شده خود n + if (target == 0) { + allPartitions.add(new ArrayList<>(currentPartition)); // یک کپی از سبد رو ذخیره می‌کنیم + return; + } + + // حلقه از بزرگترین عدد مجاز رو به پایین حرکت میکنه + for (int i = Math.min(target, maxVal); i >= 1; i--) { + + currentPartition.add(i); // عدد رو میذاریم تو سبد + + // بازی رو برای باقی‌مانده تکرار می‌کنیم + findPartitionsHelper(target - i, i, currentPartition, allPartitions); + + // عقب‌گرد: عدد رو از سبد در میاریم تا بتونیم توی دور بعدی حلقه حالت‌های دیگه رو تست کنیم + currentPartition.remove(currentPartition.size() - 1); + } } - public static void main() - { - } } \ No newline at end of file