From 0a60c2e5e99db8e42c7d843bf0877082deddb609 Mon Sep 17 00:00:00 2001 From: Matin-Ardestani Date: Tue, 21 Apr 2026 16:37:54 +0330 Subject: [PATCH 01/10] Implement generateTriangle method --- src/main/java/MainExercises.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..b2d063d 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -20,8 +20,22 @@ 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 j = 0; j <= i; j++){ + if(i == 0 || j == 0 || i == n - 1 || j == i){ + triangle[i][j] = '*'; + } + else{ + triangle[i][j] = ' '; + } + } + } + + + return triangle; } From c59b80f87c544f69799202863d05686dcb5f5370 Mon Sep 17 00:00:00 2001 From: Matin-Ardestani Date: Tue, 21 Apr 2026 17:12:50 +0330 Subject: [PATCH 02/10] Implement Traversal method --- src/main/java/MainExercises.java | 44 ++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index b2d063d..2d9b518 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -72,8 +72,48 @@ 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[] result = new int[rows * cols]; + + int top = 0, bottom = rows - 1, left = 0, right = cols - 1; + int id = 0; + + while(top <= bottom && left <= right){ // reaching the center + + for(int col = left; col <= right; col++){ + result[id] = matrix[top][col]; + id++; + } + top++; + + for(int row = top; row <= bottom; row++){ + result[id] = matrix[row][right]; + id++; + } + right--; + + if(top <= right){ + for(int col = right; col >= left; col--){ + result[id] = matrix[bottom][col]; + id++; + } + bottom--; + } + + if(left <= right){ + for(int row = bottom; row >= top; row--){ + result[id] = matrix[row][left]; + id++; + } + left++; + } + + } + + return result; } /* From 3e85586939d8c626b50bcfc892cb8554439ace59 Mon Sep 17 00:00:00 2001 From: Matin-Ardestani Date: Wed, 22 Apr 2026 22:43:25 +0330 Subject: [PATCH 03/10] Implement intPartitions method --- src/main/java/MainExercises.java | 60 ++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 2d9b518..2ede0f1 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -1,3 +1,5 @@ +import java.util.ArrayList; + public class MainExercises { /* @@ -145,8 +147,62 @@ public class MainExercises */ public int[][] intPartitions(int n) { - // todo - return null; + + ArrayList result = new ArrayList<>(); + int[] current = new int[n]; // current partition, to be built + + // we use stack arrays to have the situation saved. + int[] remainingStack = new int[n*n]; // remaining value, to be added +// int[] maxValueStack = new int[n*n]; // maximum number we can use, to prevent repeating partitions + int[] nextTryStack = new int[n*n]; // the next number we try + int[] lenStack = new int[n*n]; // length of current partition, gives us the next index in current + int head = 0; // works like a pointer, it makes stack arrays make sense. + + remainingStack[0] = n; +// maxValueStack[0] = n; + nextTryStack[0] = n; + lenStack[0] = 0; + + while(head >= 0){ + int remaining = remainingStack[head]; +// int maxValue = maxValueStack[head]; + int nextTry = nextTryStack[head]; + int partitionLen = lenStack[head]; + + if(remaining == 0){ // partition has been completed + // + int[] partition = new int[partitionLen]; + System.arraycopy(current, 0, partition, 0, partitionLen); + result.add(partition); + head--; + continue; + } + + if(nextTry <= 0){ // no choices left + head--; + continue; + } + + int chosen = nextTry; + nextTryStack[head] = chosen - 1; // next value to be tried + current[partitionLen] = chosen; // adding selected number + + // generating next number for the partition + head++; + remainingStack[head] = remaining - chosen; +// maxValueStack[head] = chosen; + nextTryStack[head] = Math.min(chosen, remaining - chosen); + lenStack[head] = partitionLen + 1; + + } + + int[][] output = new int[result.size()][]; + for(int i = 0; i < result.size(); i++){ + output[i] = result.get(i); + } + + return output; + } From 2f72c0c8b6be233505daa8c0b78b51bd9f561552 Mon Sep 17 00:00:00 2001 From: Matin-Ardestani Date: Wed, 22 Apr 2026 23:00:21 +0330 Subject: [PATCH 04/10] debug Traversal method --- src/main/java/MainExercises.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 2ede0f1..79f6b46 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -78,6 +78,8 @@ public class MainExercises int rows = matrix.length; int cols = matrix[0].length; + if(rows == 1) return matrix[0]; + int[] result = new int[rows * cols]; int top = 0, bottom = rows - 1, left = 0, right = cols - 1; From aa6cdc766dc4ba98332f46c4d3f1190bad43c6b0 Mon Sep 17 00:00:00 2001 From: Matin-Ardestani Date: Wed, 22 Apr 2026 23:10:29 +0330 Subject: [PATCH 05/10] debug intPartitions method --- src/main/java/MainExercises.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 79f6b46..6b8400b 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -150,6 +150,10 @@ public class MainExercises public int[][] intPartitions(int n) { + if(n == 1){ + return new int[][]{{1}}; + } + ArrayList result = new ArrayList<>(); int[] current = new int[n]; // current partition, to be built From 11c748526eacdb902bd0a462e6dcf7fe572cd858 Mon Sep 17 00:00:00 2001 From: Matin-Ardestani Date: Wed, 22 Apr 2026 23:42:23 +0330 Subject: [PATCH 06/10] Implement validateEmail method --- src/main/java/BonusExercises.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index d02a746..6269b47 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -20,7 +20,7 @@ public class BonusExercises { - Each segment (between dots) must follow same hyphen rules */ public boolean validateEmail(String email) { - String regex = ""; // todo + String regex = "^[a-zA-z0-9_+-]+(?:\\.[a-zA-Z0-9_+-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z0-9-]+$"; // todo Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(email); From 00c7229a9d5496b98d64799fa627a80e72ac2d4d Mon Sep 17 00:00:00 2001 From: Matin-Ardestani Date: Thu, 23 Apr 2026 00:57:00 +0330 Subject: [PATCH 07/10] Implement findDate method --- src/main/java/BonusExercises.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index 6269b47..867d66e 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -20,7 +20,7 @@ public class BonusExercises { - Each segment (between dots) must follow same hyphen rules */ public boolean validateEmail(String email) { - String regex = "^[a-zA-z0-9_+-]+(?:\\.[a-zA-Z0-9_+-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z0-9-]+$"; // todo + String regex = "^[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 +39,15 @@ public class BonusExercises { If no match for a date is found in the string, return null. */ public String findDate(String string) { - // todo + String regex1 = "(0[1-9]|1[0-2])\\/(0[1-9]|[12][0-9]|30)\\/\\d{4}|(0[1-9]|[12][0-9]|30)\\/(0[1-9]|1[0-2])\\/\\d{4}"; + String regex2 = "(\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|30))"; + String regex3 = "(\\d{4}\\/(0[1-9]|1[0-2])\\/(0[1-9]|[12][0-9]|30))"; + Pattern pattern = Pattern.compile(regex1 + "|" + regex2 + "|" + regex3); + Matcher matcher = pattern.matcher(string); + + if(matcher.find()){ + return matcher.group(); + } return null; } From 42d26bcfa5941fdc07ae6a4e68f29d51f7dde125 Mon Sep 17 00:00:00 2001 From: Matin-Ardestani Date: Thu, 23 Apr 2026 01:27:37 +0330 Subject: [PATCH 08/10] Implement findValidPasswords method --- src/main/java/BonusExercises.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index 867d66e..e14ca3c 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -62,8 +62,18 @@ public class BonusExercises { - has no white-space in it */ public int findValidPasswords(String string) { - // todo - return -1; + String regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[!@#$%^&*]).{8,}$"; + Pattern pattern = Pattern.compile(regex); + + String[] parts = string.split("\\s+"); + int counter = 0; + for(String part : parts){ + Matcher matcher = pattern.matcher(part); + if(matcher.matches()){ + counter++; + } + } + return counter; } /* From 1389bf474963d6b20512883f2364d0717e86b54d Mon Sep 17 00:00:00 2001 From: Matin-Ardestani Date: Thu, 23 Apr 2026 01:44:43 +0330 Subject: [PATCH 09/10] Implement findPalindromes method --- src/main/java/BonusExercises.java | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index e14ca3c..729530b 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -84,10 +84,32 @@ public class BonusExercises { */ public List findPalindromes(String string) { List list = new ArrayList<>(); - // todo + + String[] words = string.split("\\s+|,"); + for(String word : words){ + if(word.length() >= 3 && isPalindrome(word)){ + list.add(word); + } + } + return list; } + private boolean isPalindrome(String string){ + boolean result = true; + int counter = 0; + while(counter + 1 <= string.length() / 2){ + char ch1 = string.charAt(counter); + char ch2 = string.charAt(string.length() - 1 - counter); + if(Character.toLowerCase(ch1) != Character.toLowerCase(ch2)){ + result = false; + break; + } + counter++; + } + return result; + } + public static void main(String[] args) { // you can test your code here } From 4a8f093faff25af386d86545656f547128efec03 Mon Sep 17 00:00:00 2001 From: Matin-Ardestani Date: Thu, 23 Apr 2026 02:28:50 +0330 Subject: [PATCH 10/10] Change algorithm of intPartitions method (recursive solution) --- src/main/java/MainExercises.java | 78 +++++++++++--------------------- 1 file changed, 27 insertions(+), 51 deletions(-) diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 6b8400b..b74b3a7 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -1,4 +1,5 @@ import java.util.ArrayList; +import java.util.List; public class MainExercises { @@ -150,64 +151,39 @@ public class MainExercises public int[][] intPartitions(int n) { - if(n == 1){ - return new int[][]{{1}}; - } + List> allPartitions = new ArrayList<>(); + ArrayList current = new ArrayList<>(); + generatePartition(n, n, current, allPartitions); - ArrayList result = new ArrayList<>(); - int[] current = new int[n]; // current partition, to be built - - // we use stack arrays to have the situation saved. - int[] remainingStack = new int[n*n]; // remaining value, to be added -// int[] maxValueStack = new int[n*n]; // maximum number we can use, to prevent repeating partitions - int[] nextTryStack = new int[n*n]; // the next number we try - int[] lenStack = new int[n*n]; // length of current partition, gives us the next index in current - int head = 0; // works like a pointer, it makes stack arrays make sense. - - remainingStack[0] = n; -// maxValueStack[0] = n; - nextTryStack[0] = n; - lenStack[0] = 0; - - while(head >= 0){ - int remaining = remainingStack[head]; -// int maxValue = maxValueStack[head]; - int nextTry = nextTryStack[head]; - int partitionLen = lenStack[head]; - - if(remaining == 0){ // partition has been completed - // - int[] partition = new int[partitionLen]; - System.arraycopy(current, 0, partition, 0, partitionLen); - result.add(partition); - head--; - continue; + // converting ArrayList of ArrayLists into a 2d array + int[][] result = new int[allPartitions.size()][]; + for (int i = 0; i < allPartitions.size(); i++) { + List sublist = allPartitions.get(i); + result[i] = new int[sublist.size()]; + for (int j = 0; j < sublist.size(); j++) { + result[i][j] = sublist.get(j); } + } + + return result; - if(nextTry <= 0){ // no choices left - head--; - continue; - } - - int chosen = nextTry; - nextTryStack[head] = chosen - 1; // next value to be tried - current[partitionLen] = chosen; // adding selected number - - // generating next number for the partition - head++; - remainingStack[head] = remaining - chosen; -// maxValueStack[head] = chosen; - nextTryStack[head] = Math.min(chosen, remaining - chosen); - lenStack[head] = partitionLen + 1; + } + private void generatePartition(int remaining, int maxValue, List currentPartition, List> allPartitions){ + if(remaining == 0){ + allPartitions.add(new ArrayList<>(currentPartition)); + return ; } - int[][] output = new int[result.size()][]; - for(int i = 0; i < result.size(); i++){ - output[i] = result.get(i); - } + for (int i = Math.min(maxValue, remaining); i >= 1; i--){ // preventing choosing the bigger number between maxValue and remaining, this way repeating partitions + currentPartition.add(i); + generatePartition(remaining - i, i, currentPartition, allPartitions); - return output; + currentPartition.removeLast(); /* + after the recursive call returns. we need to undo adding i. + this way we can try adding i again. ( remaining may not be 0 yet) + */ + } }