From 42152d683b8f1fa61be5610808c9ed18667e459f Mon Sep 17 00:00:00 2001 From: ShayanEdalatjoo Date: Fri, 17 Jul 2026 22:37:38 +0330 Subject: [PATCH] Finishing bonus parts --- src/main/java/BonusExercises.java | 116 ++++++++++++++++++++++++++++-- 1 file changed, 110 insertions(+), 6 deletions(-) diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index d02a746..ce862f9 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -20,7 +20,7 @@ public class BonusExercises { - Each segment (between dots) must follow same hyphen rules */ public boolean validateEmail(String email) { - String regex = ""; // todo + String regex = "^[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); @@ -38,11 +38,31 @@ public class BonusExercises { If no match for a date is found in the string, return null. */ - public String findDate(String string) { - // todo + public String findDate(String text) { + if (text == null || text.isBlank()) { + return null; + } + + String dateRegex = + "\\b(?:" + + "\\d{4}[-/](?:0[1-9]|1[0-2])[-/](?:0[1-9]|[12]\\d|3[01])" + + "|" + + "(?:0[1-9]|[12]\\d|3[01])/(?:0[1-9]|1[0-2])/\\d{4}" + + "|" + + "(?:0[1-9]|1[0-2])/(?:0[1-9]|[12]\\d|3[01])/\\d{4}" + + ")\\b"; + + Matcher matcher = Pattern.compile(dateRegex).matcher(text); + + if (matcher.find()) { + return matcher.group(); + } + return null; } + + /* given a string, implement the method to detect all valid passwords then, it should return the count of them @@ -54,8 +74,60 @@ public class BonusExercises { - has no white-space in it */ public int findValidPasswords(String string) { - // todo - return -1; + int count = 0; + + for (int i = 0; i <= string.length() - 8;) { + + int bestJ = -1; + + for (int j = i + 8; j <= string.length(); j++) { + String tempPassword = string.substring(i,j); + + if (tempPassword.contains(" ")){ + continue; + } + + boolean hasSpecialCharacter = false; + boolean hasDigit = false; + boolean hasLowercase = false; + boolean hasUppercase = false; + + + String specialChars = "!@#$%^&*"; + String digits = "1234567890"; + String lowerCases = "abcdefghijklmnopqrstuvwxyz"; + String upperCases = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + for (int k = 0; k < tempPassword.length(); k++) { + + char currentChar = tempPassword.charAt(k); + + if (specialChars.contains(String.valueOf(currentChar))){ + hasSpecialCharacter = true; + } else if (upperCases.contains(String.valueOf(currentChar))) { + hasUppercase = true; + } else if (lowerCases.contains(String.valueOf(currentChar))) { + hasLowercase = true; + } else if (digits.contains(String.valueOf(currentChar))) { + hasDigit = true; + } + } + + if (hasDigit && hasLowercase && hasUppercase && hasSpecialCharacter){ + count++; + i = j; + bestJ = j; + break; + } + + } + + if (bestJ == -1){ + i++; + } + + } + return count; } /* @@ -64,9 +136,41 @@ public class BonusExercises { note: your implementation should be case-insensitive, e.g. Aba -> is palindrome */ + public boolean isPalindrome(String word) { + if (word.length() == 1){ + return false; + } + String lowerWord = word.toLowerCase(); + StringBuilder mutableInverse = new StringBuilder(lowerWord.length()); + + for (int i = lowerWord.length() - 1; i >= 0; i--){ + mutableInverse.append(lowerWord.charAt(i)); + } + + String inverseOfWord = mutableInverse.toString(); + return lowerWord.equals(inverseOfWord); + } + public List findPalindromes(String string) { List list = new ArrayList<>(); - // todo + String[] wordsArray = string.split("\\s+"); + + + for (String s : wordsArray){ + + String finalS = s; + if (finalS.endsWith(",")) { + finalS = finalS.substring(0, finalS.length() - 1); + } + if (finalS.isEmpty()) { + continue; + } + + if (isPalindrome(finalS)){ + list.add(finalS); + } + } + return list; }