From ac31fc381482a89c5f5edff85567751888bc6aad Mon Sep 17 00:00:00 2001 From: mohammadreza Date: Fri, 24 Apr 2026 01:39:51 -0700 Subject: [PATCH 1/2] add MainExercises.java and BonusExercise.java --- src/main/java/BonusExercises.java | 121 ++++++++++++++++++++++++++++-- src/main/java/MainExercises.java | 116 ++++++++++++++++++++++++++-- 2 files changed, 222 insertions(+), 15 deletions(-) diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index d02a746..f63c1f4 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -1,3 +1,6 @@ +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; @@ -20,11 +23,51 @@ public class BonusExercises { - Each segment (between dots) must follow same hyphen rules */ public boolean validateEmail(String email) { - String regex = ""; // todo - Pattern pattern = Pattern.compile(regex); - Matcher matcher = pattern.matcher(email); - return matcher.matches(); + + if (email.contains(" ")) + return false; + + int count = 0; + int index = 0; + + // rule 1: + for (int i = 0; i < email.length() -1; i++) { + + if (email.charAt(i) == '@') { + count++; + index = i; + } + } + + if (count != 1) + return false; + + + + + // rule 2: + String localPart = email.substring(0,index); + String domainPart = email.substring(index + 1); + + + if (localPart.isEmpty() || domainPart.isEmpty()) + return false; + + if (localPart.startsWith(".") || localPart.endsWith(".") || + domainPart.startsWith(".") || domainPart.endsWith(".")) + return false; + + if (localPart.contains("..")) + return false; + + if (domainPart.contains("_")) + return false; + + + + + return true; } /* @@ -39,7 +82,7 @@ public class BonusExercises { If no match for a date is found in the string, return null. */ public String findDate(String string) { - // todo + return null; } @@ -54,8 +97,42 @@ public class BonusExercises { - has no white-space in it */ public int findValidPasswords(String string) { - // todo - return -1; + + if (string.length() < 8) + return 0; + + int validPassWords = 0; + + List subStrings = List.of(string.split("\\s+")); + + + for (String newPart : subStrings) { + + //at least 8 characters + if (newPart.length() < 8) + continue; + + + // at least one uppercase letter + if (!newPart.matches(".*[A-Z].*")) + continue; + + // and at least a lowercase + if (!newPart.matches(".*[a-z].*")) + continue; + + // at least one number + if (!newPart.matches(".*\\d.*")) + continue; + + // at least one spacial character + if (!newPart.matches(".*[!@#$%^&*].*")) + continue; + + validPassWords++; + } + + return validPassWords; } /* @@ -66,11 +143,39 @@ public class BonusExercises { */ public List findPalindromes(String string) { List list = new ArrayList<>(); - // todo + + Pattern words = Pattern.compile("[a-zA-Z]+"); + Matcher matcher = words.matcher(string); + + while (matcher.find()) { + + String word = matcher.group(); + + if (word.length() < 2) + continue; + + String reversed = new StringBuilder(word).reverse().toString(); + + if (reversed.equalsIgnoreCase(word)) + list.add(word); + + } + return list; } public static void main(String[] args) { // you can test your code here + + BonusExercises kir = new BonusExercises(); + + System.out.println(kir.findValidPasswords(""" + [09:15] Dev1: Just changed my password to CodeMaster@2025. \s + [09:17] Dev2: Haha, mine's still qwerty123, no special chars. \s + [09:19] Dev3: I use GitHubSuper#1 but need a better one. \s + [09:21] Dev4: AdminPass42! is good, right? \s + [09:23] Dev5: No, too simple. I switched to UltraSecure$99 last week. \s + [09:25] Dev6: Wait, are we sharing passwords here? \uD83D\uDE02 \s + """)); } } diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..d578dfb 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 { /* @@ -20,8 +23,23 @@ public class MainExercises */ 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 i = 0 ; i < n ; i++) { + + for (int j = 0 ; j <= i ; j++) { + + if (i == 0 || i == n - 1 || j == 0 || j == i) + triangle[i][j] = '*'; + else + triangle[i][j] = ' '; + } + } + + return triangle; } @@ -58,10 +76,51 @@ public class MainExercises - Number of columns: matrix[0].length (if rectangular) */ public int[] spiralTraversal(int[][] matrix) { - // todo - return null; + + if ((matrix == null) || (matrix.length == 0) || (matrix[0].length == 0)) { + return new int[0]; + } + + int rows = matrix.length; + int cols = matrix[0].length; + List resultList = new ArrayList<>(); + + int top = 0, bottom = rows - 1, left = 0, right = cols - 1; + + while (top <= bottom && left <= right) { + + for (int i = left; i <= right; i++) { + resultList.add(matrix[top][i]); + } + top++; + for (int i = top; i <= bottom; i++) { + resultList.add(matrix[i][right]); + } + right--; + + if (top <= bottom) { + for (int i = right; i >= left; i--) { + resultList.add(matrix[bottom][i]); + } + bottom--; + } + + if (left <= right) { + for (int i = bottom; i >= top; i--) { + resultList.add(matrix[i][left]); + } + left++; + } + } + int[] resultArray = new int[resultList.size()]; + for (int i = 0; i < resultList.size(); i++) { + resultArray[i] = resultList.get(i); + } + + return resultArray; } + /* integer partitioning is a combinatorics problem in discreet maths the problem is to generate sum numbers which their summation is the input number @@ -90,13 +149,56 @@ public class MainExercises body to use them instead of arrays. */ + public int[][] intPartitions(int n) { - // todo - return null; + if (n <= 0) { + return new int[0][0]; + } + + List> allPartitions = new ArrayList<>(); + List newPartition = new ArrayList<>(); + + + generatePartitions(n, n, newPartition, allPartitions); // using recursion + + + int[][] result = new int[allPartitions.size()][]; + + // Convert List> to int[][] + for (int i = 0; i < allPartitions.size(); i++) { + + List partition = allPartitions.get(i); + result[i] = new int[partition.size()]; + + for (int j = 0; j < partition.size(); j++) { + result[i][j] = partition.get(j); + } + } + return result; + } + + private void generatePartitions(int n, int maxVal, List newPartition, List> allPartitions) { + // Base case: If n becomes 0, we have found a valid partition + if (n == 0) { + // Add a copy of the current partition to the list of all partitions + allPartitions.add(new ArrayList<>(newPartition)); + return; + } + + for (int i = Math.min(n, maxVal); i >= 1; i--) { + // Add the current number 'i' to the partition + newPartition.add(i); + + // The new maxVal becomes 'i' because the next number cannot be larger than 'i' + generatePartitions(n - i, i, newPartition, allPartitions); + + // Remove the last added number 'i' to explore other possibilities + newPartition.removeLast(); + } } - public static void main() + static void main() { } From c6817075362f37a6b4c90af13c740d07db20b68d Mon Sep 17 00:00:00 2001 From: mohammadreza Date: Fri, 24 Apr 2026 01:52:15 -0700 Subject: [PATCH 2/2] add BonusExercises.java --- src/main/java/BonusExercises.java | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index f63c1f4..9913f4f 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -166,16 +166,6 @@ public class BonusExercises { public static void main(String[] args) { // you can test your code here - - BonusExercises kir = new BonusExercises(); - - System.out.println(kir.findValidPasswords(""" - [09:15] Dev1: Just changed my password to CodeMaster@2025. \s - [09:17] Dev2: Haha, mine's still qwerty123, no special chars. \s - [09:19] Dev3: I use GitHubSuper#1 but need a better one. \s - [09:21] Dev4: AdminPass42! is good, right? \s - [09:23] Dev5: No, too simple. I switched to UltraSecure$99 last week. \s - [09:25] Dev6: Wait, are we sharing passwords here? \uD83D\uDE02 \s - """)); + } }