diff --git a/src/.idea/.gitignore b/src/.idea/.gitignore new file mode 100644 index 0000000..ab1f416 --- /dev/null +++ b/src/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/src/.idea/misc.xml b/src/.idea/misc.xml new file mode 100644 index 0000000..a20905f --- /dev/null +++ b/src/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/.idea/modules.xml b/src/.idea/modules.xml new file mode 100644 index 0000000..31eaf94 --- /dev/null +++ b/src/.idea/modules.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/.idea/vcs.xml b/src/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/src/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index d02a746..5b0bdb9 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -19,8 +19,11 @@ public class BonusExercises { - Can't have underscores - Each segment (between dots) must follow same hyphen rules */ - public boolean validateEmail(String email) { - String regex = ""; // todo + public boolean validateEmail(String email) + { + if(email == null) return false; + + 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); @@ -38,8 +41,31 @@ public class BonusExercises { If no match for a date is found in the string, return null. */ - public String findDate(String string) { - // todo + public String findDate(String string) + { + if(string.isEmpty()) return null; + + String isoPattern = "\\d{4}-\\d{2}-\\d{2}"; + Pattern isoP = Pattern.compile(isoPattern); + Matcher isoMatcher = isoP.matcher(string); + if(isoMatcher.find()) return isoMatcher.group(); + + String slashVariantPattern = "\\d{4}/\\d{2}/\\d{2}"; + Pattern slashP = Pattern.compile(slashVariantPattern); + Matcher slashMatcher = slashP.matcher(string); + if(slashMatcher.find()) return slashMatcher.group(); + + + String usPattern = "(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])/\\d{4}"; + Pattern usP =Pattern.compile(usPattern); + Matcher usMatcher = usP.matcher(string); + if(usMatcher.find()) return usMatcher.group(); + + String ukPattern = "(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/\\d{4}"; + Pattern ukP =Pattern.compile(ukPattern); + Matcher ukMatcher = ukP.matcher(string); + if(ukMatcher.find()) return ukMatcher.group(); + return null; } @@ -53,9 +79,40 @@ public class BonusExercises { - at least one number and at least a special char "!@#$%^&*" - has no white-space in it */ - public int findValidPasswords(String string) { - // todo - return -1; + public int findValidPasswords(String string) + { + if(string.isEmpty()) return 0; + + String[] words = string.split(" "); + int count = 0; + + for(String word : words) + { + if(word.length() < 8) continue; + + boolean hasUpper = false; + boolean hasLower = false; + boolean hasSpecialChar = false; + boolean hasNumber = false; + + for(int i = 0; i < word.length(); i++) + { + char x = word.charAt(i); + if (x >= 'A' && x <= 'Z') hasUpper = true; + if (x >= 'a' && x <= 'z') hasLower = true; + if (x >= '0' && x <= '9') hasNumber = true; + } + if(word.toString().contains("!") || word.toString().contains("@") + || word.toString().contains("#") || word.toString().contains("$") + || word.toString().contains("%") || word.toString().contains("*") + || word.toString().contains("^") || word.toString().contains("&")) + { + hasSpecialChar = true; + } + + if(hasUpper && hasLower && hasNumber && hasSpecialChar) count++; + } + return count; } /* @@ -64,13 +121,41 @@ public class BonusExercises { note: your implementation should be case-insensitive, e.g. Aba -> is palindrome */ - public List findPalindromes(String string) { + + public boolean isPalindrome(String n) + { + String a = n.toLowerCase(); + boolean isEqual = true; + for(int i = 0; i < (a.length()) / 2; i++) + { + if(a.charAt(i) != a.charAt(n.length() - 1 - i)) + { + isEqual = false; + break; + } + } + if(isEqual) return true; + return false; + } + + public List findPalindromes(String string) + { List list = new ArrayList<>(); - // todo + + if(string.isEmpty()) return list; + + String[] words = string.split(" "); + for(int i = 0; i < words.length; i++) + { + String newWord = words[i].replaceAll("[^a-zA-Z0-9]", ""); + if(newWord.length() < 3) continue; + else if(isPalindrome(newWord) == true) list.add(newWord); + } return list; } - public static void main(String[] args) { - // you can test your code here + public static void main(String[] args) + { + } } diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..422ae33 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -1,3 +1,6 @@ +import java.util.ArrayList; +import java.util.List; + public class MainExercises { /* @@ -18,11 +21,21 @@ 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; - + public char[][] generateTriangle(int n) + { + 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; } @@ -57,9 +70,38 @@ public class MainExercises - Number of rows: matrix.length - Number of columns: matrix[0].length (if rectangular) */ - public int[] spiralTraversal(int[][] matrix) { - // todo - return null; + public int[] spiralTraversal(int[][] matrix) + { + + int m = matrix.length; + int n = matrix[0].length; + int x = 0; + int y = 0; + int idx = 0; + + int[] numbers = new int[m * n]; + boolean[][] visitedCell = new boolean[m][n]; + int[][] dir = {{0,1},{1,0},{0,-1},{-1,0}}; + + for(int i = 0; i < (m * n); i++) + { + numbers[i] = matrix[x][y]; + visitedCell[x][y] = true; + + int nextX = x + dir[idx][0]; + int nextY = y + dir[idx][1]; + + if (nextX < 0 || nextX >= m || nextY < 0 || nextY >= n || visitedCell[nextX][nextY] == true) + { + idx = (idx + 1) % 4; + + nextX = x + dir[idx][0]; + nextY = y + dir[idx][1]; + } + x = nextX; + y = nextY; + } + return numbers; } /* @@ -90,11 +132,38 @@ public class MainExercises body to use them instead of arrays. */ - public int[][] intPartitions(int n) { - // todo - return null; + public int[][] intPartitions(int n) + { + List result = new ArrayList<>(); + findPartitions(n, n, new ArrayList<>(), result); + return result.toArray(new int[result.size()][]); } + private void findPartitions(int r, int max, List list, List finalList) + { + if (r == 0) + { + int[] partition = new int[list.size()]; + for (int i = 0; i < list.size(); i++) + { + partition[i] = list.get(i); + } + finalList.add(partition); + return; + } + + if (r >= max) + { + list.add(max); + findPartitions(r - max, max, list, finalList); + list.remove(list.size() - 1); + } + + if (max > 1) + { + findPartitions(r, max - 1, list, finalList); + } + } public static void main() {