-
Notifications
You must be signed in to change notification settings - Fork 10
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
base: master
Are you sure you want to change the base?
Changes from all commits
034895e
9560721
9800313
53a0621
d2d1f51
42a708b
a451660
19101d1
c09e687
205bcbe
87c44a2
4b8d232
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -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 |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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[] {"", "", ""}; | ||
} | ||
} |
There was a problem hiding this comment.
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
usesynchronized
methods, so your code will be additionally slowed by synchronization.