2HW
This commit is contained in:
@@ -20,11 +20,38 @@ public class BonusExercises {
|
||||
- Each segment (between dots) must follow same hyphen rules
|
||||
*/
|
||||
public boolean validateEmail(String email) {
|
||||
String regex = ""; // todo
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(email);
|
||||
if (email == null || email.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return matcher.matches();
|
||||
if (email.contains(" ") || email.contains("\t")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
long atCount = email.chars().filter(ch -> ch == '@').count();
|
||||
if (atCount != 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String[] parts = email.split("@", -1);
|
||||
String local = parts[0];
|
||||
String domain = parts[1];
|
||||
|
||||
if (local.isEmpty()) return false;
|
||||
if (local.startsWith(".") || local.endsWith(".")) return false;
|
||||
if (local.contains("..")) return false;
|
||||
|
||||
if (domain.isEmpty()) return false;
|
||||
if (domain.startsWith("-") || domain.endsWith("-")) return false;
|
||||
if (domain.contains("_")) return false;
|
||||
|
||||
String[] segments = domain.split("\\.", -1);
|
||||
for (String segment : segments) {
|
||||
if (segment.isEmpty()) return false;
|
||||
if (segment.startsWith("-") || segment.endsWith("-")) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -39,10 +66,54 @@ public class BonusExercises {
|
||||
If no match for a date is found in the string, return null.
|
||||
*/
|
||||
public String findDate(String string) {
|
||||
// todo
|
||||
if (string == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String regex = "(\\d{4}-\\d{2}-\\d{2})|(\\d{4}/\\d{2}/\\d{2})|(\\d{2}/\\d{2}/\\d{4})";
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(string);
|
||||
|
||||
while (matcher.find()) {
|
||||
String match = matcher.group();
|
||||
if (isValidDate(match)) {
|
||||
return match;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean isValidMonth(int month) {
|
||||
return month >= 1 && month <= 12;
|
||||
}
|
||||
|
||||
private boolean isValidDay(int day) {
|
||||
return day >= 1 && day <= 31;
|
||||
}
|
||||
|
||||
private boolean isValidDate(String match) {
|
||||
if (match.contains("-")) {
|
||||
String[] parts = match.split("-");
|
||||
int month = Integer.parseInt(parts[1]);
|
||||
int day = Integer.parseInt(parts[2]);
|
||||
return isValidMonth(month) && isValidDay(day);
|
||||
}
|
||||
|
||||
String[] parts = match.split("/");
|
||||
if (parts[0].length() == 4) {
|
||||
int month = Integer.parseInt(parts[1]);
|
||||
int day = Integer.parseInt(parts[2]);
|
||||
return isValidMonth(month) && isValidDay(day);
|
||||
}
|
||||
|
||||
int first = Integer.parseInt(parts[0]);
|
||||
int second = Integer.parseInt(parts[1]);
|
||||
boolean asMonthThenDay = isValidMonth(first) && isValidDay(second);
|
||||
boolean asDayThenMonth = isValidDay(first) && isValidMonth(second);
|
||||
return asMonthThenDay || asDayThenMonth;
|
||||
}
|
||||
|
||||
/*
|
||||
given a string, implement the method to detect all valid passwords
|
||||
then, it should return the count of them
|
||||
@@ -54,8 +125,42 @@ public class BonusExercises {
|
||||
- has no white-space in it
|
||||
*/
|
||||
public int findValidPasswords(String string) {
|
||||
// todo
|
||||
return -1;
|
||||
if (string == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
String specialChars = "!@#$%^&*";
|
||||
String[] tokens = string.split("\\s+");
|
||||
int count = 0;
|
||||
|
||||
for (String token : tokens) {
|
||||
if (token.isEmpty() || token.length() < 8) {
|
||||
continue;
|
||||
}
|
||||
|
||||
boolean hasUpper = false;
|
||||
boolean hasLower = false;
|
||||
boolean hasDigit = false;
|
||||
boolean hasSpecial = false;
|
||||
|
||||
for (char c : token.toCharArray()) {
|
||||
if (Character.isUpperCase(c)) {
|
||||
hasUpper = true;
|
||||
} else if (Character.isLowerCase(c)) {
|
||||
hasLower = true;
|
||||
} else if (Character.isDigit(c)) {
|
||||
hasDigit = true;
|
||||
} else if (specialChars.indexOf(c) >= 0) {
|
||||
hasSpecial = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasUpper && hasLower && hasDigit && hasSpecial) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -66,11 +171,32 @@ public class BonusExercises {
|
||||
*/
|
||||
public List<String> findPalindromes(String string) {
|
||||
List<String> list = new ArrayList<>();
|
||||
// todo
|
||||
|
||||
if (string == null) {
|
||||
return list;
|
||||
}
|
||||
|
||||
String[] tokens = string.split("\\s+");
|
||||
|
||||
for (String token : tokens) {
|
||||
String cleaned = token.replaceAll("[^a-zA-Z]", "");
|
||||
|
||||
if (cleaned.length() < 3) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String lower = cleaned.toLowerCase();
|
||||
String reversed = new StringBuilder(lower).reverse().toString();
|
||||
|
||||
if (lower.equals(reversed)) {
|
||||
list.add(cleaned);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// you can test your code here
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MainExercises
|
||||
{
|
||||
/*
|
||||
@@ -19,10 +22,28 @@ 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) {
|
||||
char[][] triangle = new char[n][];
|
||||
|
||||
// todo
|
||||
return null;
|
||||
for (int i = 0; i < n; i++) {
|
||||
int len = i + 1;
|
||||
char[] row = new char[len];
|
||||
|
||||
boolean isLastRow = (i == n - 1);
|
||||
|
||||
for (int j = 0; j < len; j++) {
|
||||
if (isLastRow) {
|
||||
row[j] = '*';
|
||||
} else if (j == 0 || j == len - 1) {
|
||||
row[j] = '*';
|
||||
} else {
|
||||
row[j] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
triangle[i] = row;
|
||||
}
|
||||
|
||||
return triangle;
|
||||
}
|
||||
|
||||
|
||||
@@ -58,8 +79,45 @@ 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 idx = 0;
|
||||
|
||||
int top = 0, bottom = rows - 1;
|
||||
int 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,13 +149,40 @@ public class MainExercises
|
||||
*/
|
||||
|
||||
public int[][] intPartitions(int n) {
|
||||
// todo
|
||||
return null;
|
||||
List<int[]> result = new ArrayList<>();
|
||||
List<Integer> current = new ArrayList<>();
|
||||
|
||||
partitionHelper(n, n, current, result);
|
||||
|
||||
int[][] output = new int[result.size()][];
|
||||
for (int i = 0; i < result.size(); i++) {
|
||||
output[i] = result.get(i);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
public static void main()
|
||||
private void partitionHelper(int remaining, int maxPart, List<Integer> current, List<int[]> result) {
|
||||
if (remaining == 0) {
|
||||
int[] arr = new int[current.size()];
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
arr[i] = current.get(i);
|
||||
}
|
||||
result.add(arr);
|
||||
return;
|
||||
}
|
||||
|
||||
int limit = Math.min(remaining, maxPart);
|
||||
for (int part = limit; part >= 1; part--) {
|
||||
current.add(part);
|
||||
partitionHelper(remaining - part, part, current, result);
|
||||
current.remove(current.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user