11 Commits
2 changed files with 143 additions and 11 deletions
+45 -5
View File
@@ -20,7 +20,7 @@ public class BonusExercises {
- Each segment (between dots) must follow same hyphen rules
*/
public boolean validateEmail(String email) {
String regex = ""; // todo
String regex = "^[a-zA-z0-9_+-]+(?:\\.[a-zA-Z0-9_+-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z0-9-]+$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(email);
@@ -39,7 +39,15 @@ public class BonusExercises {
If no match for a date is found in the string, return null.
*/
public String findDate(String string) {
// todo
String regex1 = "(0[1-9]|1[0-2])\\/(0[1-9]|[12][0-9]|30)\\/\\d{4}|(0[1-9]|[12][0-9]|30)\\/(0[1-9]|1[0-2])\\/\\d{4}";
String regex2 = "(\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|30))";
String regex3 = "(\\d{4}\\/(0[1-9]|1[0-2])\\/(0[1-9]|[12][0-9]|30))";
Pattern pattern = Pattern.compile(regex1 + "|" + regex2 + "|" + regex3);
Matcher matcher = pattern.matcher(string);
if(matcher.find()){
return matcher.group();
}
return null;
}
@@ -54,8 +62,18 @@ public class BonusExercises {
- has no white-space in it
*/
public int findValidPasswords(String string) {
// todo
return -1;
String regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[!@#$%^&*]).{8,}$";
Pattern pattern = Pattern.compile(regex);
String[] parts = string.split("\\s+");
int counter = 0;
for(String part : parts){
Matcher matcher = pattern.matcher(part);
if(matcher.matches()){
counter++;
}
}
return counter;
}
/*
@@ -66,10 +84,32 @@ public class BonusExercises {
*/
public List<String> findPalindromes(String string) {
List<String> list = new ArrayList<>();
// todo
String[] words = string.split("\\s+|,");
for(String word : words){
if(word.length() >= 3 && isPalindrome(word)){
list.add(word);
}
}
return list;
}
private boolean isPalindrome(String string){
boolean result = true;
int counter = 0;
while(counter + 1 <= string.length() / 2){
char ch1 = string.charAt(counter);
char ch2 = string.charAt(string.length() - 1 - counter);
if(Character.toLowerCase(ch1) != Character.toLowerCase(ch2)){
result = false;
break;
}
counter++;
}
return result;
}
public static void main(String[] args) {
// you can test your code here
}
+98 -6
View File
@@ -1,3 +1,6 @@
import java.util.ArrayList;
import java.util.List;
public class MainExercises
{
/*
@@ -20,8 +23,22 @@ public class MainExercises
*/
public char[][] generateTriangle(int n) {
// todo
return null;
char [][] triangle = new char[n][];
for(int i = 0; i < n; i++){
triangle[i] = new char[i + 1];
for(int j = 0; j <= i; j++){
if(i == 0 || j == 0 || i == n - 1 || j == i){
triangle[i][j] = '*';
}
else{
triangle[i][j] = ' ';
}
}
}
return triangle;
}
@@ -58,8 +75,50 @@ public class MainExercises
- Number of columns: matrix[0].length (if rectangular)
*/
public int[] spiralTraversal(int[][] matrix) {
// todo
return null;
int rows = matrix.length;
int cols = matrix[0].length;
if(rows == 1) return matrix[0];
int[] result = new int[rows * cols];
int top = 0, bottom = rows - 1, left = 0, right = cols - 1;
int id = 0;
while(top <= bottom && left <= right){ // reaching the center
for(int col = left; col <= right; col++){
result[id] = matrix[top][col];
id++;
}
top++;
for(int row = top; row <= bottom; row++){
result[id] = matrix[row][right];
id++;
}
right--;
if(top <= right){
for(int col = right; col >= left; col--){
result[id] = matrix[bottom][col];
id++;
}
bottom--;
}
if(left <= right){
for(int row = bottom; row >= top; row--){
result[id] = matrix[row][left];
id++;
}
left++;
}
}
return result;
}
/*
@@ -91,8 +150,41 @@ public class MainExercises
*/
public int[][] intPartitions(int n) {
// todo
return null;
List<List<Integer>> allPartitions = new ArrayList<>();
ArrayList<Integer> current = new ArrayList<>();
generatePartition(n, n, current, allPartitions);
// converting ArrayList of ArrayLists into a 2d array
int[][] result = new int[allPartitions.size()][];
for (int i = 0; i < allPartitions.size(); i++) {
List<Integer> sublist = allPartitions.get(i);
result[i] = new int[sublist.size()];
for (int j = 0; j < sublist.size(); j++) {
result[i][j] = sublist.get(j);
}
}
return result;
}
private void generatePartition(int remaining, int maxValue, List<Integer> currentPartition, List<List<Integer>> allPartitions){
if(remaining == 0){
allPartitions.add(new ArrayList<>(currentPartition));
return ;
}
for (int i = Math.min(maxValue, remaining); i >= 1; i--){ // preventing choosing the bigger number between maxValue and remaining, this way repeating partitions
currentPartition.add(i);
generatePartition(remaining - i, i, currentPartition, allPartitions);
currentPartition.removeLast(); /*
after the recursive call returns. we need to undo adding i.
this way we can try adding i again. ( remaining may not be 0 yet)
*/
}
}