Assignment-2 #1
@@ -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 = "^[^\\s.@]+(\\.[^\\s.@]+)*@[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,10 +39,50 @@ public class BonusExercises {
|
||||
If no match for a date is found in the string, return null.
|
||||
*/
|
||||
public String findDate(String string) {
|
||||
// todo
|
||||
for (int i = 0; i <= string.length() - 10; i++) {
|
||||
if (isValidMonth(string, i) && string.charAt(i+2) == '/' && isValidDay(string, i+3) && string.charAt(i+5) == '/' && isValidYear(string, i+6)) {
|
||||
return string.substring(i, i + 10);
|
||||
}
|
||||
if (isValidDay(string, i) && string.charAt(i+2) == '/' && isValidMonth(string, i+3) && string.charAt(i+5) == '/' && isValidYear(string, i+6)) {
|
||||
return string.substring(i, i + 10);
|
||||
}
|
||||
if (isValidYear(string, i) && string.charAt(i+4) == '-' && isValidMonth(string, i+5) && string.charAt(i+7) == '-' && isValidDay(string, i+8)) {
|
||||
return string.substring(i, i + 10);
|
||||
}
|
||||
if (isValidYear(string, i) && string.charAt(i+4) == '/' && isValidMonth(string, i+5) && string.charAt(i+7) == '/' && isValidDay(string, i+8)) {
|
||||
return string.substring(i, i + 10);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private int getInt2(String string, int i) {
|
||||
int n = (string.charAt(i) - '0') * 10 + (string.charAt(i+1) - '0');
|
||||
return n;
|
||||
}
|
||||
|
||||
private int getInt4(String string, int i) {
|
||||
int n = (string.charAt(i) - '0') * 1000 + (string.charAt(i+1) - '0') * 100 + (string.charAt(i+2) - '0') * 10 + (string.charAt(i+3) - '0');
|
||||
return n;
|
||||
}
|
||||
|
||||
private boolean isValidDay(String string, int start) {
|
||||
int d = getInt2(string, start);
|
||||
if (d >= 1 && d <= 31) {return true;}
|
||||
else {return false;}
|
||||
}
|
||||
|
||||
private boolean isValidMonth(String string, int start) {
|
||||
int m = getInt2(string, start);
|
||||
if (m >= 1 && m <= 12) {return true;}
|
||||
else {return false;}
|
||||
}
|
||||
|
||||
private boolean isValidYear(String string, int start) {
|
||||
int y = getInt4(string, start);
|
||||
if (y >= 1 && y <= 9999) {return true;}
|
||||
else {return false;}
|
||||
}
|
||||
/*
|
||||
given a string, implement the method to detect all valid passwords
|
||||
then, it should return the count of them
|
||||
@@ -54,10 +94,39 @@ public class BonusExercises {
|
||||
- has no white-space in it
|
||||
*/
|
||||
public int findValidPasswords(String string) {
|
||||
// todo
|
||||
return -1;
|
||||
int count = 0;
|
||||
String currentWord = "";
|
||||
String input = string + " ";
|
||||
|
||||
for (int i = 0; i < input.length(); i++) {
|
||||
char c = input.charAt(i);
|
||||
if (c == 32 || c == 9 || c == 10 || c == 13) {
|
||||
if (currentWord.length() >= 8) {
|
||||
if (isValid(currentWord)) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
currentWord = "";
|
||||
} else {
|
||||
currentWord += c;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private boolean isValid(String word) {
|
||||
boolean hasUpper = false, hasLower = false, hasNum = false, hasSpecial = false;
|
||||
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
char c = word.charAt(i);
|
||||
if (c >= 65 && c <= 90) {hasUpper = true;}
|
||||
else if (c >= 97 && c <= 122) {hasLower = true;}
|
||||
else if (c >= 48 && c <= 57) {hasNum = true;}
|
||||
else if (c == 33 || c == 64 || c == 35 || c == 36 || c == 37 || c == 94 || c == 38 || c == 42) {hasSpecial = true;}
|
||||
}
|
||||
if (hasUpper && hasLower && hasNum && hasSpecial) {return true;}
|
||||
else {return false;}
|
||||
}
|
||||
/*
|
||||
you should return a list of *words* which are palindromic
|
||||
by word we mean at least 3 letters with no whitespace in it
|
||||
@@ -66,10 +135,37 @@ public class BonusExercises {
|
||||
*/
|
||||
public List<String> findPalindromes(String string) {
|
||||
List<String> list = new ArrayList<>();
|
||||
// todo
|
||||
String currentWord = "";
|
||||
String input = string+ " ";
|
||||
|
||||
for (int i = 0; i < input.length(); i++) {
|
||||
char c = input.charAt(i);
|
||||
if (c == 32 || c == 9 || c == 10 || c == 13 || c == 44) {
|
||||
if (currentWord.length() >= 3 && isPalindrome(currentWord)) {
|
||||
list.add(currentWord);
|
||||
}
|
||||
currentWord = "";
|
||||
} else {
|
||||
currentWord += c;
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private boolean isPalindrome(String string) {
|
||||
int n = string.length();
|
||||
for (int i = 0; i < n / 2; i++) {
|
||||
char left = string.charAt(i);
|
||||
char right = string.charAt(n - 1 - i);
|
||||
if (left != right) {
|
||||
if (Math.abs(left - right) != 32) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
/*
|
||||
@@ -19,10 +22,18 @@ 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) {
|
||||
|
||||
// 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 == j || i == n-1) {
|
||||
triangle[i][j] = '*';
|
||||
} else {
|
||||
triangle[i][j] = ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
return triangle;
|
||||
}
|
||||
|
||||
|
||||
@@ -58,8 +69,48 @@ public class MainExercises
|
||||
- Number of columns: matrix[0].length (if rectangular)
|
||||
*/
|
||||
public int[] spiralTraversal(int[][] matrix) {
|
||||
// todo
|
||||
return null;
|
||||
int m = matrix.length;
|
||||
int n = matrix[0].length;
|
||||
int[] result = new int[m*n];
|
||||
int counter, t = 0, s = 0, k = 0, index = 0;
|
||||
|
||||
if (m > n) {counter = 2 * n;}
|
||||
else if (m == n) {counter = 2 * n - 1;}
|
||||
else {counter = 2 * m - 1;}
|
||||
|
||||
while (t < counter) {
|
||||
if (t < counter) {
|
||||
for (int j = s; j < n - k; j++) {
|
||||
result[index] = matrix[s][j];
|
||||
index++;
|
||||
}
|
||||
t++;
|
||||
}
|
||||
if (t < counter) {
|
||||
for (int i = s + 1; i < m - k; i++) {
|
||||
result[index] = matrix[i][n - 1 - k];
|
||||
index++;
|
||||
}
|
||||
t++;
|
||||
}
|
||||
if (t < counter) {
|
||||
for (int j = n - 2 - k; j >= s; j--) {
|
||||
result[index] = matrix[m - 1 - k][j];
|
||||
index++;
|
||||
}
|
||||
t++;
|
||||
}
|
||||
if (t < counter) {
|
||||
for (int i = m - 2 - k; i > s; i--) {
|
||||
result[index] = matrix[i][s];
|
||||
index++;
|
||||
}
|
||||
t++;
|
||||
}
|
||||
s++;
|
||||
k++;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -91,8 +142,45 @@ public class MainExercises
|
||||
*/
|
||||
|
||||
public int[][] intPartitions(int n) {
|
||||
// todo
|
||||
return null;
|
||||
List<int[]> result = new ArrayList<>();
|
||||
int[] p = new int[n];
|
||||
int k = 1;
|
||||
p[0] = n;
|
||||
result.add(new int[]{n});
|
||||
|
||||
while (true) {
|
||||
int i = k - 1;
|
||||
while (i >= 0 && p[i] == 1) {
|
||||
i--;
|
||||
}
|
||||
if (i < 0) {break;}
|
||||
p[i]--;
|
||||
int remainder = 1;
|
||||
for (int j = i + 1; j < k; j++) {
|
||||
remainder += p[j];
|
||||
}
|
||||
k = i + 1;
|
||||
while (remainder >= p[i]) {
|
||||
p[k] = p[i];
|
||||
remainder -= p[i];
|
||||
k++;
|
||||
}
|
||||
if (remainder > 0) {
|
||||
p[k] = remainder;
|
||||
k++;
|
||||
}
|
||||
int[] part = new int[k];
|
||||
for (int j = 0; j < k; j++) {
|
||||
part[j] = p[j];
|
||||
}
|
||||
result.add(part);
|
||||
}
|
||||
|
||||
int[][] out = new int[result.size()][];
|
||||
for (int i = 0; i < result.size(); i++) {
|
||||
out[i] = result.get(i);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user