diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index d02a746..fac11c8 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -5,68 +5,84 @@ 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 = ""; // todo + String regex = "^(?!\\.)(?!.*\\.\\..*)([0-9A-Za-z._]+)(? is palindrome - */ public List findPalindromes(String string) { List list = new ArrayList<>(); - // todo + String[] inputList = string.split("\\W+"); + + for (String word : inputList) + { + String regex = ""; + int wordLength = word.length(); + + if (wordLength < 3) continue; + + for (int i = 0; i < wordLength/2; i++) { + regex += "(.)"; + } + + if (wordLength % 2 != 0) + { + regex += "."; + } + + for (int i = wordLength/2; i > 0; i--) { + regex += "\\" + i; + } + + Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(word); + + if (matcher.matches()) + { + list.add(word); + } + } + return list; }