Merge pull request 'develop' (#1) from develop into main

Reviewed-on: #1
Reviewed-by: Hosein Asfiaee
This commit was merged in pull request #1.
This commit is contained in:
2026-07-30 13:09:23 +00:00
3 changed files with 188 additions and 16 deletions
Generated
+1
View File
@@ -0,0 +1 @@
MainExercises.java
+110 -6
View File
@@ -20,7 +20,7 @@ public class BonusExercises {
- Each segment (between dots) must follow same hyphen rules - Each segment (between dots) must follow same hyphen rules
*/ */
public boolean validateEmail(String email) { 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-]+$";
Pattern pattern = Pattern.compile(regex); Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(email); Matcher matcher = pattern.matcher(email);
@@ -38,11 +38,31 @@ public class BonusExercises {
If no match for a date is found in the string, return null. If no match for a date is found in the string, return null.
*/ */
public String findDate(String string) { public String findDate(String text) {
// todo if (text == null || text.isBlank()) {
return null;
}
String dateRegex =
"\\b(?:"
+ "\\d{4}[-/](?:0[1-9]|1[0-2])[-/](?:0[1-9]|[12]\\d|3[01])"
+ "|"
+ "(?:0[1-9]|[12]\\d|3[01])/(?:0[1-9]|1[0-2])/\\d{4}"
+ "|"
+ "(?:0[1-9]|1[0-2])/(?:0[1-9]|[12]\\d|3[01])/\\d{4}"
+ ")\\b";
Matcher matcher = Pattern.compile(dateRegex).matcher(text);
if (matcher.find()) {
return matcher.group();
}
return null; return null;
} }
/* /*
given a string, implement the method to detect all valid passwords given a string, implement the method to detect all valid passwords
then, it should return the count of them then, it should return the count of them
@@ -54,8 +74,60 @@ public class BonusExercises {
- has no white-space in it - has no white-space in it
*/ */
public int findValidPasswords(String string) { public int findValidPasswords(String string) {
// todo int count = 0;
return -1;
for (int i = 0; i <= string.length() - 8;) {
int bestJ = -1;
for (int j = i + 8; j <= string.length(); j++) {
String tempPassword = string.substring(i,j);
if (tempPassword.contains(" ")){
continue;
}
boolean hasSpecialCharacter = false;
boolean hasDigit = false;
boolean hasLowercase = false;
boolean hasUppercase = false;
String specialChars = "!@#$%^&*";
String digits = "1234567890";
String lowerCases = "abcdefghijklmnopqrstuvwxyz";
String upperCases = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int k = 0; k < tempPassword.length(); k++) {
char currentChar = tempPassword.charAt(k);
if (specialChars.contains(String.valueOf(currentChar))){
hasSpecialCharacter = true;
} else if (upperCases.contains(String.valueOf(currentChar))) {
hasUppercase = true;
} else if (lowerCases.contains(String.valueOf(currentChar))) {
hasLowercase = true;
} else if (digits.contains(String.valueOf(currentChar))) {
hasDigit = true;
}
}
if (hasDigit && hasLowercase && hasUppercase && hasSpecialCharacter){
count++;
i = j;
bestJ = j;
break;
}
}
if (bestJ == -1){
i++;
}
}
return count;
} }
/* /*
@@ -64,9 +136,41 @@ public class BonusExercises {
note: your implementation should be case-insensitive, e.g. Aba -> is palindrome note: your implementation should be case-insensitive, e.g. Aba -> is palindrome
*/ */
public boolean isPalindrome(String word) {
if (word.length() == 1){
return false;
}
String lowerWord = word.toLowerCase();
StringBuilder mutableInverse = new StringBuilder(lowerWord.length());
for (int i = lowerWord.length() - 1; i >= 0; i--){
mutableInverse.append(lowerWord.charAt(i));
}
String inverseOfWord = mutableInverse.toString();
return lowerWord.equals(inverseOfWord);
}
public List<String> findPalindromes(String string) { public List<String> findPalindromes(String string) {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
// todo String[] wordsArray = string.split("\\s+");
for (String s : wordsArray){
String finalS = s;
if (finalS.endsWith(",")) {
finalS = finalS.substring(0, finalS.length() - 1);
}
if (finalS.isEmpty()) {
continue;
}
if (isPalindrome(finalS)){
list.add(finalS);
}
}
return list; return list;
} }
+77 -10
View File
@@ -1,3 +1,5 @@
import java.util.ArrayList;
public class MainExercises public class MainExercises
{ {
/* /*
@@ -19,10 +21,18 @@ public class MainExercises
the output has to be a two-dimensional array of characters, so don't just print the triangle! 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(j == 0 || i == n - 1 || i == j)
triangle[i][j] = '*';
else
triangle[i][j] = ' ';
}
}
// todo return triangle;
return null;
} }
@@ -58,10 +68,38 @@ public class MainExercises
- Number of columns: matrix[0].length (if rectangular) - Number of columns: matrix[0].length (if rectangular)
*/ */
public int[] spiralTraversal(int[][] matrix) { public int[] spiralTraversal(int[][] matrix) {
// todo int rows = matrix.length;
return null; int cols = matrix[0].length;
int[] result = new int[rows * cols];
int index = 0;
int top = 0, bottom = rows - 1;
int left = 0, right = cols - 1;
while (top <= bottom && left <= right) {
for (int j = left; j <= right; j++) {
result[index++] = matrix[top][j];
}
top++;
for (int i = top; i <= bottom; i++) {
result[index++] = matrix[i][right];
}
right--;
if (top <= bottom) {
for (int j = right; j >= left; j--) {
result[index++] = matrix[bottom][j];
}
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--) {
result[index++] = matrix[i][left];
}
left++;
}
}
return result;
} }
/* /*
integer partitioning is a combinatorics problem in discreet maths integer partitioning is a combinatorics problem in discreet maths
the problem is to generate sum numbers which their summation is the input number the problem is to generate sum numbers which their summation is the input number
@@ -89,15 +127,44 @@ public class MainExercises
If you're familiar with Lists and ArrayLists, you can also edit the method's If you're familiar with Lists and ArrayLists, you can also edit the method's
body to use them instead of arrays. body to use them instead of arrays.
*/ */
private ArrayList<ArrayList<Integer>> intPartitionsHelper(int n, int biggest) {
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
if (n == 0) {
result.add(new ArrayList<>());
return result;
}
for (int k = 1; k <= Math.min(biggest, n); k++) {
ArrayList<ArrayList<Integer>> subPartitions = intPartitionsHelper(n - k, k);
for (ArrayList<Integer> subPartition : subPartitions) {
ArrayList<Integer> newPartition = new ArrayList<>();
newPartition.add(k);
newPartition.addAll(subPartition);
result.add(newPartition);
}
}
return result;
}
public int[][] intPartitions(int n) { public int[][] intPartitions(int n) {
// todo ArrayList<ArrayList<Integer>> answerList = intPartitionsHelper(n, n);
return null; int[][] answerArray = new int[answerList.size()][];
for(int i = 0; i < answerList.size(); i++){
answerArray[(answerList.size() - i) - 1] = new int[answerList.get(i).size()];
for(int j = 0; j < answerList.get(i).size(); j++){
answerArray[(answerList.size() - i) - 1][j] = answerList.get(i).get(j);
}
}
return answerArray;
} }
public static void main() public void main()
{ {
int n = 4;
int[][] partitions = intPartitions(n);
for (int i = 0; i < partitions.length; i++) {
for(int j = 0; j < partitions[i].length; j++)
System.out.print(partitions[i][j]);
System.out.println();
}
} }
} }