2 Commits
Author SHA1 Message Date
MehrdadShirvani ac9a3ceb76 Merge PR 'Complete main and bonus exercises' (#1) from develop into main
Full Mark  + Bonus - Late Submission
2026-07-10 19:17:47 +00:00
faraz_ardeh 86e177d368 Complete main and bonus exercises 2026-06-16 14:01:16 +03:30
2 changed files with 98 additions and 13 deletions
+39 -5
View File
@@ -20,7 +20,10 @@ public class BonusExercises {
- Each segment (between dots) must follow same hyphen rules
*/
public boolean validateEmail(String email) {
String regex = ""; // todo
String local = "[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)*";
String domainSegment = "[a-zA-Z0-9]([a-zA-Z0-9\\-]*[a-zA-Z0-9])?";
String domain = domainSegment + "(\\." + domainSegment + ")*";
String regex = "^" + local + "@" + domain + "$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(email);
@@ -39,7 +42,20 @@ public class BonusExercises {
If no match for a date is found in the string, return null.
*/
public String findDate(String string) {
// todo
String dayOrMonth = "(0[1-9]|[12][0-9]|3[01])";
String month = "(0[1-9]|1[0-2])";
String year = "[0-9]{4}";
String[] patterns = {
dayOrMonth + "/" + month + "/" + year,
year + "-" + month + "-" + dayOrMonth,
year + "/" + month + "/" + dayOrMonth
};
for (String pat : patterns) {
Matcher m = Pattern.compile(pat).matcher(string);
if (m.find()) return m.group();
}
return null;
}
@@ -54,8 +70,11 @@ public class BonusExercises {
- has no white-space in it
*/
public int findValidPasswords(String string) {
// todo
return -1;
String regex = "(?=\\S{8,})(?=\\S*[A-Z])(?=\\S*[a-z])(?=\\S*\\d)(?=\\S*[!@#$%^&*])\\S+";
Matcher m = Pattern.compile(regex).matcher(string);
int count = 0;
while (m.find()) count++;
return count;
}
/*
@@ -66,10 +85,25 @@ public class BonusExercises {
*/
public List<String> findPalindromes(String string) {
List<String> list = new ArrayList<>();
// todo
Matcher m = Pattern.compile("[a-zA-Z]+").matcher(string);
while (m.find()) {
String word = m.group();
if (word.length() >= 3 && isPalindrome(word)) {
list.add(word);
}
}
return list;
}
private boolean isPalindrome(String s) {
String lower = s.toLowerCase();
int left = 0, right = lower.length() - 1;
while (left < right) {
if (lower.charAt(left++) != lower.charAt(right--)) return false;
}
return true;
}
public static void main(String[] args) {
// you can test your code here
}
+59 -8
View File
@@ -1,3 +1,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainExercises
{
/*
@@ -19,10 +23,20 @@ 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) {
if (n == 0) return new char[0][];
// todo
return null;
char[][] triangle = new char[n][];
for (int i = 0; i < n; i++) {
triangle[i] = new char[i + 1];
if (i <= 1 || i == n - 1) {
Arrays.fill(triangle[i], '*');
} else {
triangle[i][0] = '*';
Arrays.fill(triangle[i], 1, i, ' ');
triangle[i][i] = '*';
}
}
return triangle;
}
@@ -58,8 +72,33 @@ public class MainExercises
- Number of columns: matrix[0].length (if rectangular)
*/
public int[] spiralTraversal(int[][] matrix) {
// todo
return null;
if (matrix == null || matrix.length == 0) return new int[0];
int rows = matrix.length;
int cols = matrix[0].length;
int[] result = new int[rows * cols];
int idx = 0;
int top = 0, bottom = rows - 1, left = 0, right = cols - 1;
while (top <= bottom && left <= right) {
for (int j = left; j <= right; j++) result[idx++] = matrix[top][j];
top++;
for (int i = top; i <= bottom; i++) result[idx++] = matrix[i][right];
right--;
if (top <= bottom) {
for (int j = right; j >= left; j--) result[idx++] = matrix[bottom][j];
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--) result[idx++] = matrix[i][left];
left++;
}
}
return result;
}
/*
@@ -91,8 +130,20 @@ public class MainExercises
*/
public int[][] intPartitions(int n) {
// todo
return null;
List<int[]> result = new ArrayList<>();
generatePartitions(n, n, new int[n], 0, result);
return result.toArray(new int[0][]);
}
private void generatePartitions(int remaining, int maxPart, int[] current, int depth, List<int[]> result) {
if (remaining == 0) {
result.add(Arrays.copyOf(current, depth));
return;
}
for (int part = Math.min(remaining, maxPart); part >= 1; part--) {
current[depth] = part;
generatePartitions(remaining - part, part, current, depth + 1, result);
}
}
@@ -100,4 +151,4 @@ public class MainExercises
{
}
}
}