Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e73ac6115f | ||
|
|
959c579550 |
@@ -20,7 +20,7 @@ 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 = "^(?!.*\\.\\.)[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])?)*$";
|
String regex = ""; // todo
|
||||||
Pattern pattern = Pattern.compile(regex);
|
Pattern pattern = Pattern.compile(regex);
|
||||||
Matcher matcher = pattern.matcher(email);
|
Matcher matcher = pattern.matcher(email);
|
||||||
|
|
||||||
@@ -39,14 +39,7 @@ 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) {
|
||||||
// String regex = "\\b(\\d{4}[/-]\\d{2}[/-]\\d{2}|\\d{2}[/-]\\d{2}[/-]\\d{4})\\b";
|
// todo
|
||||||
// Pattern pattern = Pattern.compile(regex);
|
|
||||||
// Matcher matcher = pattern.matcher(string);
|
|
||||||
// if (matcher.find())
|
|
||||||
// {
|
|
||||||
// return matcher.group();
|
|
||||||
// }
|
|
||||||
//Checking day-month compatibility!!!!!!
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,62 +54,8 @@ 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) {
|
||||||
if (string == null) {return 0;}
|
// todo
|
||||||
String[] words = string.split("\\s+");
|
return -1;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -127,35 +66,10 @@ 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<>();
|
||||||
Pattern pattern = Pattern.compile("[a-zA-Z]+");
|
// todo
|
||||||
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;
|
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) {
|
public static void main(String[] args) {
|
||||||
// you can test your code here
|
// you can test your code here
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class MainExercises
|
public class MainExercises
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
@@ -23,23 +20,8 @@ public class MainExercises
|
|||||||
*/
|
*/
|
||||||
public char[][] generateTriangle(int n) {
|
public char[][] generateTriangle(int n) {
|
||||||
|
|
||||||
char[][] triangle = new char[n][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 || j == i || j == 0)
|
|
||||||
{
|
|
||||||
triangle[i][j] = '*';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
triangle[i][j] = ' ';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return triangle;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,41 +58,8 @@ 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) {
|
||||||
int[] result = new int[matrix.length * matrix[0].length];
|
// todo
|
||||||
int total = matrix.length * matrix[0].length;
|
return null;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -141,37 +90,9 @@ public class MainExercises
|
|||||||
body to use them instead of arrays.
|
body to use them instead of arrays.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
private List<List<Integer>> allPartitions;
|
|
||||||
private List<Integer> currentPartition;
|
|
||||||
public int[][] intPartitions(int n) {
|
public int[][] intPartitions(int n) {
|
||||||
allPartitions = new ArrayList<>();
|
// todo
|
||||||
currentPartition = new ArrayList<>();
|
return null;
|
||||||
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