Doing Bonus exercises
This commit is contained in:
@@ -19,8 +19,11 @@ public class BonusExercises {
|
||||
- Can't have underscores
|
||||
- Each segment (between dots) must follow same hyphen rules
|
||||
*/
|
||||
public boolean validateEmail(String email) {
|
||||
String regex = ""; // todo
|
||||
public boolean validateEmail(String email)
|
||||
{
|
||||
if(email == null) return false;
|
||||
|
||||
String regex = "^(?!.*\\.\\.)(?!.*@.*@)[^@.\\s][^@\\s]*@([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?\\.)*[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?$";
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(email);
|
||||
|
||||
@@ -38,8 +41,31 @@ public class BonusExercises {
|
||||
|
||||
If no match for a date is found in the string, return null.
|
||||
*/
|
||||
public String findDate(String string) {
|
||||
// todo
|
||||
public String findDate(String string)
|
||||
{
|
||||
if(string.isEmpty()) return null;
|
||||
|
||||
String isoPattern = "\\d{4}-\\d{2}-\\d{2}";
|
||||
Pattern isoP = Pattern.compile(isoPattern);
|
||||
Matcher isoMatcher = isoP.matcher(string);
|
||||
if(isoMatcher.find()) return isoMatcher.group();
|
||||
|
||||
String slashVariantPattern = "\\d{4}/\\d{2}/\\d{2}";
|
||||
Pattern slashP = Pattern.compile(slashVariantPattern);
|
||||
Matcher slashMatcher = slashP.matcher(string);
|
||||
if(slashMatcher.find()) return slashMatcher.group();
|
||||
|
||||
|
||||
String usPattern = "(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])/\\d{4}";
|
||||
Pattern usP =Pattern.compile(usPattern);
|
||||
Matcher usMatcher = usP.matcher(string);
|
||||
if(usMatcher.find()) return usMatcher.group();
|
||||
|
||||
String ukPattern = "(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/\\d{4}";
|
||||
Pattern ukP =Pattern.compile(ukPattern);
|
||||
Matcher ukMatcher = ukP.matcher(string);
|
||||
if(ukMatcher.find()) return ukMatcher.group();
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -53,9 +79,40 @@ public class BonusExercises {
|
||||
- at least one number and at least a special char "!@#$%^&*"
|
||||
- has no white-space in it
|
||||
*/
|
||||
public int findValidPasswords(String string) {
|
||||
// todo
|
||||
return -1;
|
||||
public int findValidPasswords(String string)
|
||||
{
|
||||
if(string.isEmpty()) return 0;
|
||||
|
||||
String[] words = string.split(" ");
|
||||
int count = 0;
|
||||
|
||||
for(String word : words)
|
||||
{
|
||||
if(word.length() < 8) continue;
|
||||
|
||||
boolean hasUpper = false;
|
||||
boolean hasLower = false;
|
||||
boolean hasSpecialChar = false;
|
||||
boolean hasNumber = false;
|
||||
|
||||
for(int i = 0; i < word.length(); i++)
|
||||
{
|
||||
char x = word.charAt(i);
|
||||
if (x >= 'A' && x <= 'Z') hasUpper = true;
|
||||
if (x >= 'a' && x <= 'z') hasLower = true;
|
||||
if (x >= '0' && x <= '9') hasNumber = true;
|
||||
}
|
||||
if(word.toString().contains("!") || word.toString().contains("@")
|
||||
|| word.toString().contains("#") || word.toString().contains("$")
|
||||
|| word.toString().contains("%") || word.toString().contains("*")
|
||||
|| word.toString().contains("^") || word.toString().contains("&"))
|
||||
{
|
||||
hasSpecialChar = true;
|
||||
}
|
||||
|
||||
if(hasUpper && hasLower && hasNumber && hasSpecialChar) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -64,13 +121,41 @@ public class BonusExercises {
|
||||
|
||||
note: your implementation should be case-insensitive, e.g. Aba -> is palindrome
|
||||
*/
|
||||
public List<String> findPalindromes(String string) {
|
||||
|
||||
public boolean isPalindrome(String n)
|
||||
{
|
||||
String a = n.toLowerCase();
|
||||
boolean isEqual = true;
|
||||
for(int i = 0; i < (a.length()) / 2; i++)
|
||||
{
|
||||
if(a.charAt(i) != a.charAt(n.length() - 1 - i))
|
||||
{
|
||||
isEqual = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(isEqual) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<String> findPalindromes(String string)
|
||||
{
|
||||
List<String> list = new ArrayList<>();
|
||||
// todo
|
||||
|
||||
if(string.isEmpty()) return list;
|
||||
|
||||
String[] words = string.split(" ");
|
||||
for(int i = 0; i < words.length; i++)
|
||||
{
|
||||
String newWord = words[i].replaceAll("[^a-zA-Z0-9]", "");
|
||||
if(newWord.length() < 3) continue;
|
||||
else if(isPalindrome(newWord) == true) list.add(newWord);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// you can test your code here
|
||||
public static void main(String[] args)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user