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

HW_done (without task 10) #9

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
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
Expand Up @@ -2,6 +2,9 @@

public class Task1 {
public static String makeTags(String tag, String text) {
return "";
StringBuilder tagText = new StringBuilder();
tagText.append("<").append(tag).append(">").append(text).append("</").append(tag).append(">");
return tagText.toString();

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
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 regExp = "(((?<=[\\t]\\s)[a-z,A-Z]+.*[^\\s\\t]+)|([A-Z]).*[^\\t\\s])";
Pattern pattern = Pattern.compile(regExp);
Matcher matcher = pattern.matcher(text);
if (matcher.find()){
return matcher.group();
}
return "";

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@

public class Task2 {
public static String firstTwo(String s) {
return "";

if (s == null) {
return null;
}


if (s.length() < 2) {
return s;
}
else {
return s.substring(0, 2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@

public class Task3 {
public static String comboString(String s1, String s2) {
return "";
if (s1 == null){
s1 = String.valueOf(s1);
}

if (s1.length() > s2.length())
{
StringBuilder newstring = new StringBuilder();
newstring.append(s2).append(s1).append(s2);
return newstring.toString();
}
else {
StringBuilder newstring2 = new StringBuilder();
newstring2.append(s1).append(s2).append(s1);
return newstring2.toString();
}
Copy link
Member

Choose a reason for hiding this comment

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

There is no need for StringBuffer in current task.
Also, StringBuffer use synchronized methods, so your code will be additionally slowed by synchronization.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

public class Task4 {
public static String charAt(String s, int i) {
return "";

if (s == null) {
return null;
}
int len = s.length();
if (len < 0) {
if (Math.abs(i) >= len) {
while (Math.abs(i) > len) {
i = Math.abs(i) - len;
}
}
return String.valueOf(s.charAt(len - Math.abs(i)));
}
else {
if (i >= len) {
while (i >= len) i-= len;
}
return String.valueOf(s.charAt(i));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public class Task5 {
public static boolean commondEnd(int[] a, int[] b) {
return false;
return (a[0]==b[0] || a[a.length - 1]== b[b.length - 1] || a[0]==b[b.length - 1] || b[0]== a[a.length - 1]);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
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; i >= 0; i--) {
reverseArr[arr.length - i - 1] = arr[i];
}
return reverseArr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

public class Task7 {
public static int countEvens(int[] arr) {
return 0;
if (arr == null) return 0;

int evenNum = 0;
for (int anArr : arr) {
if (anArr % 2 == 0) {
Copy link
Member

Choose a reason for hiding this comment

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

There is faster way to check whether number is even or odd. Read about parity bit.

evenNum++;
}
}

return evenNum;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
public class Task8 {

public static int[] evenOdd(int[] arr) {
return null;
if (arr == null) return null;

int sortArr[] = new int[arr.length];
int j = 0;
for (int anArr : arr) {
if (anArr % 2 == 0)
sortArr[j++] = arr[anArr];
}

int n = 0;
for (int newArr: arr) {
if (newArr % 2 != 0) {
Copy link
Member

Choose a reason for hiding this comment

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

The only possible values of any number % 2 are 0 and 1, so it'll be clearer if you'll check only those values.

sortArr[j + n++] = arr[newArr];
}
}

return sortArr;
}
}
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) {
return new String[]{"", "", ""};

if (URL == null) {

return new String[]{"", "", ""};
}
Pattern pattern = Pattern.compile("(?:([a-z]+):\\/\\/)?([\\da-z._-]+)(?::(\\d+)?)?.*");
//"(?:(\w+):\/\/)?([\d\w._-]+)(?::(\d+)?)?.*"
Matcher matcher = pattern.matcher(URL);


if (matcher.matches()) {
String scheme = matcher.group(1);
String host = matcher.group(2);
String port = matcher.group(3);
return new String[] {scheme != null ? scheme : "", host != null ? host : "", port != null ? port : ""};

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