2 Commits
Author SHA1 Message Date
MehrdadShirvani e73ac6115f Revert "fix: remove redundant comment"
This reverts commit 959c579550.
2026-04-18 19:38:33 +03:30
MehrdadShirvani 959c579550 fix: remove redundant comment 2026-04-18 19:30:23 +03:30
2 changed files with 15 additions and 212 deletions
+8 -103
View File
@@ -1,6 +1,3 @@
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
@@ -23,51 +20,11 @@ public class BonusExercises {
- Each segment (between dots) must follow same hyphen rules
*/
public boolean validateEmail(String email) {
String regex = ""; // todo
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(email);
if (email.contains(" "))
return false;
int count = 0;
int index = 0;
// rule 1:
for (int i = 0; i < email.length() -1; i++) {
if (email.charAt(i) == '@') {
count++;
index = i;
}
}
if (count != 1)
return false;
// rule 2:
String localPart = email.substring(0,index);
String domainPart = email.substring(index + 1);
if (localPart.isEmpty() || domainPart.isEmpty())
return false;
if (localPart.startsWith(".") || localPart.endsWith(".") ||
domainPart.startsWith(".") || domainPart.endsWith("."))
return false;
if (localPart.contains(".."))
return false;
if (domainPart.contains("_"))
return false;
return true;
return matcher.matches();
}
/*
@@ -82,7 +39,7 @@ public class BonusExercises {
If no match for a date is found in the string, return null.
*/
public String findDate(String string) {
// todo
return null;
}
@@ -97,42 +54,8 @@ public class BonusExercises {
- has no white-space in it
*/
public int findValidPasswords(String string) {
if (string.length() < 8)
return 0;
int validPassWords = 0;
List<String> subStrings = List.of(string.split("\\s+"));
for (String newPart : subStrings) {
//at least 8 characters
if (newPart.length() < 8)
continue;
// at least one uppercase letter
if (!newPart.matches(".*[A-Z].*"))
continue;
// and at least a lowercase
if (!newPart.matches(".*[a-z].*"))
continue;
// at least one number
if (!newPart.matches(".*\\d.*"))
continue;
// at least one spacial character
if (!newPart.matches(".*[!@#$%^&*].*"))
continue;
validPassWords++;
}
return validPassWords;
// todo
return -1;
}
/*
@@ -143,29 +66,11 @@ public class BonusExercises {
*/
public List<String> findPalindromes(String string) {
List<String> list = new ArrayList<>();
Pattern words = Pattern.compile("[a-zA-Z]+");
Matcher matcher = words.matcher(string);
while (matcher.find()) {
String word = matcher.group();
if (word.length() < 2)
continue;
String reversed = new StringBuilder(word).reverse().toString();
if (reversed.equalsIgnoreCase(word))
list.add(word);
}
// todo
return list;
}
public static void main(String[] args) {
// you can test your code here
}
}
+7 -109
View File
@@ -1,6 +1,3 @@
import java.util.ArrayList;
import java.util.List;
public class MainExercises
{
/*
@@ -23,23 +20,8 @@ public class MainExercises
*/
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 i = 0 ; i < n ; i++) {
for (int j = 0 ; j <= i ; j++) {
if (i == 0 || i == n - 1 || j == 0 || j == i)
triangle[i][j] = '*';
else
triangle[i][j] = ' ';
}
}
return triangle;
// todo
return null;
}
@@ -76,51 +58,10 @@ public class MainExercises
- Number of columns: matrix[0].length (if rectangular)
*/
public int[] spiralTraversal(int[][] matrix) {
if ((matrix == null) || (matrix.length == 0) || (matrix[0].length == 0)) {
return new int[0];
}
int rows = matrix.length;
int cols = matrix[0].length;
List<Integer> resultList = new ArrayList<>();
int top = 0, bottom = rows - 1, left = 0, right = cols - 1;
while (top <= bottom && left <= right) {
for (int i = left; i <= right; i++) {
resultList.add(matrix[top][i]);
}
top++;
for (int i = top; i <= bottom; i++) {
resultList.add(matrix[i][right]);
}
right--;
if (top <= bottom) {
for (int i = right; i >= left; i--) {
resultList.add(matrix[bottom][i]);
}
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--) {
resultList.add(matrix[i][left]);
}
left++;
}
}
int[] resultArray = new int[resultList.size()];
for (int i = 0; i < resultList.size(); i++) {
resultArray[i] = resultList.get(i);
}
return resultArray;
// todo
return null;
}
/*
integer partitioning is a combinatorics problem in discreet maths
the problem is to generate sum numbers which their summation is the input number
@@ -149,56 +90,13 @@ public class MainExercises
body to use them instead of arrays.
*/
public int[][] intPartitions(int n) {
if (n <= 0) {
return new int[0][0];
}
List<List<Integer>> allPartitions = new ArrayList<>();
List<Integer> newPartition = new ArrayList<>();
generatePartitions(n, n, newPartition, allPartitions); // using recursion
int[][] result = new int[allPartitions.size()][];
// Convert List<List<Integer>> to int[][]
for (int i = 0; i < allPartitions.size(); i++) {
List<Integer> partition = allPartitions.get(i);
result[i] = new int[partition.size()];
for (int j = 0; j < partition.size(); j++) {
result[i][j] = partition.get(j);
}
}
return result;
}
private void generatePartitions(int n, int maxVal, List<Integer> newPartition, List<List<Integer>> allPartitions) {
// Base case: If n becomes 0, we have found a valid partition
if (n == 0) {
// Add a copy of the current partition to the list of all partitions
allPartitions.add(new ArrayList<>(newPartition));
return;
}
for (int i = Math.min(n, maxVal); i >= 1; i--) {
// Add the current number 'i' to the partition
newPartition.add(i);
// The new maxVal becomes 'i' because the next number cannot be larger than 'i'
generatePartitions(n - i, i, newPartition, allPartitions);
// Remove the last added number 'i' to explore other possibilities
newPartition.removeLast();
}
// todo
return null;
}
static void main()
public static void main()
{
}