From 67739f36c5eb9da0e648c8f5695322a6b961fa8e Mon Sep 17 00:00:00 2001 From: Matin Date: Thu, 23 Apr 2026 04:52:02 +0430 Subject: [PATCH 1/2] matin mortazavi / exercises + bonus --- src/main/java/BonusExercises.java | 200 +++++++++++++++++++++++++++++- src/main/java/MainExercises.java | 105 ++++++++++++++-- 2 files changed, 292 insertions(+), 13 deletions(-) diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index d02a746..7fa393b 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -21,10 +21,99 @@ public class BonusExercises { */ public boolean validateEmail(String email) { String regex = ""; // todo + int indexAt = 0; + int Atnum = 0 ; + int datnum = 0 ; + int dat1 = 0; + int dat2 = 0 ; + int emsize = email.length() ; + for (int i = 0 ; i < email.length(); i ++) + { + if (email.charAt(i) == '@') + { + indexAt = i ; + Atnum ++ ; + + } + if (email.charAt(i) == ' ') + { + return false ; + } + } + if (Atnum != 1) + { + return false ; + } + if(indexAt == 0) + { + return false ; + } + if (indexAt == 1 && email.charAt(0) == ' ') + { + return false ; + } + if (email.charAt(0) == '.' || email.charAt(Atnum-1) == '.') + { + return false ; + } + for (int i = 0 ; i 1) + { + return false ; + } + if(indexAt== (emsize-1)) + { + return false ; + } + if (indexAt == (emsize-2) && email.charAt(emsize-1) == ' ' ) + { + return false ; + } + if (email.charAt(indexAt+1) == '-' || email.charAt(emsize-1) == '-') + { + return false ; + } + for (int i = indexAt ; i =8) + { + len = true ; + } + i = j-1 ; + if (len && check_lower && check_number && check_special && check_upper) + { + FinalNumber ++ ; + } + } + + + } + return FinalNumber; } /* @@ -67,6 +233,34 @@ public class BonusExercises { public List findPalindromes(String string) { List list = new ArrayList<>(); // todo + + int size = string.length() ; + for (int i = 0 ; i < size ; i ++) + { + if(string.charAt(i)==' ') continue; + String pal = "" ; + String pal2 = "" ; + String pal3 = "" ; + String spe = "!?.*," ; + int j = i ; + + while (j < size && string.charAt(j) != ' ' ) { + char y = string.charAt(j) ; + if (Character.isLetter(y)) { + pal3 = pal3 + y; + pal = Character.toLowerCase(y) + pal; + pal2 = pal2 + Character.toLowerCase(y); + } + j++ ; + + + } + if (pal2.equals(pal) && pal3.length()>=3) + { + list.add(pal3) ; + } + i = j - 1 ; + } return list; } diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..6ed7193 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -1,5 +1,7 @@ -public class MainExercises -{ +import java.util.ArrayList ; +import java.util.List; + +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 " " @@ -20,13 +22,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 j = 0; j <= i; j++) { + if (i == 0 || j == 0 || i == j || i == (n - 1)) { + triangle[i][j] = '*'; + } else { + triangle[i][j] = ' '; + } + } + } + + return triangle; } - /* SPIRAL TRAVERSAL OF A RECTANGULAR MATRIX @@ -57,11 +69,52 @@ 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) { + int rows = matrix.length; + int col = matrix[0].length; + + int size = 0 ; + boolean increase = true; + int range = rows * col ; + int check = 0 ; + int UP = 0 ; + int DOWN = rows-1 ; + int LEFT = 0 ; + int RIGHT = col -1; + + int[] newmat = new int[rows*col] ; + while (UP <= DOWN && LEFT<= RIGHT) { + for (int j = LEFT; j <= RIGHT; j++) { + newmat[size] = matrix[UP][j]; + size++; + } + + UP++; + for (int i = UP; i <= DOWN; i++) { + newmat[size] = matrix[i][RIGHT ]; + size++; + } + RIGHT--; + if(UP <= DOWN) { + for (int j = RIGHT ; j >= LEFT; j--) { + newmat[size] = matrix[DOWN ][j]; + size++; + } + DOWN--; + } + if(LEFT <= RIGHT) { + for (int i = DOWN ; i >= UP; i--) { + newmat[size] = matrix[i][LEFT]; + size++; + } + LEFT++; + } + + } + return newmat ; } + /* integer partitioning is a combinatorics problem in discreet maths the problem is to generate sum numbers which their summation is the input number @@ -90,9 +143,41 @@ public class MainExercises body to use them instead of arrays. */ + public int[][] intPartitions(int n) { - // todo - return null; + ArrayList all = new ArrayList<>() ; + ArrayListlist = new ArrayList<>() ; + part(n , n ,list , all); + return convertTo2DArray(all) ; + + + } + public static int[][] convertTo2DArray(ArrayList all) { + int [][] mat = new int [all.size()][] ; + for (int i = 0 ; i < all.size() ; i ++ ) + { + mat[i] = all.get(i) ; + } + return mat; + } + public static void part(int rem , int max , ArrayList list , ArrayList all ) + { + if (rem == 0) + { + int[] final1 = new int [list.size()] ; + for (int i = 0 ; i < list.size(); i ++ ) + { + final1[i] = list.get(i) ; + } + all.add(final1) ; + return; + } + for (int i = Math.min(rem , max) ; i >=1 ; i -- ) + { + list.add(i) ; + part(rem-i , i , list, all ); + list.remove(list.size()-1) ; + } } From 574ad85ff0e8189d820bdac9df94899b7b978677 Mon Sep 17 00:00:00 2001 From: Matin Date: Thu, 23 Apr 2026 04:57:11 +0430 Subject: [PATCH 2/2] matin mortazavi / + bonus --- src/main/java/BonusExercises.java | 1 + src/main/java/MainExercises.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index 7fa393b..cbdfb79 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -227,6 +227,7 @@ public class BonusExercises { /* you should return a list of *words* which are palindromic by word we mean at least 3 letters with no whitespace in it + vv note: your implementation should be case-insensitive, e.g. Aba -> is palindrome */ diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 6ed7193..67d1c4c 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -132,7 +132,7 @@ public class MainExercises { 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. + which m eans 1,2 and 2,1 are not different — they count as the same combination. You should generate all partitions of the input number.