163 lines
4.9 KiB
Java
163 lines
4.9 KiB
Java
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
public class BonusExercises {
|
|
|
|
/*
|
|
complete the method below, so it will validate an email address
|
|
1. Must have exactly one @ (no more, no less)
|
|
2. Split into local-part and domain (before @ and after @)
|
|
3. Local-part rules:
|
|
- Can't be empty
|
|
- Can't start or end with dot
|
|
- Can't have two dots in a row
|
|
4. Domain rules:
|
|
- Can't be empty
|
|
- Can't start or end with hyphen
|
|
- Can't have underscores
|
|
- Each segment (between dots) must follow same hyphen rules
|
|
*/
|
|
public boolean validateEmail(String email) {
|
|
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-]*[a-zA-Z0-9])?)*$";
|
|
Pattern pattern = Pattern.compile(regex);
|
|
Matcher matcher = pattern.matcher(email);
|
|
|
|
return matcher.matches();
|
|
}
|
|
|
|
/*
|
|
This method should find and return the first date in a string.
|
|
|
|
Supported formats:
|
|
- American: MM/DD/YYYY (e.g., 12/09/2023)
|
|
- British: DD/MM/YYYY (e.g., 12/09/2023 — same pattern, context matters)
|
|
- ISO: YYYY-MM-DD (e.g., 2024-07-15)
|
|
- Slash variant: YYYY/MM/DD (e.g., 2025/01/01)
|
|
|
|
If no match for a date is found in the string, return null.
|
|
*/
|
|
public String findDate(String string) {
|
|
// String regex = "\\b(\\d{4}[/-]\\d{2}[/-]\\d{2}|\\d{2}[/-]\\d{2}[/-]\\d{4})\\b";
|
|
// Pattern pattern = Pattern.compile(regex);
|
|
// Matcher matcher = pattern.matcher(string);
|
|
// if (matcher.find())
|
|
// {
|
|
// return matcher.group();
|
|
// }
|
|
//Checking day-month compatibility!!!!!!
|
|
return null;
|
|
}
|
|
|
|
/*
|
|
given a string, implement the method to detect all valid passwords
|
|
then, it should return the count of them
|
|
|
|
a valid password has the following properties:
|
|
- at least 8 characters
|
|
- has to include at least one uppercase letter, and at least a lowercase
|
|
- at least one number and at least a special char "!@#$%^&*"
|
|
- has no white-space in it
|
|
*/
|
|
public int findValidPasswords(String string) {
|
|
if (string == null) {return 0;}
|
|
String[] words = string.split("\\s+");
|
|
int passwordCount = 0;
|
|
for (String word : words)
|
|
{
|
|
String cleanWord = cleanWord(word);
|
|
if (isValid(cleanWord))
|
|
{
|
|
passwordCount++;
|
|
}
|
|
}
|
|
return passwordCount;
|
|
}
|
|
|
|
private String cleanWord(String word) {
|
|
return word.replaceAll("[^a-zA-Z0-9!@#$%^&*]", "");
|
|
}
|
|
|
|
private boolean isValid(String word)
|
|
{
|
|
if (word.length() < 8)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
boolean hasUppercase = false;
|
|
boolean hasLowercase = false;
|
|
boolean hasSpecial = false;
|
|
boolean hasNumber = false;
|
|
String special = "!@#$%^&*";
|
|
|
|
for (int i = 0; i < word.length(); i++)
|
|
{
|
|
char c = word.charAt(i);
|
|
if (Character.isUpperCase(c))
|
|
{
|
|
hasUppercase = true;
|
|
}
|
|
else if (Character.isLowerCase(c))
|
|
{
|
|
hasLowercase = true;
|
|
}
|
|
else if (Character.isDigit(c))
|
|
{
|
|
hasNumber = true;
|
|
}
|
|
else if (special.indexOf(c) != -1)
|
|
{
|
|
hasSpecial = true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return hasNumber && hasLowercase && hasSpecial && hasUppercase;
|
|
}
|
|
|
|
/*
|
|
you should return a list of *words* which are palindromic
|
|
by word we mean at least 3 letters with no whitespace in it
|
|
|
|
note: your implementation should be case-insensitive, e.g. Aba -> is palindrome
|
|
*/
|
|
public List<String> findPalindromes(String string) {
|
|
List<String> list = new ArrayList<>();
|
|
Pattern pattern = Pattern.compile("[a-zA-Z]+");
|
|
Matcher matcher = pattern.matcher(string);
|
|
while (matcher.find())
|
|
{
|
|
String word = matcher.group();
|
|
if (word.length() >= 3) {
|
|
String lowerWord = word.toLowerCase();
|
|
if (isPalindrome(lowerWord)) {
|
|
list.add(word);
|
|
}
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
private boolean isPalindrome(String word)
|
|
{
|
|
boolean isPalindrome = true;
|
|
int length = word.length();
|
|
for (int i = 0; i < length/2; i++)
|
|
{
|
|
if (word.charAt(i) != word.charAt(length - i - 1))
|
|
{
|
|
isPalindrome = false;
|
|
}
|
|
}
|
|
return isPalindrome;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
// you can test your code here
|
|
}
|
|
}
|