workshop_one
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -20,7 +25,13 @@ public class BonusExercises {
|
||||
- Each segment (between dots) must follow same hyphen rules
|
||||
*/
|
||||
public boolean validateEmail(String email) {
|
||||
String regex = ""; // todo
|
||||
if (email == null) return false;
|
||||
|
||||
String regex =
|
||||
"^[A-Za-z0-9_+&*-]+(?:\\.[A-Za-z0-9_+&*-]+)*@" +
|
||||
"(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\\.)+" +
|
||||
"[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?$";
|
||||
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(email);
|
||||
|
||||
@@ -39,7 +50,34 @@ 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;
|
||||
}
|
||||
|
||||
Pattern pattern = Pattern.compile("\\b\\d{2,4}[/-]\\d{1,2}[/-]\\d{1,4}\\b");
|
||||
Matcher matcher = pattern.matcher(string);
|
||||
|
||||
Map<String, DateTimeFormatter> formattersMap = new LinkedHashMap<>();
|
||||
|
||||
formattersMap.put("yyyy-MM-dd", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||
formattersMap.put("yyyy/M/d", DateTimeFormatter.ofPattern("yyyy/M/d"));
|
||||
formattersMap.put("M/d/yyyy", DateTimeFormatter.ofPattern("M/d/yyyy"));
|
||||
formattersMap.put("d/M/yyyy", DateTimeFormatter.ofPattern("d/M/yyyy"));
|
||||
|
||||
while (matcher.find()) {
|
||||
String candidate = matcher.group();
|
||||
for (Map.Entry<String, DateTimeFormatter> entry : formattersMap.entrySet()) {
|
||||
String patternStr = entry.getKey();
|
||||
DateTimeFormatter formatter = entry.getValue();
|
||||
|
||||
try {
|
||||
LocalDate.parse(candidate, formatter);
|
||||
return candidate;
|
||||
|
||||
} catch (DateTimeParseException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -54,8 +92,52 @@ public class BonusExercises {
|
||||
- has no white-space in it
|
||||
*/
|
||||
public int findValidPasswords(String string) {
|
||||
// todo
|
||||
return -1;
|
||||
if(string == null || string.isEmpty()){
|
||||
return 0;
|
||||
}
|
||||
int validPasswordCount = 0;
|
||||
Pattern pattern = Pattern.compile("[A-Za-z0-9!@#$%^&*]+");
|
||||
Matcher matcher = pattern.matcher(string);
|
||||
|
||||
while (matcher.find()){
|
||||
String candidate = matcher.group();
|
||||
if(isValidPassword(candidate)){
|
||||
validPasswordCount ++;
|
||||
}
|
||||
}
|
||||
|
||||
return validPasswordCount;
|
||||
}
|
||||
|
||||
public boolean isValidPassword(String password){
|
||||
if(password.length()<8){
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean hasUpperCase = false;
|
||||
boolean hasLowerCase = false;
|
||||
boolean hasDigit = false;
|
||||
boolean hasSpecialChar = false;
|
||||
String sepecialChras = "!@#$%^&*";
|
||||
|
||||
for( char c : password.toCharArray()){
|
||||
if(Character.isUpperCase(c)){
|
||||
hasUpperCase = true;
|
||||
}
|
||||
else if(Character.isLowerCase(c)){
|
||||
hasLowerCase = true;
|
||||
}
|
||||
else if(Character.isDigit(c)){
|
||||
hasDigit = true;
|
||||
}
|
||||
else if(sepecialChras.indexOf(c) != -1){
|
||||
hasSpecialChar = true;
|
||||
}
|
||||
else if (Character.isSpaceChar(c)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return hasUpperCase && hasLowerCase && hasDigit && hasSpecialChar;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -66,11 +148,37 @@ public class BonusExercises {
|
||||
*/
|
||||
public List<String> findPalindromes(String string) {
|
||||
List<String> list = new ArrayList<>();
|
||||
// todo
|
||||
|
||||
if (string == null || string.isEmpty()) {
|
||||
return list;
|
||||
}
|
||||
|
||||
String[] words = string.split("\\s+");
|
||||
|
||||
for(String word : words){
|
||||
word = word.replaceAll("^[^a-zA-Z0-9]+|[^a-zA-Z0-9]+$" , "");
|
||||
|
||||
if(word.length() >= 3 && isPalindrome(word)){
|
||||
list.add(word);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private boolean isPalindrome(String word){
|
||||
String lower = word.toLowerCase();
|
||||
int left = 0 ,right = lower.length()-1;
|
||||
|
||||
while(left < right) {
|
||||
if (lower.charAt(left) != lower.charAt(right)) {
|
||||
return false;
|
||||
}
|
||||
left++;
|
||||
right--;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// you can test your code here
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user