1 Commits
Author SHA1 Message Date
Reza 8dff112467 Complete 2026-04-22 17:07:37 +03:30
2 changed files with 139 additions and 87 deletions
+63 -4
View File
@@ -39,7 +39,21 @@ 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 string) {
// todo String regex1 = "\\d{4}[-/](0[1-9]|1[0-2])[-/](0[1-9]|[12]\\d|3[01])";
String regex2 = "(0[1-9]|[12]\\d|3[01])/(0[1-9]|1[0-2])/\\d{4}";
Pattern pattern1 = Pattern.compile(regex1);
Pattern pattern2 = Pattern.compile(regex2);
Matcher matcher1 = pattern1.matcher(string);
Matcher matcher2 = pattern2.matcher(string);
if (matcher1.find()) {
return matcher1.group();
}
if (matcher2.find()) {
return matcher2.group();
}
return null; return null;
} }
@@ -54,8 +68,42 @@ 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; String[] parts = string.split(" ");
for (String p : parts) {
if (p.length() < 8) {
continue;
}
boolean hasUpper = false;
boolean hasLower = false;
boolean hasNumber = false;
boolean hasSpecial = false;
boolean hasSpace = false;
for (int i = 0; i < p.length(); i++) {
char c = p.charAt(i);
if (Character.isUpperCase(c)) {
hasUpper = true;
}
else if (Character.isLowerCase(c)) {
hasLower = true;
}
else if (Character.isDigit(c)) {
hasNumber = true;
}
else if (Character.isWhitespace(c)) {
hasSpace = true;
}
else {
hasSpecial = true;
}
}
if (!hasSpace && hasUpper && hasLower && hasNumber && hasSpecial) {
count++;
}
}
return count;
} }
/* /*
@@ -66,7 +114,18 @@ public class BonusExercises {
*/ */
public List<String> findPalindromes(String string) { public List<String> findPalindromes(String string) {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
// todo String[] words = string.split(" ");
for (String w : words) {
String clean = w.replaceAll("[^A-Za-z]", "");
if (clean.length() >= 3) {
String lower = clean.toLowerCase() ;
String reversed = new StringBuilder(lower).reverse().toString();
if (lower.equals(reversed)) {
list.add(clean);
}
}
}
return list; return list;
} }
+76 -83
View File
@@ -1,98 +1,91 @@
import java.util.ArrayList;
import java.util.List;
public class MainExercises 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 " "
example 1, input = 3:
*
**
***
example 2, input = 5:
*
**
* *
* *
*****
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) {
if (n == 0) {
return new char[0][0];
}
char[][] triangle = new char[n][];
// todo for (int i = 0; i < n; i++) {
return null; triangle[i] = new char[i + 1];
for (int j = 0; j <= i; j++) {
if (i == 0 || i == n - 1) {
triangle[i][j] = '*';
}
else {
if (j == 0 || j == i) {
triangle[i][j] = '*';
}
else {
triangle[i][j] = ' ';
}
}
}
}
return triangle;
} }
/*
SPIRAL TRAVERSAL OF A RECTANGULAR MATRIX
Given a rectangular matrix (2D array) of integers, this method traverses
it in a spiral order (clockwise from outside to inside) and returns
the elements as a 1D array.
EXAMPLE:
Input matrix:
1 2 3
4 5 6
7 8 9
Spiral order: start at top-left (1), go right →, then down ↓,
then left ←, then up ↑, then repeat inward.
Result: {1, 2, 3, 6, 9, 8, 7, 4, 5}
so you should walk in that matrix in a curl and then add the numbers in order you've seen them in a 1D array
RECTANGULAR MATRIX ASSUMPTION:
This method assumes the input matrix is RECTANGULAR (all rows have
the same number of columns). In Java, we can verify this because
2D arrays might be jagged (rows of different lengths).
IMPORTANT: In Java, we do NOT need to pass rows and cols!
The 2D array 'matrix' knows its own dimensions:
- Number of rows: matrix.length
- Number of columns: matrix[0].length (if rectangular)
*/
public int[] spiralTraversal(int[][] matrix) { public int[] spiralTraversal(int[][] matrix) {
// todo int top = 0;
return null; int bottom = matrix.length - 1;
int left = 0;
int right = matrix[0].length - 1;
int[] result = new int[matrix.length * matrix[0].length];
int index = 0;
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
the problem is to generate sum numbers which their summation is the input number
e.g. 1 -> all partitions of integer 3 are:
3
2, 1
1, 1, 1
e.g. 2 -> for number 4 goes as:
4
3, 1
2, 2
2, 1, 1
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.
You should generate all partitions of the input number.
Hint: You can determine the size and order of the arrays by finding the pattern
of partitions and their count. Trust me, this one's fun and easy :)
If you're familiar with Lists and ArrayLists, you can also edit the method's
body to use them instead of arrays.
*/
public int[][] intPartitions(int n) { public int[][] intPartitions(int n) {
// todo List<List<Integer>> temp = new ArrayList<>();
return null; generate(n, n, new ArrayList<>(), temp);
int[][] output = new int[temp.size()][];
for (int i = 0; i < temp.size(); i++) {
List<Integer> l = temp.get(i);
output[i] = l.stream().mapToInt(Integer::intValue).toArray();
}
return output;
}
private void generate(int n, int max, List<Integer> current, List<List<Integer>> result) {
if (n == 0) {
result.add(new ArrayList<>(current));
return;
}
for (int i = Math.min(n, max); i >= 1; i--) {
current.add(i);
generate(n - i, i, current, result);
current.remove(current.size() - 1);
}
} }