Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ht Diana #3

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 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
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,19 @@
package school.lemon.changerequest.java.introduction.hw2;


import com.sun.xml.internal.fastinfoset.util.CharArray;

public class Task6 {
public static int[] reverse(int[] arr) {
return null;
if (arr == null) {
return null;
}
int reverseArr[] = new int[arr.length];
int n = 0;
for (int i = arr.length - 1; i >= 0; --i) {
reverseArr[i] = arr[n];
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;
}
}