From d816db16c14742aad7f79e791c9f983cb45ea23d Mon Sep 17 00:00:00 2001 From: Parmis Jamami Date: Thu, 23 Apr 2026 22:45:06 +0330 Subject: [PATCH] complete code --- src/main/java/BonusExercises.java | 28 ++++++++- src/main/java/MainExercises.java | 96 ++++++++++++++++++++++++++++--- 2 files changed, 113 insertions(+), 11 deletions(-) diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index d02a746..3d5927e 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -20,7 +20,10 @@ public class BonusExercises { - Each segment (between dots) must follow same hyphen rules */ public boolean validateEmail(String email) { - String regex = ""; // todo + if(email == null) return false; + 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]+)*)+$"; + Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(email); @@ -54,8 +57,27 @@ public class BonusExercises { - has no white-space in it */ public int findValidPasswords(String string) { - // todo - return -1; + Pattern wordPattern = Pattern.compile("\\S+"); + Matcher wordMatcher = wordPattern.matcher(string); + + + String passwordRegex = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*])(?=\\S+$).{8,}$"; + Pattern validationPattern = Pattern.compile(passwordRegex); + + int validPasswordCount = 0; + + + while (wordMatcher.find()) { + String potentialPassword = wordMatcher.group(); + Matcher validationMatcher = validationPattern.matcher(potentialPassword); + if (validationMatcher.matches()) { + validPasswordCount++; + } + } + + return validPasswordCount; + + } /* diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..5921b37 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -18,11 +18,21 @@ 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) + { + 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 || j == i || i==n-1) triangle[i][j] = '*'; + else triangle[i][j] = ' '; - // todo - return null; + } + } + return triangle; } @@ -58,8 +68,43 @@ 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]; + boolean[][] visited = new boolean[rows][cols]; + + int [] rowDirection = {0,1,0,-1}; //تغییر سطر در راست و پایین و چپ و بالا + int [] colDirection = {1,0,-1,0}; //تغییر ستون در راست و پایین و چپ و بالا + + int dir = 0 ; // شروع حرکت به سمت راست + int row = 0 ; //شروع از سطر اول + int col = 0; // شروع از ستون اول + + for(int i = 0 ; i < rows * cols ; i++) + { + visited[row][col] = true; + result[i] = matrix[row][col]; + + int nextRow = row + rowDirection[dir]; + int nextCol = col + colDirection[dir]; + + + + if(nextRow < 0 || nextRow >=rows || nextCol < 0 || nextCol >=cols || visited[nextRow][nextCol]) + { + dir = (dir + 1) % 4; //تغییر جهت + nextRow = row + rowDirection[dir]; + nextCol = col + colDirection[dir]; + + } + row = nextRow; + col = nextCol; + + } + + return result; + } /* @@ -89,10 +134,45 @@ public class MainExercises If you're familiar with Lists and ArrayLists, you can also edit the method's body to use them instead of arrays. */ - + private int[][] tempPartitions; + private int partitionCount; public int[][] intPartitions(int n) { - // todo - return null; + int initialCapacity = n + 5; + tempPartitions = new int[initialCapacity][n]; + partitionCount = 0; + + int[] currentPartitionArray = new int[n]; + int[] lengths = new int[initialCapacity]; + + generatePartitionsWithLength(n, n, 0, currentPartitionArray, lengths); + + int[][] finalPartitions = new int[partitionCount][]; + for (int i = 0; i < partitionCount; i++) { + finalPartitions[i] = new int[lengths[i]]; + + for (int j = 0; j < lengths[i]; j++) { + finalPartitions[i][j] = tempPartitions[i][j]; + } + } + + return finalPartitions; + } + + private void generatePartitionsWithLength(int remainingValue, int maxValue, int currentIndex, int[] currentPartitionArray, int[] lengths) { + if (remainingValue == 0) { + lengths[partitionCount] = currentIndex; + + for(int k=0; k < currentIndex; k++){ + tempPartitions[partitionCount][k] = currentPartitionArray[k]; + } + partitionCount++; + return; + } + + for (int i = Math.min(maxValue, remainingValue); i >= 1; i--) { + currentPartitionArray[currentIndex] = i; + generatePartitionsWithLength(remainingValue - i, i, currentIndex + 1, currentPartitionArray, lengths); + } }