implement exercises #1

Merged
peyman merged 2 commits from develope into main 2026-04-25 21:45:25 +00:00
2 changed files with 153 additions and 8 deletions
+70 -2
View File
@@ -38,8 +38,46 @@ public class BonusExercises {
If no match for a date is found in the string, return null.
*/
//Helper method
private boolean isValidDateForm(String date){
//ISO Format
if (date.matches("\\d{4}-\\d{2}-\\d{2}")){
return true;
}
//Slash Variant Format
if (date.matches("\\d{4}/\\d{2}/\\d{2}")){
return true;
}
//American/British Format
if (date.matches("\\d{2}/\\d{2}/\\d{4}")){
return true;
}
return false;
}
public String findDate(String string) {
// todo
if (string == null || string.isEmpty()){
return null;
}
Pattern patt = Pattern.compile(
"\\b(\\d{4}-\\d{2}-\\d{2}" +
"|" +
"\\d{4}/\\d{2}/\\d{2}" +
"|" +
"\\d{2}/\\d{2}/\\d{4}" +
")\\b"
);
Matcher match = patt.matcher(string);
while (match.find()){
String date = match.group();
if (isValidDateForm(date)){
return date;
}
}
return null;
}
@@ -64,10 +102,40 @@ public class BonusExercises {
note: your implementation should be case-insensitive, e.g. Aba -> is palindrome
*/
//Helper method
private boolean isPalin(String s){
int left = 0;
int right = s.length() - 1;
while (left < right){
if(s.charAt(left) != s.charAt(right)){
return false;
}
left++;
right--;
}
return true;
}
public List<String> findPalindromes(String string) {
List<String> list = new ArrayList<>();
// todo
if (string == null || string.isEmpty()){
return list;
}
String[] words = string.split("\\s+");
for (String w : words){
if (w.length() < 3){
continue;
}
String lowercase = w.toLowerCase();
if (isPalin(lowercase)){
list.add(w);
}
}
return list;
}
public static void main(String[] args) {
+83 -6
View File
@@ -1,3 +1,6 @@
import java.util.ArrayList;
import java.util.List;
public class MainExercises
{
/*
@@ -19,10 +22,24 @@ 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 x = 0; x < n; x++){
triangle[x] = new char[x + 1];
for (int y = 0; y <= x; y++){
if (y == 0 || x == y || x == n-1){
triangle[x][y] = '*';
}
else {
triangle[x][y] = ' ';
}
}
}
return triangle;
}
@@ -58,8 +75,43 @@ 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 || matrix[0].length == 0) {
return new int[0];
}
int[] path = new int[matrix.length * matrix[0].length];
int up = 0 ;
int down = matrix.length - 1;
int left = 0;
int right = matrix[0].length - 1;
int step = 0;
while ( up <= down && left <= right){
for (int i = left; i <= right; i++){
path[step++] = matrix[up][i];
}
up++;
for (int j = up; j <= down; j++){
path[step++] = matrix[j][right];
}
right--;
if (up <= down){
for (int i = right; i >=left; i--){
path[step++] = matrix[down][i];
}
down--;
}
if (right >= left){
for (int j = down; j >= up; j--){
path[step++] = matrix[j][left];
}
left++;
}
}
return path;
}
/*
@@ -89,10 +141,35 @@ 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.
*/
//Helper method
private void makePartitions(int remain, int max, List<Integer> current, List<int[]> result){
if (remain == 0){
int[] arr = new int[current.size()];
for (int i = 0; i < current.size(); i++){
arr[i] = current.get(i);
}
result.add(arr);
return;
}
for(int i = Math.min(max, remain); i >= 1; i--){
current.add(i);
makePartitions(remain - i, i, current, result);
current.remove(current.size() - 1);
}
}
public int[][] intPartitions(int n) {
// todo
return null;
List<int[]> result = new ArrayList<>();
List<Integer> current = new ArrayList<>();
makePartitions(n, n, current, result);
int[][] output = new int[result.size()][];
for (int i = 0; i < result.size(); i++){
output[i] = result.get(i);
}
return output;
}