Doing Bonus exercises

This commit is contained in:
2026-04-24 01:10:46 +03:30
parent f88049c7c4
commit de430d3ebe
+96 -11
View File
@@ -19,8 +19,11 @@ public class BonusExercises {
- Can't have underscores - Can't have underscores
- 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 {
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); Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(email); 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. If no match for a date is found in the string, return null.
*/ */
public String findDate(String string) { public String findDate(String string)
// todo {
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; return null;
} }
@@ -53,9 +79,40 @@ public class BonusExercises {
- at least one number and at least a special char "!@#$%^&*" - at least one number and at least a special char "!@#$%^&*"
- has no white-space in it - has no white-space in it
*/ */
public int findValidPasswords(String string) { public int findValidPasswords(String string)
// todo {
return -1; 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 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<>(); 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; return list;
} }
public static void main(String[] args) { public static void main(String[] args)
// you can test your code here {
} }
} }