Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 + "</" + tag + ">");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are almost done.
Now, there is no need to use only one %s in format.
Also, you can combine format with any symbols, so that it should look like: <%s>%s...


}
}
Original file line number Diff line number Diff line change
@@ -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 "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's quite uncommon way to solve this task.
Actually, there is no need for StringBuilder here - you can just use substring method of String.

Also, you can move length check to null-check, so that you'll check string for null and for length<2 and just return s.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it means like this ?6cac7fc

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your implementation pass all tests, but you've used lots of unnecessary operations and it'll be quite slow (due to additional usage of Math and StringBuilder.
Try to solve this task from Math point of view - you just need to calculate char index properly and this could be done using % operator.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i didn't have any idea. what do you mean about use % operator ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

% - is modulus operator in Java.
For example for positive index, result index on which you can get needed char could be calculated in following way:
int index %=s.length().

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, interesting. thx

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]);


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. you don't need if clause, as result of your expression is boolean - you can just return it.
  2. you should cover one more case - last a is equal to last b.

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And again. It do pass all tests, but it's again to complicated.
Why do you need this if clause ? Also there is no need for lengthA and lengthB vars (or you should reuse them everywhere).
By the task description: you'll 100% have arrays with length>=1.

}
Original file line number Diff line number Diff line change
@@ -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];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create more complex for condition:

for (int i = arr.length - 1, n=0; i >= 0 && n<arr.length; i--, n++) {
  reverseArr[i] = arr[n];
}

But, actually, this task could be solved with only one index variable.

return reverseArr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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] ) ";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure, that your regexp is not correct.
Schema could contain only [a-zA-Z0-9].
Host could contain [a-zA-Z0-9-_.]
Port could have more than 1 number

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()};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to avoid such naming.
Characters are free of charge now and you can easily create names with any length.
Also, follow lowerCamelCase for variables names.
Following names would be better:
url -> regexp
purl -> urlPattern
murl -> urlMatcher
surl -> result.
Also, you need to provide String[3] array and with current implementation you'll supply String[1] array, as surl.toString() will create one String object.

}


return new String[]{"", "", ""};

}
}