Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e73ac6115f | ||
|
|
959c579550 |
@@ -21,99 +21,10 @@ public class BonusExercises {
|
|||||||
*/
|
*/
|
||||||
public boolean validateEmail(String email) {
|
public boolean validateEmail(String email) {
|
||||||
String regex = ""; // todo
|
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);
|
Pattern pattern = Pattern.compile(regex);
|
||||||
Matcher matcher = pattern.matcher(email);
|
Matcher matcher = pattern.matcher(email);
|
||||||
|
|
||||||
return true;
|
return matcher.matches();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -129,33 +40,7 @@ public class BonusExercises {
|
|||||||
*/
|
*/
|
||||||
public String findDate(String string) {
|
public String findDate(String string) {
|
||||||
// todo
|
// todo
|
||||||
int StartIndex = 0 ;
|
return null;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -170,98 +55,18 @@ public class BonusExercises {
|
|||||||
*/
|
*/
|
||||||
public int findValidPasswords(String string) {
|
public int findValidPasswords(String string) {
|
||||||
// todo
|
// 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
you should return a list of *words* which are palindromic
|
you should return a list of *words* which are palindromic
|
||||||
by word we mean at least 3 letters with no whitespace in it
|
by word we mean at least 3 letters with no whitespace in it
|
||||||
vv
|
|
||||||
|
|
||||||
note: your implementation should be case-insensitive, e.g. Aba -> is palindrome
|
note: your implementation should be case-insensitive, e.g. Aba -> is palindrome
|
||||||
*/
|
*/
|
||||||
public List<String> findPalindromes(String string) {
|
public List<String> findPalindromes(String string) {
|
||||||
List<String> list = new ArrayList<>();
|
List<String> list = new ArrayList<>();
|
||||||
// todo
|
// 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;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import java.util.ArrayList ;
|
public class MainExercises
|
||||||
import java.util.List;
|
{
|
||||||
|
|
||||||
public class MainExercises {
|
|
||||||
/*
|
/*
|
||||||
you should create a triangle with "*" and return a two-dimensional array of characters based on that
|
you should create a triangle with "*" and return a two-dimensional array of characters based on that
|
||||||
the triangle's area is empty, which means some characters should be " "
|
the triangle's area is empty, which means some characters should be " "
|
||||||
@@ -22,23 +20,13 @@ public class MainExercises {
|
|||||||
*/
|
*/
|
||||||
public char[][] generateTriangle(int n) {
|
public char[][] generateTriangle(int n) {
|
||||||
|
|
||||||
char[][] triangle = new char[n][];
|
// todo
|
||||||
for (int i = 0; i < n; i++) {
|
return null;
|
||||||
triangle[i] = new char[i + 1];
|
|
||||||
for (int j = 0; j <= i; j++) {
|
|
||||||
if (i == 0 || j == 0 || i == j || i == (n - 1)) {
|
|
||||||
triangle[i][j] = '*';
|
|
||||||
} else {
|
|
||||||
triangle[i][j] = ' ';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return triangle;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
SPIRAL TRAVERSAL OF A RECTANGULAR MATRIX
|
SPIRAL TRAVERSAL OF A RECTANGULAR MATRIX
|
||||||
|
|
||||||
@@ -70,51 +58,10 @@ public class MainExercises {
|
|||||||
- Number of columns: matrix[0].length (if rectangular)
|
- Number of columns: matrix[0].length (if rectangular)
|
||||||
*/
|
*/
|
||||||
public int[] spiralTraversal(int[][] matrix) {
|
public int[] spiralTraversal(int[][] matrix) {
|
||||||
int rows = matrix.length;
|
// todo
|
||||||
int col = matrix[0].length;
|
return null;
|
||||||
|
|
||||||
int size = 0 ;
|
|
||||||
boolean increase = true;
|
|
||||||
int range = rows * col ;
|
|
||||||
int check = 0 ;
|
|
||||||
int UP = 0 ;
|
|
||||||
int DOWN = rows-1 ;
|
|
||||||
int LEFT = 0 ;
|
|
||||||
int RIGHT = col -1;
|
|
||||||
|
|
||||||
int[] newmat = new int[rows*col] ;
|
|
||||||
while (UP <= DOWN && LEFT<= RIGHT) {
|
|
||||||
for (int j = LEFT; j <= RIGHT; j++) {
|
|
||||||
newmat[size] = matrix[UP][j];
|
|
||||||
size++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
UP++;
|
|
||||||
for (int i = UP; i <= DOWN; i++) {
|
|
||||||
newmat[size] = matrix[i][RIGHT ];
|
|
||||||
size++;
|
|
||||||
}
|
|
||||||
RIGHT--;
|
|
||||||
if(UP <= DOWN) {
|
|
||||||
for (int j = RIGHT ; j >= LEFT; j--) {
|
|
||||||
newmat[size] = matrix[DOWN ][j];
|
|
||||||
size++;
|
|
||||||
}
|
|
||||||
DOWN--;
|
|
||||||
}
|
|
||||||
if(LEFT <= RIGHT) {
|
|
||||||
for (int i = DOWN ; i >= UP; i--) {
|
|
||||||
newmat[size] = matrix[i][LEFT];
|
|
||||||
size++;
|
|
||||||
}
|
|
||||||
LEFT++;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return newmat ;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
integer partitioning is a combinatorics problem in discreet maths
|
integer partitioning is a combinatorics problem in discreet maths
|
||||||
the problem is to generate sum numbers which their summation is the input number
|
the problem is to generate sum numbers which their summation is the input number
|
||||||
@@ -132,7 +79,7 @@ public class MainExercises {
|
|||||||
1, 1, 1, 1
|
1, 1, 1, 1
|
||||||
|
|
||||||
Note: As you can see in the examples, we want to generate distinct partitions,
|
Note: As you can see in the examples, we want to generate distinct partitions,
|
||||||
which m eans 1,2 and 2,1 are not different — they count as the same combination.
|
which means 1,2 and 2,1 are not different — they count as the same combination.
|
||||||
|
|
||||||
You should generate all partitions of the input number.
|
You should generate all partitions of the input number.
|
||||||
|
|
||||||
@@ -143,41 +90,9 @@ public class MainExercises {
|
|||||||
body to use them instead of arrays.
|
body to use them instead of arrays.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
public int[][] intPartitions(int n) {
|
public int[][] intPartitions(int n) {
|
||||||
ArrayList <int[]> all = new ArrayList<>() ;
|
// todo
|
||||||
ArrayList<Integer>list = new ArrayList<>() ;
|
return null;
|
||||||
part(n , n ,list , all);
|
|
||||||
return convertTo2DArray(all) ;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
public static int[][] convertTo2DArray(ArrayList<int[]> all) {
|
|
||||||
int [][] mat = new int [all.size()][] ;
|
|
||||||
for (int i = 0 ; i < all.size() ; i ++ )
|
|
||||||
{
|
|
||||||
mat[i] = all.get(i) ;
|
|
||||||
}
|
|
||||||
return mat;
|
|
||||||
}
|
|
||||||
public static void part(int rem , int max , ArrayList<Integer> list , ArrayList<int []> all )
|
|
||||||
{
|
|
||||||
if (rem == 0)
|
|
||||||
{
|
|
||||||
int[] final1 = new int [list.size()] ;
|
|
||||||
for (int i = 0 ; i < list.size(); i ++ )
|
|
||||||
{
|
|
||||||
final1[i] = list.get(i) ;
|
|
||||||
}
|
|
||||||
all.add(final1) ;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (int i = Math.min(rem , max) ; i >=1 ; i -- )
|
|
||||||
{
|
|
||||||
list.add(i) ;
|
|
||||||
part(rem-i , i , list, all );
|
|
||||||
list.remove(list.size()-1) ;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user