From ff2e308e56c137c850ce7c404a97063719f59764 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 16 Jun 2026 13:46:28 +0330 Subject: [PATCH] HW 2 --- .idea/.name | 1 + src/main/java/BonusExercises.java | 127 +++++++++++++++++++++++-- src/main/java/MainExercises.java | 150 ++++++++++++++++++++++++++++-- 3 files changed, 263 insertions(+), 15 deletions(-) create mode 100644 .idea/.name diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..30a3ff1 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +MainExercises.java \ No newline at end of file diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index d02a746..4dfb080 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -3,6 +3,10 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +/* First Name and Last Name: Hesam Ghazi + Student Number: 403222015 + */ + public class BonusExercises { /* @@ -20,10 +24,18 @@ public class BonusExercises { - Each segment (between dots) must follow same hyphen rules */ public boolean validateEmail(String email) { - String regex = ""; // todo + + String regex = + "^(?!.*\\.\\.)" + // no consecutive dots + "[^.][A-Za-z0-9._%+-]*[^.]@" + // local part + "(?!-)" + // domain can't start with - + "[A-Za-z0-9-]+" + + "(\\.[A-Za-z0-9-]+)*" + // domain segments + "(? is palindrome */ public List findPalindromes(String string) { + List list = new ArrayList<>(); - // todo + // Split text into words + String[] words = string.split("\\s+"); + + for (String word : words) { + + // remove punctuation around words + String cleaned = word.replaceAll("[^A-Za-z]", ""); + + if (cleaned.length() < 3) { + continue; + } + + String lower = cleaned.toLowerCase(); + + boolean palindrome = true; + + for (int i = 0; i < lower.length() / 2; i++) { + if (lower.charAt(i) != lower.charAt(lower.length() - 1 - i)) { + palindrome = false; + break; + } + } + + if (palindrome) { + list.add(cleaned); + } + } + return list; } + public static void main(String[] args) { - // you can test your code here + + BonusExercises ex = new BonusExercises(); + + System.out.println(ex.validateEmail("john.doe@gmail.com")); + System.out.println(ex.validateEmail(".john@gmail.com")); + + System.out.println( + ex.findDate("Meeting on 2025-07-15 at noon")); + + System.out.println( + ex.findValidPasswords( + "abc Test123! Password1@ weakpass HELLO123!")); + + System.out.println( + ex.findPalindromes( + "Anna level racecar hello Madam kayak")); } } diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..ab869ae 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -1,4 +1,10 @@ +import java.util.List; +import java.util.ArrayList; public class MainExercises + + /* first Name and last Name: Hesam Ghazi + Student Number: 403222015 + */ { /* you should create a triangle with "*" and return a two-dimensional array of characters based on that @@ -19,9 +25,43 @@ 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) { + char[][] triangle = new char[n][]; +// For n <= 3 the examples show a completely filled triangle. + if (n <= 3) { + for (int i = 0; i < n; i++) { + // Allocate row i with length i+1 + triangle[i] = new char[i + 1]; + for (int j = 0; j <= i; j++) { + triangle[i][j] = '*'; + } + } + return triangle; + } + // For n > 3, i build a hollow triangle + for (int i = 0; i < n; i++) { + // Allocate row i + triangle[i] = new char[i + 1]; + // Initialize all positions with spaces + for (int j = 0; j <= i; j++) { + triangle[i][j] = ' '; + } + // First row contains only one star + if (i == 0) { + triangle[i][0] = '*'; + // Last row is completely filled with stars + } else if (i == n - 1) { + for (int j = 0; j <= i; j++) { + triangle[i][j] = '*'; + } + } + // Middle rows contain stars only at the boundaries + else { + triangle[i][0] = '*'; + triangle[i][i] = '*'; + } + } - // todo - return null; + return triangle; } @@ -58,8 +98,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) { + return new int[0]; + } + + int rows = matrix.length; + int cols = matrix[0].length; + // Result array will contain every matrix element exactly once + int[] result = new int[rows * cols]; + int index = 0; + // Here i define the current boundaries of the spiral + int top = 0; + int bottom = rows - 1; + int left = 0; + int right = cols - 1; + + while (top <= bottom && left <= right) { + // Traverse top row from left to right + for (int j = left; j <= right; j++) { + result[index++] = matrix[top][j]; + } + top++; + // Traverse right column from top to bottom + for (int i = top; i <= bottom; i++) { + result[index++] = matrix[i][right]; + } + right--; + // Traverse bottom row from right to left + // Only if a valid row remains + if (top <= bottom) { + for (int j = right; j >= left; j--) { + result[index++] = matrix[bottom][j]; + } + bottom--; + } + // Traverse left column from bottom to top + // Only if a valid column remains + if (left <= right) { + for (int i = bottom; i >= top; i--) { + result[index++] = matrix[i][left]; + } + left++; + } + } + + return result; } /* @@ -91,13 +174,64 @@ public class MainExercises */ public int[][] intPartitions(int n) { - // todo - return null; + // Stores all partitions that are generated + List partitions = new ArrayList<>(); + // Start recursive generation + generatePartitions(n, n, new ArrayList<>(), partitions); + + int[][] result = new int[partitions.size()][]; + + for (int i = 0; i < partitions.size(); i++) { + result[i] = partitions.get(i); + } + + return result; + } + + private void generatePartitions( + int remaining, + int max, + List current, + List result) { + + if (remaining == 0) { + + int[] partition = new int[current.size()]; + + for (int i = 0; i < current.size(); i++) { + partition[i] = current.get(i); + } + + result.add(partition); + 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() - { + public static void main(String[] args) { + MainExercises ex = new MainExercises(); + + int[][] parts = ex.intPartitions(4); + + for (int[] p : parts) { + for (int x : p) { + System.out.print(x + " "); + } + System.out.println(); + } } } \ No newline at end of file