workshop_one

This commit is contained in:
2026-04-22 22:46:46 +03:30
parent 5780e034f1
commit 80855777f2
2 changed files with 206 additions and 14 deletions
+114 -6
View File
@@ -1,5 +1,10 @@
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -20,7 +25,13 @@ 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 if (email == null) return false;
String regex =
"^[A-Za-z0-9_+&*-]+(?:\\.[A-Za-z0-9_+&*-]+)*@" +
"(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[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);
@@ -39,7 +50,34 @@ 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 if (string == null) {
return null;
}
Pattern pattern = Pattern.compile("\\b\\d{2,4}[/-]\\d{1,2}[/-]\\d{1,4}\\b");
Matcher matcher = pattern.matcher(string);
Map<String, DateTimeFormatter> formattersMap = new LinkedHashMap<>();
formattersMap.put("yyyy-MM-dd", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
formattersMap.put("yyyy/M/d", DateTimeFormatter.ofPattern("yyyy/M/d"));
formattersMap.put("M/d/yyyy", DateTimeFormatter.ofPattern("M/d/yyyy"));
formattersMap.put("d/M/yyyy", DateTimeFormatter.ofPattern("d/M/yyyy"));
while (matcher.find()) {
String candidate = matcher.group();
for (Map.Entry<String, DateTimeFormatter> entry : formattersMap.entrySet()) {
String patternStr = entry.getKey();
DateTimeFormatter formatter = entry.getValue();
try {
LocalDate.parse(candidate, formatter);
return candidate;
} catch (DateTimeParseException ignored) {
}
}
}
return null; return null;
} }
@@ -54,8 +92,52 @@ 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 if(string == null || string.isEmpty()){
return -1; return 0;
}
int validPasswordCount = 0;
Pattern pattern = Pattern.compile("[A-Za-z0-9!@#$%^&*]+");
Matcher matcher = pattern.matcher(string);
while (matcher.find()){
String candidate = matcher.group();
if(isValidPassword(candidate)){
validPasswordCount ++;
}
}
return validPasswordCount;
}
public boolean isValidPassword(String password){
if(password.length()<8){
return false;
}
boolean hasUpperCase = false;
boolean hasLowerCase = false;
boolean hasDigit = false;
boolean hasSpecialChar = false;
String sepecialChras = "!@#$%^&*";
for( char c : password.toCharArray()){
if(Character.isUpperCase(c)){
hasUpperCase = true;
}
else if(Character.isLowerCase(c)){
hasLowerCase = true;
}
else if(Character.isDigit(c)){
hasDigit = true;
}
else if(sepecialChras.indexOf(c) != -1){
hasSpecialChar = true;
}
else if (Character.isSpaceChar(c)){
return false;
}
}
return hasUpperCase && hasLowerCase && hasDigit && hasSpecialChar;
} }
/* /*
@@ -66,11 +148,37 @@ 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
if (string == null || string.isEmpty()) {
return list;
}
String[] words = string.split("\\s+");
for(String word : words){
word = word.replaceAll("^[^a-zA-Z0-9]+|[^a-zA-Z0-9]+$" , "");
if(word.length() >= 3 && isPalindrome(word)){
list.add(word);
}
}
return list; return list;
} }
private boolean isPalindrome(String word){
String lower = word.toLowerCase();
int left = 0 ,right = lower.length()-1;
while(left < right) {
if (lower.charAt(left) != lower.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
public static void main(String[] args) { public static void main(String[] args) {
// you can test your code here
} }
} }
+92 -8
View File
@@ -1,3 +1,6 @@
import java.util.ArrayList;
import java.util.List;
public class MainExercises public class MainExercises
{ {
/* /*
@@ -19,10 +22,16 @@ 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][];
// 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 || j == 0 || i == j || i == n-1)
triangle[i][j] = '*';
else triangle[i][j] = ' ';
}
}
return triangle;
} }
@@ -58,8 +67,51 @@ 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
return null; int rows = matrix.length;
int cols = matrix[0].length;
int total_element = rows * cols;
int [] result = new int[total_element];
int index = 0;
int top = 0;
int bottom = rows - 1;
int left = 0;
int right = cols - 1;
//Direction codes : 0 = right, 1 = down , 2 = left , 3 = up
int direction = 0;
while (top <= bottom && left <= right) {
switch (direction){
case 0 :
for (int i = left; i <= right ; i++) {
result[index++] = (matrix[top][i]);
}
top++;
break;
case 1 :
for (int i = top; i <=bottom ; i++) {
result[index++] = (matrix[i][right]);
}
right--;
break;
case 2 :
for (int i = right; i >=left ; i--) {
result[index++] = (matrix[bottom][i]);
}
bottom--;
break;
case 3 :
for (int i = bottom; i >=top ; i--) {
result[index++] = (matrix[i][left]);
}
left++;
break;
}
direction = (direction + 1) % 4;
}
return result;
} }
/* /*
@@ -91,10 +143,42 @@ public class MainExercises
*/ */
public int[][] intPartitions(int n) { public int[][] intPartitions(int n) {
// todo List<List<Integer>> allpartitions = new ArrayList<>();
return null; List<Integer> currentpartition = new ArrayList<>();
findpartitionrecursive(n, n, currentpartition, allpartitions);
return convertListToArray(allpartitions);
} }
private void findpartitionrecursive(int target, int max, List<Integer> currentpartition, List<List<Integer>> allpartitions) {
if (target == 0) {
allpartitions.add(new ArrayList<>(currentpartition));
return;
}
for (int i = Math.min(target, max); i >= 1; i--) {
currentpartition.add(i);
findpartitionrecursive(target - i, i, currentpartition, allpartitions);
currentpartition.remove(currentpartition.size() - 1);
}
}
private int[][] convertListToArray(List<List<Integer>> mylist) {
int[][] resultArray = new int[mylist.size()][];
for (int i = 0; i < mylist.size(); i++) {
List<Integer> innerList = mylist.get(i);
resultArray[i] = new int[innerList.size()];
for (int j = 0; j < innerList.size(); j++) {
resultArray[i][j] = innerList.get(j);
}
}
return resultArray;
}
public static void main() public static void main()
{ {