From 596e964956b4ee2f3e89c150dd486dc8bb02cfea Mon Sep 17 00:00:00 2001 From: "amirM.t" Date: Wed, 22 Apr 2026 19:20:43 +0330 Subject: [PATCH] complete second assignment --- src/main/java/BonusExercises.java | 142 +++++++++++++++++++++++++----- src/main/java/MainExercises.java | 116 ++++++++++++++++++++++-- 2 files changed, 226 insertions(+), 32 deletions(-) diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index d02a746..529f91e 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -20,11 +20,27 @@ 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); + if (email == null) return false; - return matcher.matches(); + // Explanation: + // ^ start + // (?!\\.) - cannot start with dot + // (?!.*\\.{2}) - cannot contain consecutive dots + // [A-Za-z0-9._%+-]+ - allowed local chars + // (? is palindrome - */ - public List findPalindromes(String string) { + note: your implementation should be case-insensitive, e.g. Aba -> is palindrome + */ + public List findPalindromes(String string) + { List list = new ArrayList<>(); - // todo + if (string == null) return list; + + Matcher matcher = Pattern.compile("\\b[A-Za-z]{3,}\\b").matcher(string); + + while (matcher.find()) + { + String word = matcher.group(); + + boolean isPalindrome = new StringBuilder(word).reverse().toString().equalsIgnoreCase(word); + + if (isPalindrome) + list.add(word); + } + return list; } public static void main(String[] args) { - // you can test your code here + BonusExercises b = new BonusExercises(); + + System.out.println(b.validateEmail("user.name+tag@sub-domain.co.uk")); // true + System.out.println(b.validateEmail(".bad@domain.com")); // false + System.out.println(b.findDate("Today's date is 2024-07-15.")); // 2024-07-15 + System.out.println(b.findDate("event on 12/09/2023 in US format")); // 12/09/2023 + System.out.println(b.findValidPasswords("Abcdef1! strongP@ss2 WeakPass noSpacePass3!")); + System.out.println(b.findPalindromes("Level noon Kayak test civic radar rotor deed pop")); } } diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..123c2ef 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 { /* @@ -18,15 +21,33 @@ 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) { + public char[][] generateTriangle(int n) + { + if (n <= 0) + return new char[0][0]; - // todo - return null; + char[][] triangle = new char[n][]; + for (int row = 0; row < n; row++) + { + triangle[row] = new char[row + 1]; + + for (int col = 0; col < row + 1; col++) + { + if((row == col) || (row == n-1) || (col == 0)) + triangle[row][col] = '*'; + else + triangle[row][col] = ' '; + } + } + + return triangle; } + + /* SPIRAL TRAVERSAL OF A RECTANGULAR MATRIX @@ -57,11 +78,57 @@ public class MainExercises - Number of rows: matrix.length - Number of columns: matrix[0].length (if rectangular) */ - public int[] spiralTraversal(int[][] matrix) { - // todo - return null; + public int[] spiralTraversal(int[][] matrix) + { + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) + return new int[0]; + + int rows = matrix.length; + int cols = matrix[0].length; + + int[] result = new int[rows * cols]; + int index = 0; + + int top = 0, bottom = rows - 1; + int left = 0, right = cols - 1; + + while (top <= bottom && left <= right) + { + // move right + for (int c = left; c <= right; c++) + result[index++] = matrix[top][c]; + + top++; + + // move down + for (int r = top; r <= bottom; r++) + result[index++] = matrix[r][right]; + + right--; + + if (top <= bottom) + { + // move left + for (int c = right; c >= left; c--) + result[index++] = matrix[bottom][c]; + + bottom--; + } + + if (left <= right) + { + // move up + for (int r = bottom; r >= top; r--) + result[index++] = matrix[r][left]; + + left++; + } + } + + return result; } + /* integer partitioning is a combinatorics problem in discreet maths the problem is to generate sum numbers which their summation is the input number @@ -90,11 +157,42 @@ public class MainExercises body to use them instead of arrays. */ - public int[][] intPartitions(int n) { - // todo - return null; + public int[][] intPartitions(int n) + { + List> result = new ArrayList<>(); + + generatePartitions(n, n, new ArrayList<>(), result); + + int[][] arr = new int[result.size()][]; + + for (int i = 0; i < result.size(); i++) + { + List part = result.get(i); + arr[i] = new int[part.size()]; + + for (int j = 0; j < part.size(); j++) + arr[i][j] = part.get(j); + } + return arr; } + private void generatePartitions(int remaining, int max, List current, List> result) + { + if (remaining == 0) + { + result.add(new ArrayList<>(current)); + return; + } + + for (int i = Math.min(max, remaining); i >= 1; i--) + { + current.add(i); + generatePartitions(remaining - i, i, current, result); + current.remove(current.size() - 1); + } + } + + public static void main() {