Compare commits
2
Commits
2f122c9d2b
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e73ac6115f | ||
|
|
959c579550 |
@@ -1,10 +1,5 @@
|
|||||||
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;
|
||||||
|
|
||||||
@@ -25,13 +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) {
|
||||||
if (email == null) return false;
|
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])?$";
|
|
||||||
|
|
||||||
Pattern pattern = Pattern.compile(regex);
|
Pattern pattern = Pattern.compile(regex);
|
||||||
Matcher matcher = pattern.matcher(email);
|
Matcher matcher = pattern.matcher(email);
|
||||||
|
|
||||||
@@ -50,34 +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) {
|
||||||
if (string == null) {
|
// todo
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,52 +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 || string.isEmpty()){
|
// todo
|
||||||
return 0;
|
return -1;
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -148,37 +66,11 @@ 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class MainExercises
|
public class MainExercises
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
@@ -22,16 +19,10 @@ 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][];
|
|
||||||
for (int i = 0; i < n; i++) {
|
// todo
|
||||||
triangle[i] = new char[i+1];
|
return null;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -67,51 +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) {
|
||||||
|
// todo
|
||||||
int rows = matrix.length;
|
return null;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -143,42 +91,10 @@ public class MainExercises
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public int[][] intPartitions(int n) {
|
public int[][] intPartitions(int n) {
|
||||||
List<List<Integer>> allpartitions = new ArrayList<>();
|
// todo
|
||||||
List<Integer> currentpartition = new ArrayList<>();
|
return null;
|
||||||
|
|
||||||
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()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user