02 #2

Closed
SoroushZiaee wants to merge 1 commits from SoroushZiaee/HW-02-git-and-java-practice:develop into main
3 changed files with 131 additions and 11 deletions
+18
View File
@@ -21,6 +21,24 @@
<version>5.11.4</version> <version>5.11.4</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
+20 -4
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 = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"; // todo
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 = "\\b(\\d{2}/\\d{2}/\\d{4}|\\d{4}-\\d{2}-\\d{2}|\\d{4}/\\d{2}/\\d{2})\\b";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);
if (matcher.find()) {
return matcher.group();
}
return null; return null;
} }
@@ -54,8 +61,17 @@ 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)(?=\\S*[A-Z])(?=\\S*[a-z])(?=\\S*\\d)(?=\\S*[!@#$%^&*])[^\\s]{8,}(?!\\S)";
return -1;
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);
int count = 0;
while (matcher.find()) {
count++;
}
return count;
} }
/* /*
+93 -7
View File
@@ -1,3 +1,6 @@
import java.util.ArrayList;
import java.util.List;
public class MainExercises public class MainExercises
{ {
/* /*
@@ -20,9 +23,27 @@ public class MainExercises
*/ */
public char[][] generateTriangle(int n) { public char[][] generateTriangle(int n) {
// todo if (n <= 0) {
return null; return new char[0][0];
}
char[][] result = new char[n][];
for (int i = 0; i < n; i++) {
result[i] = new char[i + 1];
if (i == 0) {
result[i][0] = '*';
} else if (i == n - 1) {
for (int j = 0; j <= i; j++) {
result[i][j] = '*';
}
} else {
result[i][0] = '*';
result[i][i] = '*';
for (int j = 1; j < i; j++) {
result[i][j] = ' ';
}
}
}
return result;
} }
@@ -58,8 +79,49 @@ 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 if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return null; return new int[0];
}
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) {
for (int j = left; j <= right; j++) {
result[index++] = matrix[top][j];
}
top++;
for (int i = top; i <= bottom; i++) {
result[index++] = matrix[i][right];
}
right--;
if (top <= bottom) {
for (int j = right; j >= left; j--) {
result[index++] = matrix[bottom][j];
}
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--) {
result[index++] = matrix[i][left];
}
left++;
}
}
return result;
} }
/* /*
@@ -91,8 +153,32 @@ public class MainExercises
*/ */
public int[][] intPartitions(int n) { public int[][] intPartitions(int n) {
// todo if (n <= 0) return new int[0][0];
return null;
List<int[]> resultList = new ArrayList<>();
findPartitions(n, n, new ArrayList<>(), resultList);
int[][] resultArray = new int[resultList.size()][];
for (int i = 0; i < resultList.size(); i++) {
resultArray[i] = resultList.get(i);
}
return resultArray;
}
private void findPartitions(int n, int max, List<Integer> current, List<int[]> resultList) {
if (n == 0) {
int[] partition = new int[current.size()];
for (int i = 0; i < current.size(); i++) {
partition[i] = current.get(i);
}
resultList.add(partition);
return;
}
for (int i = Math.min(max, n); i >= 1; i--) {
current.add(i);
findPartitions(n - i, i, current, resultList);
current.removeLast();
}
} }