Complete main and bonus exercises
This commit is contained in:
@@ -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
|
String local = "[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)*";
|
||||||
|
String domainSegment = "[a-zA-Z0-9]([a-zA-Z0-9\\-]*[a-zA-Z0-9])?";
|
||||||
|
String domain = domainSegment + "(\\." + domainSegment + ")*";
|
||||||
|
String regex = "^" + local + "@" + domain + "$";
|
||||||
Pattern pattern = Pattern.compile(regex);
|
Pattern pattern = Pattern.compile(regex);
|
||||||
Matcher matcher = pattern.matcher(email);
|
Matcher matcher = pattern.matcher(email);
|
||||||
|
|
||||||
@@ -39,7 +42,20 @@ 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 dayOrMonth = "(0[1-9]|[12][0-9]|3[01])";
|
||||||
|
String month = "(0[1-9]|1[0-2])";
|
||||||
|
String year = "[0-9]{4}";
|
||||||
|
|
||||||
|
String[] patterns = {
|
||||||
|
dayOrMonth + "/" + month + "/" + year,
|
||||||
|
year + "-" + month + "-" + dayOrMonth,
|
||||||
|
year + "/" + month + "/" + dayOrMonth
|
||||||
|
};
|
||||||
|
|
||||||
|
for (String pat : patterns) {
|
||||||
|
Matcher m = Pattern.compile(pat).matcher(string);
|
||||||
|
if (m.find()) return m.group();
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,8 +70,11 @@ 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;
|
Matcher m = Pattern.compile(regex).matcher(string);
|
||||||
|
int count = 0;
|
||||||
|
while (m.find()) count++;
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -66,10 +85,25 @@ 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
|
Matcher m = Pattern.compile("[a-zA-Z]+").matcher(string);
|
||||||
|
while (m.find()) {
|
||||||
|
String word = m.group();
|
||||||
|
if (word.length() >= 3 && isPalindrome(word)) {
|
||||||
|
list.add(word);
|
||||||
|
}
|
||||||
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isPalindrome(String s) {
|
||||||
|
String lower = s.toLowerCase();
|
||||||
|
int left = 0, right = lower.length() - 1;
|
||||||
|
while (left < right) {
|
||||||
|
if (lower.charAt(left++) != lower.charAt(right--)) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// you can test your code here
|
// you can test your code here
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class MainExercises
|
public class MainExercises
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
@@ -19,10 +23,20 @@ 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) {
|
||||||
|
if (n == 0) return new char[0][];
|
||||||
|
|
||||||
// todo
|
char[][] triangle = new char[n][];
|
||||||
return null;
|
for (int i = 0; i < n; i++) {
|
||||||
|
triangle[i] = new char[i + 1];
|
||||||
|
if (i <= 1 || i == n - 1) {
|
||||||
|
Arrays.fill(triangle[i], '*');
|
||||||
|
} else {
|
||||||
|
triangle[i][0] = '*';
|
||||||
|
Arrays.fill(triangle[i], 1, i, ' ');
|
||||||
|
triangle[i][i] = '*';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return triangle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -58,8 +72,33 @@ 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) return new int[0];
|
||||||
return null;
|
|
||||||
|
int rows = matrix.length;
|
||||||
|
int cols = matrix[0].length;
|
||||||
|
int[] result = new int[rows * cols];
|
||||||
|
int idx = 0;
|
||||||
|
|
||||||
|
int top = 0, bottom = rows - 1, left = 0, right = cols - 1;
|
||||||
|
|
||||||
|
while (top <= bottom && left <= right) {
|
||||||
|
for (int j = left; j <= right; j++) result[idx++] = matrix[top][j];
|
||||||
|
top++;
|
||||||
|
|
||||||
|
for (int i = top; i <= bottom; i++) result[idx++] = matrix[i][right];
|
||||||
|
right--;
|
||||||
|
|
||||||
|
if (top <= bottom) {
|
||||||
|
for (int j = right; j >= left; j--) result[idx++] = matrix[bottom][j];
|
||||||
|
bottom--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (left <= right) {
|
||||||
|
for (int i = bottom; i >= top; i--) result[idx++] = matrix[i][left];
|
||||||
|
left++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -91,8 +130,20 @@ public class MainExercises
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public int[][] intPartitions(int n) {
|
public int[][] intPartitions(int n) {
|
||||||
// todo
|
List<int[]> result = new ArrayList<>();
|
||||||
return null;
|
generatePartitions(n, n, new int[n], 0, result);
|
||||||
|
return result.toArray(new int[0][]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generatePartitions(int remaining, int maxPart, int[] current, int depth, List<int[]> result) {
|
||||||
|
if (remaining == 0) {
|
||||||
|
result.add(Arrays.copyOf(current, depth));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (int part = Math.min(remaining, maxPart); part >= 1; part--) {
|
||||||
|
current[depth] = part;
|
||||||
|
generatePartitions(remaining - part, part, current, depth + 1, result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -100,4 +151,4 @@ public class MainExercises
|
|||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user