diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index d02a746..fcdd56d 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -20,11 +20,39 @@ public class BonusExercises { - Each segment (between dots) must follow same hyphen rules */ public boolean validateEmail(String email) { - String regex = ""; // todo + String regex = "^[^@\\s]+@[^@\\s]+$"; // todo Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(email); + if(matcher.find()) + { + String[] s =email.split("@"); + String local_part = s[0]; + String domain = s[1]; + regex = "(^[.].*$) | (^.*[.]$) | (^(?=.*\\.{2}).*$)"; + pattern = Pattern.compile(regex); + matcher = pattern.matcher(local_part); + if(matcher.find()) + return false; + regex = "(^-.*$)|(^.*-$)|(_)"; + pattern = Pattern.compile(regex); + matcher = pattern.matcher(domain); + if(matcher.find()) + return false; + regex = "[.][^.]+[.]"; + pattern = Pattern.compile(regex); + matcher = pattern.matcher(domain); - return matcher.matches(); + while(matcher.find()) { + regex = "(^-.*$)|(^.*-$)|(_)"; + pattern = Pattern.compile(regex); + Matcher matcher1 = pattern.matcher(matcher.group()); + if(matcher1.find()) + return false; + } + return true; + + } + return false; } /* @@ -39,8 +67,51 @@ public class BonusExercises { If no match for a date is found in the string, return null. */ public String findDate(String string) { - // todo - return null; + + Pattern pattern = Pattern.compile( "(?(?<=\\D)(1[0-2]|0?[1-9])/(3[0-1]|[1-2][0-9]|0?[1-9])/[0-9]{4})" + + "|(?(?<=\\D)(3[0-1]|[1-2][0-9]|0?[1-9])/(1[0-2]|0?[1-9])/[0-9]{4})" + + "|(?(?<=\\D)[0-9]{4}-(1[0-2]|0?[1-9])-(3[0-1]|[1-2][0-9]|0?[1-9]))" + + "|(?(?<=\\D)[0-9]{4}/(1[0-2]|0?[1-9])/(3[0-1]|[1-2][0-9]|0?[1-9]))"); + Matcher matcher = pattern.matcher(string); + if(!matcher.find()) + return null; + String date = matcher.group(); + String year =""; + String month =""; + String day =""; + if(matcher.group("american")!=null){ + String[] s = date.split("/"); + year = s[2]; + month = s[0]; + day = s[1]; + } + else if(matcher.group("british")!=null){ + String[] s = date.split("/"); + year = s[2]; + month = s[1]; + day = s[0]; + } + else if(matcher.group("iso")!=null){ + String[] s = date.split("-"); + year = s[0]; + month = s[1]; + day = s[2]; + } + else if(matcher.group("slash")!=null){ + String[] s = date.split("/"); + year = s[0]; + month = s[1]; + day = s[2]; + } + // is date valid? + int Year =Integer.parseInt(year); + + if( (month.matches("0?4|0?6|0?9|11")&&day.matches("31")) || + ((Year%400==0||(Year%100!=0&&Year%4==0))&&month.matches("0?2")&&day.matches("3[0-1]")) || + (month.matches("0?2")&&day.matches("3[0-1]|29")) ) + return null; + return date; + } /* @@ -54,10 +125,19 @@ public class BonusExercises { - has no white-space in it */ public int findValidPasswords(String string) { - // todo - return -1; + int count = 0; + Pattern pattern = Pattern.compile("(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[!@#$%^&*]).{8,}"); + + for (String s : string.split("\\s+")) { + Matcher matcher = pattern.matcher(s); + if (matcher.matches()) { + count++; + } + } + return count; } + /* you should return a list of *words* which are palindromic by word we mean at least 3 letters with no whitespace in it @@ -66,7 +146,25 @@ public class BonusExercises { */ public List findPalindromes(String string) { List list = new ArrayList<>(); - // todo + String[] str = string.split(" "); + Pattern pattern = Pattern.compile("[a-zA-Z]{3,}", Pattern.CASE_INSENSITIVE); + Matcher matcher ; + loop:for (String s:str){ + + matcher = pattern.matcher(s); + if(matcher.find()){ + String temp = matcher.group(); + for (int i = 0; i <= temp.length()/2-1; i++) { + int dumy = temp.charAt(i)-temp.charAt(temp.length()-i-1); + if(dumy!=0&&dumy!='A'-'a'&&dumy!='a'-'A') + continue loop; + + + } + list.add(matcher.group()); + } + + } return list; } diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..6d330df 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -1,3 +1,5 @@ +import java.util.ArrayList; +import java.util.Arrays; public class MainExercises { /* @@ -19,9 +21,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=stop[2]&&start[3]>=stop[3] + */ + while (pointer < matrix.length*matrix[0].length){ + switch (Case){ + case 0: + for ( column=start[Case]; column<=stop[Case]; column++){ + S_T[pointer++]=matrix[row][column]; + } + column--; + start[Case]++; + stop[Case]--; + Case ++; + break; + case 1: + for ( row=start[Case]; row<=stop[Case]; row++){ + S_T[pointer++]=matrix[row][column]; + } + row--; + start[Case]++; + stop[Case]--; + Case ++; + break; + case 2: + for ( column=start[Case]; column>=stop[Case]; column--){ + S_T[pointer++]=matrix[row][column]; + } + column++; + start[Case]--; + stop[Case]++; + Case ++; + break; + case 3: + for ( row=start[Case]; row>=stop[Case]; row--){ + S_T[pointer++]=matrix[row][column]; + } + row++; + start[Case]--; + stop[Case]++; + Case=0; + break; + + } + + } + return S_T; } /* @@ -92,7 +159,32 @@ public class MainExercises public int[][] intPartitions(int n) { // todo - return null; + if(n==1) + return new int[][]{new int[]{1}}; + ArrayList list = new ArrayList(); + list.add(new int[]{n}); + for (int i = n-1; i >=1 ; i--) { + int[][]temp = intPartitions(n-i); + int rows = temp.length; + for (int j = 0; j < rows; j++) { + ArrayList merged =new ArrayList<>(); + merged.add(i); + if(temp[j][0]<=i) { + for (int k : temp[j]) { + merged.add( k); + } + int[] d = new int[merged.size()]; + for(int z=0;z< merged.size();z++) { + d[z] = merged.get(z); + } + list.add(d); + + } + } + + } + + return (int[][]) list.toArray(new int[list.size()][]); } diff --git a/src/test/.idea/misc.xml b/src/test/.idea/misc.xml new file mode 100644 index 0000000..4acafcc --- /dev/null +++ b/src/test/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/test/.idea/modules.xml b/src/test/.idea/modules.xml new file mode 100644 index 0000000..40ab362 --- /dev/null +++ b/src/test/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/test/.idea/vcs.xml b/src/test/.idea/vcs.xml new file mode 100644 index 0000000..c8ade07 --- /dev/null +++ b/src/test/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/test/.idea/workspace.xml b/src/test/.idea/workspace.xml new file mode 100644 index 0000000..71f0e54 --- /dev/null +++ b/src/test/.idea/workspace.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + 1783490746045 + + + + \ No newline at end of file