From 80855777f26e796376293f17d34de9c9fd2439bc Mon Sep 17 00:00:00 2001 From: ZahraSadatMirvakili Date: Wed, 22 Apr 2026 22:46:46 +0330 Subject: [PATCH] workshop_one --- src/main/java/BonusExercises.java | 120 ++++++++++++++++++++++++++++-- src/main/java/MainExercises.java | 100 +++++++++++++++++++++++-- 2 files changed, 206 insertions(+), 14 deletions(-) diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index d02a746..41ccbd3 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -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 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 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 findPalindromes(String string) { List 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 } } diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..56a4eb8 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -1,3 +1,6 @@ +import java.util.ArrayList; +import java.util.List; + public class MainExercises { /* @@ -19,10 +22,16 @@ public class MainExercises the output has to be a two-dimensional array of characters, so don't just print the triangle! */ public char[][] generateTriangle(int n) { - - // todo - return null; - + char[][] triangle = new char[n][]; + for (int i = 0; i < n; i++) { + triangle[i] = new char[i+1]; + for (int j = 0; j <=i ; j++) { + if(i == 0 || j == 0 || i == j || i == n-1) + triangle[i][j] = '*'; + else triangle[i][j] = ' '; + } + } + return triangle; } @@ -58,8 +67,51 @@ public class MainExercises - Number of columns: matrix[0].length (if rectangular) */ public int[] spiralTraversal(int[][] matrix) { - // todo - return null; + + int rows = matrix.length; + int cols = matrix[0].length; + int total_element = rows * cols; + int [] result = new int[total_element]; + int index = 0; + + int top = 0; + int bottom = rows - 1; + int left = 0; + int right = cols - 1; + + //Direction codes : 0 = right, 1 = down , 2 = left , 3 = up + int direction = 0; + + while (top <= bottom && left <= right) { + switch (direction){ + case 0 : + for (int i = left; i <= right ; i++) { + result[index++] = (matrix[top][i]); + } + top++; + break; + case 1 : + for (int i = top; i <=bottom ; i++) { + result[index++] = (matrix[i][right]); + } + right--; + break; + case 2 : + for (int i = right; i >=left ; i--) { + result[index++] = (matrix[bottom][i]); + } + bottom--; + break; + case 3 : + for (int i = bottom; i >=top ; i--) { + result[index++] = (matrix[i][left]); + } + left++; + break; + } + direction = (direction + 1) % 4; + } + return result; } /* @@ -91,10 +143,42 @@ public class MainExercises */ public int[][] intPartitions(int n) { - // todo - return null; + List> allpartitions = new ArrayList<>(); + List currentpartition = new ArrayList<>(); + + findpartitionrecursive(n, n, currentpartition, allpartitions); + + return convertListToArray(allpartitions); } + private void findpartitionrecursive(int target, int max, List currentpartition, List> allpartitions) { + + if (target == 0) { + allpartitions.add(new ArrayList<>(currentpartition)); + return; + } + + + for (int i = Math.min(target, max); i >= 1; i--) { + currentpartition.add(i); + findpartitionrecursive(target - i, i, currentpartition, allpartitions); + currentpartition.remove(currentpartition.size() - 1); + } + } + + private int[][] convertListToArray(List> mylist) { + int[][] resultArray = new int[mylist.size()][]; + for (int i = 0; i < mylist.size(); i++) { + List innerList = mylist.get(i); + resultArray[i] = new int[innerList.size()]; + for (int j = 0; j < innerList.size(); j++) { + resultArray[i][j] = innerList.get(j); + } + } + return resultArray; + } + + public static void main() { -- 2.54.0