1 Commits
Author SHA1 Message Date
Arshia_Mahdaviani aecf60d06c Done! 2026-07-15 22:48:57 +03:30
6 changed files with 269 additions and 11 deletions
+104 -6
View File
@@ -20,11 +20,39 @@ public class BonusExercises {
- Each segment (between dots) must follow same hyphen rules - Each segment (between dots) must follow same hyphen rules
*/ */
public boolean validateEmail(String email) { public boolean validateEmail(String email) {
String regex = ""; // todo String regex = "^[^@\\s]+@[^@\\s]+$"; // todo
Pattern pattern = Pattern.compile(regex); Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(email); Matcher matcher = pattern.matcher(email);
if(matcher.find())
{
String[] s =email.split("@");
String local_part = s[0];
String domain = s[1];
regex = "(^[.].*$) | (^.*[.]$) | (^(?=.*\\.{2}).*$)";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(local_part);
if(matcher.find())
return false;
regex = "(^-.*$)|(^.*-$)|(_)";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(domain);
if(matcher.find())
return false;
regex = "[.][^.]+[.]";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(domain);
return matcher.matches(); while(matcher.find()) {
regex = "(^-.*$)|(^.*-$)|(_)";
pattern = Pattern.compile(regex);
Matcher matcher1 = pattern.matcher(matcher.group());
if(matcher1.find())
return false;
}
return true;
}
return false;
} }
/* /*
@@ -39,8 +67,51 @@ public class BonusExercises {
If no match for a date is found in the string, return null. If no match for a date is found in the string, return null.
*/ */
public String findDate(String string) { public String findDate(String string) {
// todo
Pattern pattern = Pattern.compile( "(?<american>(?<=\\D)(1[0-2]|0?[1-9])/(3[0-1]|[1-2][0-9]|0?[1-9])/[0-9]{4})"
+ "|(?<british>(?<=\\D)(3[0-1]|[1-2][0-9]|0?[1-9])/(1[0-2]|0?[1-9])/[0-9]{4})"
+ "|(?<iso>(?<=\\D)[0-9]{4}-(1[0-2]|0?[1-9])-(3[0-1]|[1-2][0-9]|0?[1-9]))"
+ "|(?<slash>(?<=\\D)[0-9]{4}/(1[0-2]|0?[1-9])/(3[0-1]|[1-2][0-9]|0?[1-9]))");
Matcher matcher = pattern.matcher(string);
if(!matcher.find())
return null; return null;
String date = matcher.group();
String year ="";
String month ="";
String day ="";
if(matcher.group("american")!=null){
String[] s = date.split("/");
year = s[2];
month = s[0];
day = s[1];
}
else if(matcher.group("british")!=null){
String[] s = date.split("/");
year = s[2];
month = s[1];
day = s[0];
}
else if(matcher.group("iso")!=null){
String[] s = date.split("-");
year = s[0];
month = s[1];
day = s[2];
}
else if(matcher.group("slash")!=null){
String[] s = date.split("/");
year = s[0];
month = s[1];
day = s[2];
}
// is date valid?
int Year =Integer.parseInt(year);
if( (month.matches("0?4|0?6|0?9|11")&&day.matches("31")) ||
((Year%400==0||(Year%100!=0&&Year%4==0))&&month.matches("0?2")&&day.matches("3[0-1]")) ||
(month.matches("0?2")&&day.matches("3[0-1]|29")) )
return null;
return date;
} }
/* /*
@@ -54,9 +125,18 @@ public class BonusExercises {
- has no white-space in it - has no white-space in it
*/ */
public int findValidPasswords(String string) { public int findValidPasswords(String string) {
// todo int count = 0;
return -1; Pattern pattern = Pattern.compile("(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[!@#$%^&*]).{8,}");
for (String s : string.split("\\s+")) {
Matcher matcher = pattern.matcher(s);
if (matcher.matches()) {
count++;
} }
}
return count;
}
/* /*
you should return a list of *words* which are palindromic you should return a list of *words* which are palindromic
@@ -66,7 +146,25 @@ public class BonusExercises {
*/ */
public List<String> findPalindromes(String string) { public List<String> findPalindromes(String string) {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
// todo String[] str = string.split(" ");
Pattern pattern = Pattern.compile("[a-zA-Z]{3,}", Pattern.CASE_INSENSITIVE);
Matcher matcher ;
loop:for (String s:str){
matcher = pattern.matcher(s);
if(matcher.find()){
String temp = matcher.group();
for (int i = 0; i <= temp.length()/2-1; i++) {
int dumy = temp.charAt(i)-temp.charAt(temp.length()-i-1);
if(dumy!=0&&dumy!='A'-'a'&&dumy!='a'-'A')
continue loop;
}
list.add(matcher.group());
}
}
return list; return list;
} }
+96 -4
View File
@@ -1,3 +1,5 @@
import java.util.ArrayList;
import java.util.Arrays;
public class MainExercises public class MainExercises
{ {
/* /*
@@ -19,9 +21,18 @@ public class MainExercises
the output has to be a two-dimensional array of characters, so don't just print the triangle! the output has to be a two-dimensional array of characters, so don't just print the triangle!
*/ */
public char[][] generateTriangle(int n) { public char[][] generateTriangle(int n) {
// todo // todo
return null; char[][] triangle = new char[n][];
for (int i=0; i<n; i++){
triangle[i] = new char[i+1];
for (int j=0; j<=i; j++){
if(i==j||i==n-1||j==0)
triangle[i][j]='*';
else
triangle[i][j]=' ';
}
}
return triangle;
} }
@@ -59,7 +70,63 @@ public class MainExercises
*/ */
public int[] spiralTraversal(int[][] matrix) { public int[] spiralTraversal(int[][] matrix) {
// todo // todo
return null; int[] start = {0,1, matrix[0].length-2,matrix.length-2};
int[] stop = {matrix[0].length-1, matrix.length-1,0,1};
int[] S_T = new int[matrix.length*matrix[0].length];
int pointer = 0;
int Case =0;
int row=0;
int column=0;
/**
*
*
*
start[0]<=stop[0]&&start[1]<=stop[1]&&
start[2]>=stop[2]&&start[3]>=stop[3]
*/
while (pointer < matrix.length*matrix[0].length){
switch (Case){
case 0:
for ( column=start[Case]; column<=stop[Case]; column++){
S_T[pointer++]=matrix[row][column];
}
column--;
start[Case]++;
stop[Case]--;
Case ++;
break;
case 1:
for ( row=start[Case]; row<=stop[Case]; row++){
S_T[pointer++]=matrix[row][column];
}
row--;
start[Case]++;
stop[Case]--;
Case ++;
break;
case 2:
for ( column=start[Case]; column>=stop[Case]; column--){
S_T[pointer++]=matrix[row][column];
}
column++;
start[Case]--;
stop[Case]++;
Case ++;
break;
case 3:
for ( row=start[Case]; row>=stop[Case]; row--){
S_T[pointer++]=matrix[row][column];
}
row++;
start[Case]--;
stop[Case]++;
Case=0;
break;
}
}
return S_T;
} }
/* /*
@@ -92,7 +159,32 @@ public class MainExercises
public int[][] intPartitions(int n) { public int[][] intPartitions(int n) {
// todo // todo
return null; if(n==1)
return new int[][]{new int[]{1}};
ArrayList<int[]> list = new ArrayList<int[]>();
list.add(new int[]{n});
for (int i = n-1; i >=1 ; i--) {
int[][]temp = intPartitions(n-i);
int rows = temp.length;
for (int j = 0; j < rows; j++) {
ArrayList<Integer> merged =new ArrayList<>();
merged.add(i);
if(temp[j][0]<=i) {
for (int k : temp[j]) {
merged.add( k);
}
int[] d = new int[merged.size()];
for(int z=0;z< merged.size();z++) {
d[z] = merged.get(z);
}
list.add(d);
}
}
}
return (int[][]) list.toArray(new int[list.size()][]);
} }
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_24" project-jdk-name="25" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/test.iml" filepath="$PROJECT_DIR$/test.iml" />
</modules>
</component>
</project>
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
</component>
</project>
+48
View File
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="ac272b64-2dd1-434f-b5b7-3d63bb85f743" name="Changes" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/../.." />
</component>
<component name="ProjectColorInfo"><![CDATA[{
"associatedIndex": 2
}]]></component>
<component name="ProjectId" id="3GD0Hh7SguEkean0jp165yqgvdT" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"ModuleVcsDetector.initialDetectionPerformed": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"git-widget-placeholder": "main",
"ignore.virus.scanning.warn.message": "true",
"kotlin-language-version-configured": "true",
"last_opened_file_path": "D:/uni_t4/HW-02-git-and-java-practice/src/test"
}
}]]></component>
<component name="SharedIndexes">
<attachedChunks>
<set>
<option value="bundled-jdk-9823dce3aa75-bf35d07a577b-intellij.indexing.shared.core-IU-252.23892.409" />
</set>
</attachedChunks>
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="ac272b64-2dd1-434f-b5b7-3d63bb85f743" name="Changes" comment="" />
<created>1783490746045</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1783490746045</updated>
</task>
<servers />
</component>
</project>