From 2d725cce52f7408c0f90cc03c40cb0a358ab017f Mon Sep 17 00:00:00 2001 From: Zahra Gharagozloo Mazlaghan Date: Wed, 22 Apr 2026 15:59:05 +0330 Subject: [PATCH 1/7] Main Exercise 1 completed. --- src/main/java/MainExercises.java | 29 +++++++++-------------------- src/test/java/MainTestTriangle.java | 2 +- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..a8fd7fb 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -1,28 +1,17 @@ public class MainExercises { - /* - you should create a triangle with "*" and return a two-dimensional array of characters based on that - the triangle's area is empty, which means some characters should be " " - - example 1, input = 3: - * - ** - *** - - example 2, input = 5: - * - ** - * * - * * - ***** - - 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][]; - // todo - return null; + for(int i=0; i Date: Wed, 22 Apr 2026 21:12:03 +0330 Subject: [PATCH 2/7] Main Exercise 2 completed. --- src/main/java/MainExercises.java | 49 ++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index a8fd7fb..a70f990 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -1,8 +1,12 @@ +import java.util.Arrays; +import java.util.ArrayList; + public class MainExercises { public char[][] generateTriangle(int n) { char[][] Triangle = new char[n][]; + //The for loop to put the stars a spaces in the array for(int i=0; i result = new ArrayList<>(); + + //Setting matrix edge indices + int top = 0; + int bottom = matrix.length - 1; + int left = 0; + int right = matrix[0].length - 1; + + //This loop reads the indices in a spiral order and at each stage, it brings the edges closer to the center. + while ((top <= bottom) && (left <= right)) { + for (int i=left; i <= right; i++) { + result.add(matrix[top][i]); + } + top++; //to getting closer to the center + + for (int i=top; i <= bottom; i++) { + result.add(matrix[i][right]); + } + right--; //to getting closer to the center + + if (top <= bottom) { + for (int i = right; i >= left; i--) { + result.add(matrix[bottom][i]); + } + bottom--; + } + + if (left <= right) { + for (int i = bottom; i >= top; i--) { + result.add(matrix[i][left]); + } + left++; + } + } + + int n = (matrix.length)*(matrix[0].length); + int[] finalResult = new int[n]; //for returning an int[] array + for (int i=0; i Date: Thu, 23 Apr 2026 14:25:40 +0330 Subject: [PATCH 3/7] Main Exercise 3 completed. --- src/main/java/MainExercises.java | 99 +++++++++++--------------------- 1 file changed, 35 insertions(+), 64 deletions(-) diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index a70f990..72e0acc 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -20,36 +20,6 @@ public class MainExercises - /* - SPIRAL TRAVERSAL OF A RECTANGULAR MATRIX - - Given a rectangular matrix (2D array) of integers, this method traverses - it in a spiral order (clockwise from outside to inside) and returns - the elements as a 1D array. - - EXAMPLE: - Input matrix: - 1 2 3 - 4 5 6 - 7 8 9 - - Spiral order: start at top-left (1), go right →, then down ↓, - then left ←, then up ↑, then repeat inward. - - Result: {1, 2, 3, 6, 9, 8, 7, 4, 5} - - so you should walk in that matrix in a curl and then add the numbers in order you've seen them in a 1D array - - RECTANGULAR MATRIX ASSUMPTION: - This method assumes the input matrix is RECTANGULAR (all rows have - the same number of columns). In Java, we can verify this because - 2D arrays might be jagged (rows of different lengths). - - IMPORTANT: In Java, we do NOT need to pass rows and cols! - The 2D array 'matrix' knows its own dimensions: - - Number of rows: matrix.length - - Number of columns: matrix[0].length (if rectangular) - */ public int[] spiralTraversal(int[][] matrix) { ArrayList result = new ArrayList<>(); @@ -86,8 +56,9 @@ public class MainExercises } } + //for returning an int[] array: int n = (matrix.length)*(matrix[0].length); - int[] finalResult = new int[n]; //for returning an int[] array + int[] finalResult = new int[n]; for (int i=0; i all partitions of integer 3 are: - 3 - 2, 1 - 1, 1, 1 - - e.g. 2 -> for number 4 goes as: - 4 - 3, 1 - 2, 2 - 2, 1, 1 - 1, 1, 1, 1 - - Note: As you can see in the examples, we want to generate distinct partitions, - which means 1,2 and 2,1 are not different — they count as the same combination. - - You should generate all partitions of the input number. - - Hint: You can determine the size and order of the arrays by finding the pattern - of partitions and their count. Trust me, this one's fun and easy :) - - If you're familiar with Lists and ArrayLists, you can also edit the method's - body to use them instead of arrays. - */ - public int[][] intPartitions(int n) { - // todo - return null; + ArrayList> partitions = new ArrayList<>(); + + class partitionRecursive { + void solve(int remaining, int max, ArrayList current) { + //End condition + if (remaining == 0) { + partitions.add(new ArrayList<>(current)); + return; + } + + //The main part of the algorithm + for (int i = Math.min(max, remaining); i>0; i--) { + current.add(i); + solve(remaining - i, i, current); + current.remove(current.size() - 1); + } + } + } + + new partitionRecursive().solve(n, n, new ArrayList<>()); + + //Making the result + int[][] result = new int[partitions.size()][]; + for (int i = 0; i < partitions.size(); i++) { + ArrayList partition = partitions.get(i); + result[i] = new int[partition.size()]; + for (int j = 0; j < partition.size(); j++) { + result[i][j] = partition.get(j); + } + } + + return result; } public static void main() - { - - } + {} } \ No newline at end of file -- 2.54.0 From 621dfb4474e0e24d6df0836351ae008bbf5fc725 Mon Sep 17 00:00:00 2001 From: Zahra Gharagozloo Mazlaghan Date: Thu, 23 Apr 2026 21:09:54 +0330 Subject: [PATCH 4/7] Bonus Exercise 1 completed. --- src/main/java/BonusExercises.java | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index d02a746..c682bc7 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -5,22 +5,10 @@ import java.util.regex.Pattern; public class BonusExercises { - /* - complete the method below, so it will validate an email address - 1. Must have exactly one @ (no more, no less) - 2. Split into local-part and domain (before @ and after @) - 3. Local-part rules: - - Can't be empty - - Can't start or end with dot - - Can't have two dots in a row - 4. Domain rules: - - Can't be empty - - Can't start or end with hyphen - - Can't have underscores - - Each segment (between dots) must follow same hyphen rules - */ public boolean validateEmail(String email) { - String regex = ""; // todo + //This string matches the conditions for the email + 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); -- 2.54.0 From cceedd95ba1f32b6245e80a56a130bc04710fd34 Mon Sep 17 00:00:00 2001 From: Zahra Gharagozloo Mazlaghan Date: Thu, 23 Apr 2026 21:23:49 +0330 Subject: [PATCH 5/7] Bonus Exercise 2 completed. --- src/main/java/BonusExercises.java | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index c682bc7..e85ec4c 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -27,7 +27,31 @@ public class BonusExercises { If no match for a date is found in the string, return null. */ public String findDate(String string) { - // todo + //Different types of dates + String[] datePatterns = { + //American: MM/DD/YYYY + "\\b(0[1-9]|1[0-2])/(0[1-9]|1[0-9]|2[0-9]|3[0-1])/(\\d{4})\\b", + + //British: DD/MM/YYYY + "\\b(0[1-9]|1[0-9]|2[0-9]|3[0-1])/(0[1-9]|1[0-2])/(\\d{4})\\b", + + //ISO: YYYY-MM-DD + "\\b(\\d{4})-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|3[0-1])\\b", + + //Slash variant: YYYY/MM/DD + "\\b(\\d{4})/(0[1-9]|1[0-2])/(0[1-9]|1[0-9]|2[0-9]|3[0-1])\\b" + }; + + for (String pattern : datePatterns) { + Pattern p = Pattern.compile(pattern); + Matcher m = p.matcher(string); + + //To return the first date + if (m.find()) { + return m.group(); + } + } + return null; } -- 2.54.0 From aac792e1f2842bb43cc912e547e1a9f6783e93af Mon Sep 17 00:00:00 2001 From: Zahra Gharagozloo Mazlaghan Date: Thu, 23 Apr 2026 21:47:39 +0330 Subject: [PATCH 6/7] Bonus Exercise 3 completed. --- src/main/java/BonusExercises.java | 40 +++++++++++++++---------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index e85ec4c..ee0b1a0 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -15,17 +15,7 @@ public class BonusExercises { return matcher.matches(); } - /* - This method should find and return the first date in a string. - Supported formats: - - American: MM/DD/YYYY (e.g., 12/09/2023) - - British: DD/MM/YYYY (e.g., 12/09/2023 — same pattern, context matters) - - ISO: YYYY-MM-DD (e.g., 2024-07-15) - - Slash variant: YYYY/MM/DD (e.g., 2025/01/01) - - If no match for a date is found in the string, return null. - */ public String findDate(String string) { //Different types of dates String[] datePatterns = { @@ -55,19 +45,27 @@ public class BonusExercises { return null; } - /* - given a string, implement the method to detect all valid passwords - then, it should return the count of them - a valid password has the following properties: - - at least 8 characters - - has to include at least one uppercase letter, and at least a lowercase - - at least one number and at least a special char "!@#$%^&*" - - has no white-space in it - */ public int findValidPasswords(String string) { - // todo - return -1; + //This string matches the conditions for the password + String regex = "^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[!@#$%^&*])(?!.*\\s).{8,}$"; + + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(string); + + //Splitting the string by the spaces to check each one + String[] words = string.split("\\s+"); + + int n = 0; + + //For counting the matches in the string array + for (String word : words) { + if (pattern.matcher(word).matches()) { + n++; + } + } + + return n; } /* -- 2.54.0 From e2a62ea34eeb8004e0cc6e1d50f94f3e66c90f47 Mon Sep 17 00:00:00 2001 From: Zahra Gharagozloo Mazlaghan Date: Thu, 23 Apr 2026 22:00:26 +0330 Subject: [PATCH 7/7] Bonus Exercise 4 completed. --- src/main/java/BonusExercises.java | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index ee0b1a0..61c0c60 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -76,11 +76,27 @@ public class BonusExercises { */ public List findPalindromes(String string) { List list = new ArrayList<>(); - // todo + + String regex = "[^a-zA-Z]+"; + + //Splitting the string by anything except words to check each one + String[] words = string.split(regex); + + for (String word : words) { + if (word.length() >= 3) { + //Case-insensitive + String lower = word.toLowerCase(); + String reversed = new StringBuilder(lower).reverse().toString(); + + //Checking if it's palindrome or not + if (lower.equals(reversed)) { + list.add(word); + } + } + } + return list; } - public static void main(String[] args) { - // you can test your code here - } + public static void main(String[] args) {} } -- 2.54.0