diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task1.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task1.java index fb8a529..6a2934d 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task1.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task1.java @@ -1,7 +1,13 @@ package school.lemon.changerequest.java.introduction.hw2; +import java.util.Formatter; + public class Task1 { public static String makeTags(String tag, String text) { - return ""; + + + return String.format("%s", "<" + tag + ">" + + text + ""); + } } diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task10.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task10.java index 95ec7eb..97826bb 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task10.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task10.java @@ -1,7 +1,23 @@ package school.lemon.changerequest.java.introduction.hw2; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class Task10 { public static String trim(String text) { + if (text == null) { + return null; + } + // "" + String stext = "(((?<=[\\s])[\\w]+.*[^\\s]+)|([\\w]).*[^\\s])"; + + + Pattern ptext = Pattern.compile(stext); + Matcher mtext = ptext.matcher(text); + StringBuilder surl = new StringBuilder(); + if (mtext.find()) { + return mtext.group(); + } return ""; } } diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task2.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task2.java index fd4494e..8977d36 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task2.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task2.java @@ -3,6 +3,13 @@ public class Task2 { public static String firstTwo(String s) { - return ""; + + + if (s == null || s.length() < 2) + return s; + + return s.substring(0, 2); + + } } diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task3.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task3.java index 4d2e987..5a3848c 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task3.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task3.java @@ -3,6 +3,23 @@ public class Task3 { public static String comboString(String s1, String s2) { - return ""; + + + if (s1 == null) + + return null + s2 + null; + + else if (s2 == null) + return null + s1 + null; + + + if (s1.length() > s2.length()) + return s2 + s1 + s2; + + + else return s1 + s2 + s1; + + + } } diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task4.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task4.java index 35edd98..2d88d1c 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task4.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task4.java @@ -2,6 +2,26 @@ public class Task4 { public static String charAt(String s, int i) { - return ""; + + + if (i < 0 && Math.abs(i) <= s.length()) { + StringBuilder s1 = new StringBuilder(s); + s1.reverse(); + i = Math.abs(i) - 1; + return String.valueOf(s1.charAt(i)); + } + + + if (i < 0 && Math.abs(i) > s.length()) { + StringBuilder s1 = new StringBuilder(s); + s1.reverse(); + i = s.length() - (Math.abs(i) - 1); + return String.valueOf(s1.charAt(i)); + } + + if (i > 0 && i >= s.length()) { + i = i - s.length(); + } + return String.valueOf(s.charAt(i)); } } diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task5.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task5.java index 5e42b95..11564f8 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task5.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task5.java @@ -2,6 +2,10 @@ public class Task5 { public static boolean commondEnd(int[] a, int[] b) { - return false; + + + return (a[0] == b[0] || a[0] == b[b.length - 1] || a[a.length - 1] == b[0] || a[a.length - 1] == b[b.length - 1]); + + } } diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task6.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task6.java index ee19236..a5ecce8 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task6.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task6.java @@ -1,8 +1,17 @@ package school.lemon.changerequest.java.introduction.hw2; + + public class Task6 { public static int[] reverse(int[] arr) { - return null; + if (arr == null) { + return null; + } + int reverseArr[] = new int[arr.length]; + for (int i = arr.length - 1, n = 0; i >= 0 && n < arr.length; i--, n++) { + reverseArr[i] = arr[n]; + } + return reverseArr; } } diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task7.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task7.java index f95de42..9f304df 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task7.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task7.java @@ -2,6 +2,14 @@ public class Task7 { public static int countEvens(int[] arr) { - return 0; + if (arr == null) { + return 0; + } + int n = 0; + for (int i = arr.length - 1; i >= 0; --i) + if (arr[i] % 2 == 0) { + n++; + } + return n; } } diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task8.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task8.java index 271facb..3fd7c04 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task8.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task8.java @@ -3,6 +3,23 @@ public class Task8 { public static int[] evenOdd(int[] arr) { - return null; + if (arr == null) { + return null; + } + int newArr[] = new int[arr.length]; + int n = 0; + for (int i = 0; i < arr.length; i++) { + { + if (arr[i] % 2 == 0) + newArr[n++] = arr[i]; + } + } + for (int i = 0, m = 0; i < arr.length; i++) { + { + if (arr[i] % 2 != 0) + newArr[n + m++] = arr[i]; + } + } + return newArr; } } diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task9.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task9.java index 267a59d..3c6eeda 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task9.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task9.java @@ -1,7 +1,27 @@ package school.lemon.changerequest.java.introduction.hw2; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class Task9 { public static String[] extractData(String URL) { + if (URL == null) { + return new String[]{"", "", ""}; + } + + + +String url =" ( (?<=\\:\\/\\/) [\\w] +.) ? ((?<= \\/\\/) [\\w\\d.-_] +.) ((?<=\\: ) [\\d] ) "; + Pattern purl = Pattern.compile(url); + Matcher murl = purl.matcher(URL); + StringBuilder surl = new StringBuilder(); + if (murl.find()){ + surl.append(murl.group()).append("\n"); + return new String[]{surl.toString()}; + } + + return new String[]{"", "", ""}; + } }