matin mortazavi / exercises + bonus
This commit is contained in:
@@ -21,10 +21,99 @@ public class BonusExercises {
|
||||
*/
|
||||
public boolean validateEmail(String email) {
|
||||
String regex = ""; // todo
|
||||
int indexAt = 0;
|
||||
int Atnum = 0 ;
|
||||
int datnum = 0 ;
|
||||
int dat1 = 0;
|
||||
int dat2 = 0 ;
|
||||
int emsize = email.length() ;
|
||||
for (int i = 0 ; i < email.length(); i ++)
|
||||
{
|
||||
if (email.charAt(i) == '@')
|
||||
{
|
||||
indexAt = i ;
|
||||
Atnum ++ ;
|
||||
|
||||
}
|
||||
if (email.charAt(i) == ' ')
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
}
|
||||
if (Atnum != 1)
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
if(indexAt == 0)
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
if (indexAt == 1 && email.charAt(0) == ' ')
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
if (email.charAt(0) == '.' || email.charAt(Atnum-1) == '.')
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
for (int i = 0 ; i <Atnum ; i++)
|
||||
{
|
||||
if (email.charAt(i) == '.')
|
||||
{
|
||||
|
||||
datnum ++ ;
|
||||
|
||||
}
|
||||
if (datnum == 1)
|
||||
{
|
||||
dat1 = i;
|
||||
}
|
||||
if (datnum == 2)
|
||||
{
|
||||
dat2 = i ;
|
||||
}
|
||||
}
|
||||
if (datnum > 1)
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
if(indexAt== (emsize-1))
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
if (indexAt == (emsize-2) && email.charAt(emsize-1) == ' ' )
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
if (email.charAt(indexAt+1) == '-' || email.charAt(emsize-1) == '-')
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
for (int i = indexAt ; i <emsize ; i++)
|
||||
{
|
||||
if (email.charAt(i) == '_')
|
||||
{
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
if(datnum == 2)
|
||||
{
|
||||
if (email.charAt(dat1+1) == '-' || email.charAt(dat2-1) == '-')
|
||||
{
|
||||
return false ;
|
||||
}
|
||||
else {
|
||||
return true ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(email);
|
||||
|
||||
return matcher.matches();
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -40,7 +129,33 @@ public class BonusExercises {
|
||||
*/
|
||||
public String findDate(String string) {
|
||||
// todo
|
||||
return null;
|
||||
int StartIndex = 0 ;
|
||||
String Final = "" ;
|
||||
boolean CheckNull = true ;
|
||||
String number = "0123456789" ;
|
||||
int StrSize = string.length() ;
|
||||
for (int i = 0 ; i < StrSize ; i ++)
|
||||
{
|
||||
char x = string.charAt(i) ;
|
||||
if(number.indexOf(x) != -1)
|
||||
{
|
||||
StartIndex = i ;
|
||||
CheckNull = false ;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (CheckNull == true)
|
||||
{
|
||||
return null ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (int i = StartIndex ; i <StartIndex+10 ; i ++)
|
||||
{
|
||||
Final = Final + string.charAt(i) ;
|
||||
}
|
||||
return Final;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -55,7 +170,58 @@ public class BonusExercises {
|
||||
*/
|
||||
public int findValidPasswords(String string) {
|
||||
// todo
|
||||
return -1;
|
||||
|
||||
String number = "0123456789" ;
|
||||
String special = "!@#$%^&*" ;
|
||||
int FinalNumber = 0 ;
|
||||
|
||||
int StrSize = string.length() ;
|
||||
for(int i = 0 ; i < StrSize ; i ++)
|
||||
{
|
||||
if(string.charAt(i)==' ') continue;
|
||||
|
||||
|
||||
|
||||
boolean check_upper = false ;
|
||||
boolean check_lower = false ;
|
||||
boolean check_number = false ;
|
||||
boolean check_special = false ;
|
||||
boolean len = false ;
|
||||
char x = string.charAt(i) ;
|
||||
if (x != ' ' ) {
|
||||
int j = i ;
|
||||
while ( j < StrSize && string.charAt(j) != ' ') {
|
||||
char y = string.charAt(j) ;
|
||||
if (Character.isDigit(y)) {
|
||||
check_number = true;
|
||||
}
|
||||
if (Character.isUpperCase(y)) {
|
||||
check_upper = true;
|
||||
}
|
||||
if (Character.isLowerCase(y)) {
|
||||
check_lower = true;
|
||||
}
|
||||
if (special.indexOf(y) != -1) {
|
||||
check_special = true;
|
||||
}
|
||||
|
||||
j ++ ;
|
||||
|
||||
}
|
||||
if ((j-i)>=8)
|
||||
{
|
||||
len = true ;
|
||||
}
|
||||
i = j-1 ;
|
||||
if (len && check_lower && check_number && check_special && check_upper)
|
||||
{
|
||||
FinalNumber ++ ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return FinalNumber;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -67,6 +233,34 @@ public class BonusExercises {
|
||||
public List<String> findPalindromes(String string) {
|
||||
List<String> list = new ArrayList<>();
|
||||
// todo
|
||||
|
||||
int size = string.length() ;
|
||||
for (int i = 0 ; i < size ; i ++)
|
||||
{
|
||||
if(string.charAt(i)==' ') continue;
|
||||
String pal = "" ;
|
||||
String pal2 = "" ;
|
||||
String pal3 = "" ;
|
||||
String spe = "!?.*," ;
|
||||
int j = i ;
|
||||
|
||||
while (j < size && string.charAt(j) != ' ' ) {
|
||||
char y = string.charAt(j) ;
|
||||
if (Character.isLetter(y)) {
|
||||
pal3 = pal3 + y;
|
||||
pal = Character.toLowerCase(y) + pal;
|
||||
pal2 = pal2 + Character.toLowerCase(y);
|
||||
}
|
||||
j++ ;
|
||||
|
||||
|
||||
}
|
||||
if (pal2.equals(pal) && pal3.length()>=3)
|
||||
{
|
||||
list.add(pal3) ;
|
||||
}
|
||||
i = j - 1 ;
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user