Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36bddc41ab | ||
|
|
98a1ece080 |
@@ -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]([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);
|
||||
Matcher matcher = pattern.matcher(email);
|
||||
|
||||
@@ -39,7 +39,14 @@ public class BonusExercises {
|
||||
If no match for a date is found in the string, return null.
|
||||
*/
|
||||
public String findDate(String string) {
|
||||
// todo
|
||||
// String regex = "\\b(\\d{4}[/-]\\d{2}[/-]\\d{2}|\\d{2}[/-]\\d{2}[/-]\\d{4})\\b";
|
||||
// Pattern pattern = Pattern.compile(regex);
|
||||
// Matcher matcher = pattern.matcher(string);
|
||||
// if (matcher.find())
|
||||
// {
|
||||
// return matcher.group();
|
||||
// }
|
||||
//Checking day-month compatibility!!!!!!
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -54,8 +61,62 @@ public class BonusExercises {
|
||||
- has no white-space in it
|
||||
*/
|
||||
public int findValidPasswords(String string) {
|
||||
// todo
|
||||
return -1;
|
||||
if (string == null) {return 0;}
|
||||
String[] words = string.split("\\s+");
|
||||
int passwordCount = 0;
|
||||
for (String word : words)
|
||||
{
|
||||
String cleanWord = cleanWord(word);
|
||||
if (isValid(cleanWord))
|
||||
{
|
||||
passwordCount++;
|
||||
}
|
||||
}
|
||||
return passwordCount;
|
||||
}
|
||||
|
||||
private String cleanWord(String word) {
|
||||
return word.replaceAll("[^a-zA-Z0-9!@#$%^&*]", "");
|
||||
}
|
||||
|
||||
private boolean isValid(String word)
|
||||
{
|
||||
if (word.length() < 8)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean hasUppercase = false;
|
||||
boolean hasLowercase = false;
|
||||
boolean hasSpecial = false;
|
||||
boolean hasNumber = false;
|
||||
String special = "!@#$%^&*";
|
||||
|
||||
for (int i = 0; i < word.length(); i++)
|
||||
{
|
||||
char c = word.charAt(i);
|
||||
if (Character.isUpperCase(c))
|
||||
{
|
||||
hasUppercase = true;
|
||||
}
|
||||
else if (Character.isLowerCase(c))
|
||||
{
|
||||
hasLowercase = true;
|
||||
}
|
||||
else if (Character.isDigit(c))
|
||||
{
|
||||
hasNumber = true;
|
||||
}
|
||||
else if (special.indexOf(c) != -1)
|
||||
{
|
||||
hasSpecial = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return hasNumber && hasLowercase && hasSpecial && hasUppercase;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -66,10 +127,35 @@ public class BonusExercises {
|
||||
*/
|
||||
public List<String> findPalindromes(String string) {
|
||||
List<String> list = new ArrayList<>();
|
||||
// todo
|
||||
Pattern pattern = Pattern.compile("[a-zA-Z]+");
|
||||
Matcher matcher = pattern.matcher(string);
|
||||
while (matcher.find())
|
||||
{
|
||||
String word = matcher.group();
|
||||
if (word.length() >= 3) {
|
||||
String lowerWord = word.toLowerCase();
|
||||
if (isPalindrome(lowerWord)) {
|
||||
list.add(word);
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private boolean isPalindrome(String word)
|
||||
{
|
||||
boolean isPalindrome = true;
|
||||
int length = word.length();
|
||||
for (int i = 0; i < length/2; i++)
|
||||
{
|
||||
if (word.charAt(i) != word.charAt(length - i - 1))
|
||||
{
|
||||
isPalindrome = false;
|
||||
}
|
||||
}
|
||||
return isPalindrome;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// you can test your code here
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MainExercises
|
||||
{
|
||||
/*
|
||||
@@ -20,8 +23,23 @@ public class MainExercises
|
||||
*/
|
||||
public char[][] generateTriangle(int n) {
|
||||
|
||||
// todo
|
||||
return null;
|
||||
char[][] triangle = new char[n][n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
triangle[i] = new char[i+1];
|
||||
for (int j = 0; j <= i; j++)
|
||||
{
|
||||
if (i == 0 || i == n-1 || j == i || j == 0)
|
||||
{
|
||||
triangle[i][j] = '*';
|
||||
}
|
||||
else
|
||||
{
|
||||
triangle[i][j] = ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
return triangle;
|
||||
|
||||
}
|
||||
|
||||
@@ -58,8 +76,41 @@ public class MainExercises
|
||||
- Number of columns: matrix[0].length (if rectangular)
|
||||
*/
|
||||
public int[] spiralTraversal(int[][] matrix) {
|
||||
// todo
|
||||
return null;
|
||||
int[] result = new int[matrix.length * matrix[0].length];
|
||||
int total = matrix.length * matrix[0].length;
|
||||
int top = 0;
|
||||
int index = 0;
|
||||
int left = 0;
|
||||
int right = matrix[0].length-1;
|
||||
int bottom = matrix.length-1;
|
||||
while (index < total)
|
||||
{
|
||||
for (int i = left; i <= right && index < total; i++)
|
||||
{
|
||||
result[index] = matrix[top][i];
|
||||
index++;
|
||||
}
|
||||
top++;
|
||||
for (int i = top; i <= bottom && index < total; i++)
|
||||
{
|
||||
result[index] = matrix[i][right];
|
||||
index++;
|
||||
}
|
||||
right--;
|
||||
for (int i = right; i >= left && index < total; i--)
|
||||
{
|
||||
result[index] = matrix[bottom][i];
|
||||
index++;
|
||||
}
|
||||
bottom--;
|
||||
for (int i = bottom; i >= top && index < total; i--)
|
||||
{
|
||||
result[index] = matrix[i][left];
|
||||
index++;
|
||||
}
|
||||
left++;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -90,9 +141,37 @@ public class MainExercises
|
||||
body to use them instead of arrays.
|
||||
*/
|
||||
|
||||
private List<List<Integer>> allPartitions;
|
||||
private List<Integer> currentPartition;
|
||||
public int[][] intPartitions(int n) {
|
||||
// todo
|
||||
return null;
|
||||
allPartitions = new ArrayList<>();
|
||||
currentPartition = new ArrayList<>();
|
||||
generatePartitions(n, n);
|
||||
int [][] result = new int[allPartitions.size()][];
|
||||
for (int i = 0; i < allPartitions.size(); i++)
|
||||
{
|
||||
List<Integer> partition = allPartitions.get(i);
|
||||
result[i] = new int[partition.size()];
|
||||
for (int j = 0; j < partition.size(); j++)
|
||||
{
|
||||
result[i][j] = partition.get(j);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
private void generatePartitions(int remainingSum, int Max)
|
||||
{
|
||||
if (remainingSum == 0)
|
||||
{
|
||||
allPartitions.add(new ArrayList<>(currentPartition));
|
||||
return;
|
||||
}
|
||||
for (int i = Math.min(Max, remainingSum); i >= 1; i--)
|
||||
{
|
||||
currentPartition.add(i);
|
||||
generatePartitions(remainingSum-i, i);
|
||||
currentPartition.remove(currentPartition.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user