Merge pull request 'complete code' (#1) from develop into main
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
@@ -20,7 +20,10 @@ public class BonusExercises {
|
||||
- Each segment (between dots) must follow same hyphen rules
|
||||
*/
|
||||
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);
|
||||
Matcher matcher = pattern.matcher(email);
|
||||
|
||||
@@ -54,8 +57,27 @@ public class BonusExercises {
|
||||
- has no white-space in it
|
||||
*/
|
||||
public int findValidPasswords(String string) {
|
||||
// todo
|
||||
return -1;
|
||||
Pattern wordPattern = Pattern.compile("\\S+");
|
||||
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;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -18,12 +18,22 @@ 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 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)
|
||||
*/
|
||||
public int[] spiralTraversal(int[][] matrix) {
|
||||
// todo
|
||||
return null;
|
||||
int rows = matrix.length;
|
||||
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
|
||||
body to use them instead of arrays.
|
||||
*/
|
||||
|
||||
private int[][] tempPartitions;
|
||||
private int partitionCount;
|
||||
public int[][] intPartitions(int n) {
|
||||
// todo
|
||||
return null;
|
||||
int initialCapacity = n + 5;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user