This commit is contained in:
Reza
2026-04-22 17:07:37 +03:30
parent e73ac6115f
commit 8dff112467
2 changed files with 139 additions and 87 deletions
+63 -4
View File
@@ -39,7 +39,21 @@ public class BonusExercises {
If no match for a date is found in the string, return null.
*/
public String findDate(String string) {
// todo
String regex1 = "\\d{4}[-/](0[1-9]|1[0-2])[-/](0[1-9]|[12]\\d|3[01])";
String regex2 = "(0[1-9]|[12]\\d|3[01])/(0[1-9]|1[0-2])/\\d{4}";
Pattern pattern1 = Pattern.compile(regex1);
Pattern pattern2 = Pattern.compile(regex2);
Matcher matcher1 = pattern1.matcher(string);
Matcher matcher2 = pattern2.matcher(string);
if (matcher1.find()) {
return matcher1.group();
}
if (matcher2.find()) {
return matcher2.group();
}
return null;
}
@@ -54,8 +68,42 @@ public class BonusExercises {
- has no white-space in it
*/
public int findValidPasswords(String string) {
// todo
return -1;
int count = 0;
String[] parts = string.split(" ");
for (String p : parts) {
if (p.length() < 8) {
continue;
}
boolean hasUpper = false;
boolean hasLower = false;
boolean hasNumber = false;
boolean hasSpecial = false;
boolean hasSpace = false;
for (int i = 0; i < p.length(); i++) {
char c = p.charAt(i);
if (Character.isUpperCase(c)) {
hasUpper = true;
}
else if (Character.isLowerCase(c)) {
hasLower = true;
}
else if (Character.isDigit(c)) {
hasNumber = true;
}
else if (Character.isWhitespace(c)) {
hasSpace = true;
}
else {
hasSpecial = true;
}
}
if (!hasSpace && hasUpper && hasLower && hasNumber && hasSpecial) {
count++;
}
}
return count;
}
/*
@@ -66,7 +114,18 @@ public class BonusExercises {
*/
public List<String> findPalindromes(String string) {
List<String> list = new ArrayList<>();
// todo
String[] words = string.split(" ");
for (String w : words) {
String clean = w.replaceAll("[^A-Za-z]", "");
if (clean.length() >= 3) {
String lower = clean.toLowerCase() ;
String reversed = new StringBuilder(lower).reverse().toString();
if (lower.equals(reversed)) {
list.add(clean);
}
}
}
return list;
}