add MainExercises.java and BonusExercise.java
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -20,11 +23,51 @@ 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);
|
||||
|
||||
return matcher.matches();
|
||||
|
||||
if (email.contains(" "))
|
||||
return false;
|
||||
|
||||
int count = 0;
|
||||
int index = 0;
|
||||
|
||||
// rule 1:
|
||||
for (int i = 0; i < email.length() -1; i++) {
|
||||
|
||||
if (email.charAt(i) == '@') {
|
||||
count++;
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (count != 1)
|
||||
return false;
|
||||
|
||||
|
||||
|
||||
|
||||
// rule 2:
|
||||
String localPart = email.substring(0,index);
|
||||
String domainPart = email.substring(index + 1);
|
||||
|
||||
|
||||
if (localPart.isEmpty() || domainPart.isEmpty())
|
||||
return false;
|
||||
|
||||
if (localPart.startsWith(".") || localPart.endsWith(".") ||
|
||||
domainPart.startsWith(".") || domainPart.endsWith("."))
|
||||
return false;
|
||||
|
||||
if (localPart.contains(".."))
|
||||
return false;
|
||||
|
||||
if (domainPart.contains("_"))
|
||||
return false;
|
||||
|
||||
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -39,7 +82,7 @@ public class BonusExercises {
|
||||
If no match for a date is found in the string, return null.
|
||||
*/
|
||||
public String findDate(String string) {
|
||||
// todo
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -54,8 +97,42 @@ public class BonusExercises {
|
||||
- has no white-space in it
|
||||
*/
|
||||
public int findValidPasswords(String string) {
|
||||
// todo
|
||||
return -1;
|
||||
|
||||
if (string.length() < 8)
|
||||
return 0;
|
||||
|
||||
int validPassWords = 0;
|
||||
|
||||
List<String> subStrings = List.of(string.split("\\s+"));
|
||||
|
||||
|
||||
for (String newPart : subStrings) {
|
||||
|
||||
//at least 8 characters
|
||||
if (newPart.length() < 8)
|
||||
continue;
|
||||
|
||||
|
||||
// at least one uppercase letter
|
||||
if (!newPart.matches(".*[A-Z].*"))
|
||||
continue;
|
||||
|
||||
// and at least a lowercase
|
||||
if (!newPart.matches(".*[a-z].*"))
|
||||
continue;
|
||||
|
||||
// at least one number
|
||||
if (!newPart.matches(".*\\d.*"))
|
||||
continue;
|
||||
|
||||
// at least one spacial character
|
||||
if (!newPart.matches(".*[!@#$%^&*].*"))
|
||||
continue;
|
||||
|
||||
validPassWords++;
|
||||
}
|
||||
|
||||
return validPassWords;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -66,11 +143,39 @@ public class BonusExercises {
|
||||
*/
|
||||
public List<String> findPalindromes(String string) {
|
||||
List<String> list = new ArrayList<>();
|
||||
// todo
|
||||
|
||||
Pattern words = Pattern.compile("[a-zA-Z]+");
|
||||
Matcher matcher = words.matcher(string);
|
||||
|
||||
while (matcher.find()) {
|
||||
|
||||
String word = matcher.group();
|
||||
|
||||
if (word.length() < 2)
|
||||
continue;
|
||||
|
||||
String reversed = new StringBuilder(word).reverse().toString();
|
||||
|
||||
if (reversed.equalsIgnoreCase(word))
|
||||
list.add(word);
|
||||
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// you can test your code here
|
||||
|
||||
BonusExercises kir = new BonusExercises();
|
||||
|
||||
System.out.println(kir.findValidPasswords("""
|
||||
[09:15] Dev1: Just changed my password to CodeMaster@2025. \s
|
||||
[09:17] Dev2: Haha, mine's still qwerty123, no special chars. \s
|
||||
[09:19] Dev3: I use GitHubSuper#1 but need a better one. \s
|
||||
[09:21] Dev4: AdminPass42! is good, right? \s
|
||||
[09:23] Dev5: No, too simple. I switched to UltraSecure$99 last week. \s
|
||||
[09:25] Dev6: Wait, are we sharing passwords here? \uD83D\uDE02 \s
|
||||
"""));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user