implement all tasks

This commit is contained in:
2026-06-13 03:32:42 +04:30
parent e73ac6115f
commit d9fdc7c8a0
2 changed files with 147 additions and 14 deletions
+35 -5
View File
@@ -20,7 +20,7 @@ public class BonusExercises {
- Each segment (between dots) must follow same hyphen rules - Each segment (between dots) must follow same hyphen rules
*/ */
public boolean validateEmail(String email) { 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); Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(email); 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. If no match for a date is found in the string, return null.
*/ */
public String findDate(String string) { 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; return null;
} }
@@ -54,8 +61,15 @@ public class BonusExercises {
- has no white-space in it - has no white-space in it
*/ */
public int findValidPasswords(String string) { public int findValidPasswords(String string) {
// todo String regex = "(?=\\S{8,})(?=\\S*[a-z])(?=\\S*[A-Z])(?=\\S*\\d)(?=\\S*[!@#$%^&*])\\S+";
return -1; 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<String> findPalindromes(String string) { public List<String> findPalindromes(String string) {
List<String> list = new ArrayList<>(); List<String> 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; return list;
} }
+112 -9
View File
@@ -1,3 +1,5 @@
import java.util.ArrayList;
public class MainExercises 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! 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 // 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 rows: matrix.length
- Number of columns: matrix[0].length (if rectangular) - Number of columns: matrix[0].length (if rectangular)
*/ */
public int[] spiralTraversal(int[][] matrix) { public int[] spiralTraversal(int[][] matrix)
{
// todo // 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. body to use them instead of arrays.
*/ */
/*
integer partitioning is a combinatorics problem in discreet maths
...
*/
// ۱. خروجی متد رو به لیستی از لیست‌ها تغییر می‌دیم تا کار راحت بشه
public int[][] intPartitions(int n) { public int[][] intPartitions(int n) {
// todo
return null; ArrayList<ArrayList<Integer>> allPartitions = new ArrayList<>();
ArrayList<Integer> 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<Integer> currentPartition, ArrayList<ArrayList<Integer>> 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()
{
} }
}