Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c24c558466 | ||
|
|
64d7fe7429 |
@@ -21,6 +21,24 @@
|
||||
<version>5.11.4</version>
|
||||
<scope>test</scope>
|
||||
</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>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -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 = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"; // todo
|
||||
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 = "\\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;
|
||||
}
|
||||
|
||||
@@ -54,8 +61,17 @@ public class BonusExercises {
|
||||
- has no white-space in it
|
||||
*/
|
||||
public int findValidPasswords(String string) {
|
||||
// todo
|
||||
return -1;
|
||||
String regex = "(?<!\\S)(?=\\S*[A-Z])(?=\\S*[a-z])(?=\\S*\\d)(?=\\S*[!@#$%^&*])[^\\s]{8,}(?!\\S)";
|
||||
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(string);
|
||||
|
||||
int count = 0;
|
||||
while (matcher.find()) {
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MainExercises
|
||||
{
|
||||
/*
|
||||
@@ -20,9 +23,27 @@ public class MainExercises
|
||||
*/
|
||||
public char[][] generateTriangle(int n) {
|
||||
|
||||
// todo
|
||||
return null;
|
||||
|
||||
if (n <= 0) {
|
||||
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)
|
||||
*/
|
||||
public int[] spiralTraversal(int[][] matrix) {
|
||||
// todo
|
||||
return null;
|
||||
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
|
||||
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) {
|
||||
// todo
|
||||
return null;
|
||||
if (n <= 0) return new int[0][0];
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user