complete code #1

Merged
Aeen merged 1 commits from develop into main 2026-04-24 01:54:40 +00:00
2 changed files with 113 additions and 11 deletions
+25 -3
View File
@@ -20,7 +20,10 @@ 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 if(email == null) return false;
String regex = "^[A-Za-z0-9]+([._%+-][A-Za-z0-9]+)*@" +
"[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);
@@ -54,8 +57,27 @@ 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 Pattern wordPattern = Pattern.compile("\\S+");
return -1; Matcher wordMatcher = wordPattern.matcher(string);
String passwordRegex = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*])(?=\\S+$).{8,}$";
Pattern validationPattern = Pattern.compile(passwordRegex);
int validPasswordCount = 0;
while (wordMatcher.find()) {
String potentialPassword = wordMatcher.group();
Matcher validationMatcher = validationPattern.matcher(potentialPassword);
if (validationMatcher.matches()) {
validPasswordCount++;
}
}
return validPasswordCount;
} }
/* /*
+88 -8
View File
@@ -18,11 +18,21 @@ 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 char[][] generateTriangle(int n)
{
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 == 0 || j == 0 || j == i || i==n-1) triangle[i][j] = '*';
else triangle[i][j] = ' ';
// todo
return null;
}
}
return triangle;
} }
@@ -58,8 +68,43 @@ public class MainExercises
- 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 int rows = matrix.length;
return null; int cols = matrix[0].length;
int [] result = new int[rows * cols];
boolean[][] visited = new boolean[rows][cols];
int [] rowDirection = {0,1,0,-1}; //تغییر سطر در راست و پایین و چپ و بالا
int [] colDirection = {1,0,-1,0}; //تغییر ستون در راست و پایین و چپ و بالا
int dir = 0 ; // شروع حرکت به سمت راست
int row = 0 ; //شروع از سطر اول
int col = 0; // شروع از ستون اول
for(int i = 0 ; i < rows * cols ; i++)
{
visited[row][col] = true;
result[i] = matrix[row][col];
int nextRow = row + rowDirection[dir];
int nextCol = col + colDirection[dir];
if(nextRow < 0 || nextRow >=rows || nextCol < 0 || nextCol >=cols || visited[nextRow][nextCol])
{
dir = (dir + 1) % 4; //تغییر جهت
nextRow = row + rowDirection[dir];
nextCol = col + colDirection[dir];
}
row = nextRow;
col = nextCol;
}
return result;
} }
/* /*
@@ -89,10 +134,45 @@ public class MainExercises
If you're familiar with Lists and ArrayLists, you can also edit the method's If you're familiar with Lists and ArrayLists, you can also edit the method's
body to use them instead of arrays. body to use them instead of arrays.
*/ */
private int[][] tempPartitions;
private int partitionCount;
public int[][] intPartitions(int n) { public int[][] intPartitions(int n) {
// todo int initialCapacity = n + 5;
return null; tempPartitions = new int[initialCapacity][n];
partitionCount = 0;
int[] currentPartitionArray = new int[n];
int[] lengths = new int[initialCapacity];
generatePartitionsWithLength(n, n, 0, currentPartitionArray, lengths);
int[][] finalPartitions = new int[partitionCount][];
for (int i = 0; i < partitionCount; i++) {
finalPartitions[i] = new int[lengths[i]];
for (int j = 0; j < lengths[i]; j++) {
finalPartitions[i][j] = tempPartitions[i][j];
}
}
return finalPartitions;
}
private void generatePartitionsWithLength(int remainingValue, int maxValue, int currentIndex, int[] currentPartitionArray, int[] lengths) {
if (remainingValue == 0) {
lengths[partitionCount] = currentIndex;
for(int k=0; k < currentIndex; k++){
tempPartitions[partitionCount][k] = currentPartitionArray[k];
}
partitionCount++;
return;
}
for (int i = Math.min(maxValue, remainingValue); i >= 1; i--) {
currentPartitionArray[currentIndex] = i;
generatePartitionsWithLength(remainingValue - i, i, currentIndex + 1, currentPartitionArray, lengths);
}
} }