done: main and partial bonus exercises

This commit is contained in:
2026-04-22 17:07:43 +03:30
parent 5780e034f1
commit 98a1ece080
2 changed files with 176 additions and 11 deletions
+91 -5
View File
@@ -20,7 +20,7 @@ public class BonusExercises {
- Each segment (between dots) must follow same hyphen rules
*/
public boolean validateEmail(String email) {
String regex = ""; // todo
String regex = "^(?!.*\\.\\.)[a-zA-Z0-9]([a-zA-Z0-9_.-]*[a-zA-Z0-9])?@[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);
@@ -39,7 +39,14 @@ public class BonusExercises {
If no match for a date is found in the string, return null.
*/
public String findDate(String string) {
// todo
// String regex = "\\b(\\d{4}[/-]\\d{2}[/-]\\d{2}|\\d{2}[/-]\\d{2}[/-]\\d{4})\\b";
// Pattern pattern = Pattern.compile(regex);
// Matcher matcher = pattern.matcher(string);
// if (matcher.find())
// {
// return matcher.group();
// }
//Checking day-month compatibility!!!!!!
return null;
}
@@ -54,8 +61,62 @@ public class BonusExercises {
- has no white-space in it
*/
public int findValidPasswords(String string) {
// todo
return -1;
if (string == null) {return 0;}
String[] words = string.split("\\s+");
int passwordCount = 0;
for (String word : words)
{
String cleanWord = cleanWord(word);
if (isValid(cleanWord))
{
passwordCount++;
}
}
return passwordCount;
}
private String cleanWord(String word) {
return word.replaceAll("[^a-zA-Z0-9!@#$%^&*]", "");
}
private boolean isValid(String word)
{
if (word.length() < 8)
{
return false;
}
boolean hasUppercase = false;
boolean hasLowercase = false;
boolean hasSpecial = false;
boolean hasNumber = false;
String special = "!@#$%^&*";
for (int i = 0; i < word.length(); i++)
{
char c = word.charAt(i);
if (Character.isUpperCase(c))
{
hasUppercase = true;
}
else if (Character.isLowerCase(c))
{
hasLowercase = true;
}
else if (Character.isDigit(c))
{
hasNumber = true;
}
else if (special.indexOf(c) != -1)
{
hasSpecial = true;
}
else
{
return false;
}
}
return hasNumber && hasLowercase && hasSpecial && hasUppercase;
}
/*
@@ -66,10 +127,35 @@ public class BonusExercises {
*/
public List<String> findPalindromes(String string) {
List<String> list = new ArrayList<>();
// todo
Pattern pattern = Pattern.compile("[a-zA-Z]+");
Matcher matcher = pattern.matcher(string);
while (matcher.find())
{
String word = matcher.group();
if (word.length() >= 3) {
String lowerWord = word.toLowerCase();
if (isPalindrome(lowerWord)) {
list.add(word);
}
}
}
return list;
}
private boolean isPalindrome(String word)
{
boolean isPalindrome = true;
int length = word.length();
for (int i = 0; i < length/2; i++)
{
if (word.charAt(i) != word.charAt(length - i - 1))
{
isPalindrome = false;
}
}
return isPalindrome;
}
public static void main(String[] args) {
// you can test your code here
}