Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fa7227822b | ||
|
|
dd399fd00a | ||
|
|
95028697fd |
@@ -5,68 +5,84 @@ import java.util.regex.Pattern;
|
||||
|
||||
public class BonusExercises {
|
||||
|
||||
/*
|
||||
complete the method below, so it will validate an email address
|
||||
1. Must have exactly one @ (no more, no less)
|
||||
2. Split into local-part and domain (before @ and after @)
|
||||
3. Local-part rules:
|
||||
- Can't be empty
|
||||
- Can't start or end with dot
|
||||
- Can't have two dots in a row
|
||||
4. Domain rules:
|
||||
- Can't be empty
|
||||
- Can't start or end with hyphen
|
||||
- Can't have underscores
|
||||
- Each segment (between dots) must follow same hyphen rules
|
||||
*/
|
||||
public boolean validateEmail(String email) {
|
||||
String regex = ""; // todo
|
||||
String regex = "^(?!\\.)(?!.*\\.\\..*)([0-9A-Za-z._]+)(?<!\\.)@(?!-)([0-9A-Za-z.\\-]+)(?<!-)$";
|
||||
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(email);
|
||||
|
||||
return matcher.matches();
|
||||
}
|
||||
|
||||
/*
|
||||
This method should find and return the first date in a string.
|
||||
|
||||
Supported formats:
|
||||
- American: MM/DD/YYYY (e.g., 12/09/2023)
|
||||
- British: DD/MM/YYYY (e.g., 12/09/2023 — same pattern, context matters)
|
||||
- ISO: YYYY-MM-DD (e.g., 2024-07-15)
|
||||
- Slash variant: YYYY/MM/DD (e.g., 2025/01/01)
|
||||
|
||||
If no match for a date is found in the string, return null.
|
||||
*/
|
||||
public String findDate(String string) {
|
||||
// todo
|
||||
String regex = "(\\b((1[0-2])|(0[1-9]))\\b/\\b((0[1-9])|([12][0-9])|(3[01]))\\b/\\b(\\d{4})\\b)|(\\b((0[1-9])|([12][0-9])|(3[01]))\\b/\\b((1[0-2])|(0[1-9]))\\b/\\b(\\d{4})\\b)|(\\b(\\d{4})\\b-\\b((1[0-2])|(0[1-9]))\\b-\\b((0[1-9])|([12][0-9])|(3[01]))\\b)|(\\b(\\d{4})\\b/\\b((1[0-2])|(0[1-9]))\\b/\\b((0[1-9])|([12][0-9])|(3[01]))\\b)";
|
||||
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(string);
|
||||
|
||||
if (matcher.find()) {
|
||||
return matcher.group();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
given a string, implement the method to detect all valid passwords
|
||||
then, it should return the count of them
|
||||
|
||||
a valid password has the following properties:
|
||||
- at least 8 characters
|
||||
- has to include at least one uppercase letter, and at least a lowercase
|
||||
- at least one number and at least a special char "!@#$%^&*"
|
||||
- has no white-space in it
|
||||
*/
|
||||
public int findValidPasswords(String string) {
|
||||
// todo
|
||||
return -1;
|
||||
String passwordRegex = "(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*\\W).{8,}";
|
||||
String tokenRegex = "(?<!\\S)[A-Za-z0-9!@#$%^&*_]{8,}(?!\\S)";
|
||||
|
||||
Pattern tokenPattern = Pattern.compile(tokenRegex);
|
||||
Pattern passwordPattern = Pattern.compile(passwordRegex);
|
||||
Matcher matcher = tokenPattern.matcher(string);
|
||||
|
||||
int count = 0;
|
||||
while (matcher.find()) {
|
||||
String token = matcher.group();
|
||||
|
||||
Matcher passwordMatcher = passwordPattern.matcher(token);
|
||||
if (passwordMatcher.matches()){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
/*
|
||||
you should return a list of *words* which are palindromic
|
||||
by word we mean at least 3 letters with no whitespace in it
|
||||
|
||||
note: your implementation should be case-insensitive, e.g. Aba -> is palindrome
|
||||
*/
|
||||
public List<String> findPalindromes(String string) {
|
||||
List<String> list = new ArrayList<>();
|
||||
// todo
|
||||
String[] inputList = string.split("\\W+");
|
||||
|
||||
for (String word : inputList)
|
||||
{
|
||||
String regex = "";
|
||||
int wordLength = word.length();
|
||||
|
||||
if (wordLength < 3) continue;
|
||||
|
||||
for (int i = 0; i < wordLength/2; i++) {
|
||||
regex += "(.)";
|
||||
}
|
||||
|
||||
if (wordLength % 2 != 0)
|
||||
{
|
||||
regex += ".";
|
||||
}
|
||||
|
||||
for (int i = wordLength/2; i > 0; i--) {
|
||||
regex += "\\" + i;
|
||||
}
|
||||
|
||||
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
|
||||
Matcher matcher = pattern.matcher(word);
|
||||
|
||||
if (matcher.matches())
|
||||
{
|
||||
list.add(word);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,21 +8,26 @@ public class MainExercises
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
result[i] = new char[i+1];
|
||||
if (i == n-1)
|
||||
char[] line = new char[i+1];
|
||||
|
||||
if (i == n-1 || i == 0)
|
||||
{
|
||||
for (int j = 0; j <= i; j++)
|
||||
{
|
||||
result[i][j] = '*';
|
||||
line[j] = '*';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result[i][0] = '*';
|
||||
result[i][i] = '*';
|
||||
line[0] = '*';
|
||||
line[i] = '*';
|
||||
|
||||
for (int j = 1; j < i; j++) result[i][j] = ' ';
|
||||
for (int j = i - 1; j > 0; j--) {
|
||||
line[j] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
result[i] = line;
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -64,59 +69,35 @@ public class MainExercises
|
||||
}
|
||||
|
||||
public int[][] intPartitions(int n) {
|
||||
int[][] result;
|
||||
ArrayList<ArrayList<Integer>> mainArray = new ArrayList<>();
|
||||
ArrayList<int[]> result = new ArrayList<>();
|
||||
ArrayList<Integer> curPartition = new ArrayList<>();
|
||||
|
||||
ArrayList<Integer> start = new ArrayList<>();
|
||||
start.add(n);
|
||||
mainArray.add(start);
|
||||
|
||||
while (true) {
|
||||
ArrayList<Integer> latestArray = mainArray.get(mainArray.size() - 1);
|
||||
fillPartition(n, n, curPartition, result);
|
||||
|
||||
Integer numberOfInterest = 0;
|
||||
for (Integer number : latestArray)
|
||||
{
|
||||
if (number > 1) {
|
||||
numberOfInterest = number;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result.toArray(new int[result.size()][]);
|
||||
}
|
||||
|
||||
if (numberOfInterest == 0) break;
|
||||
private void fillPartition(int max, int remaining, ArrayList<Integer> curPartition, ArrayList<int[]> result) {
|
||||
if (remaining == 0) {
|
||||
int size = curPartition.size();
|
||||
int[] savingArr = new int[size];
|
||||
|
||||
numberOfInterest--;
|
||||
Integer remain = n - numberOfInterest;
|
||||
ArrayList<Integer> newArray = new ArrayList<>();
|
||||
newArray.add(numberOfInterest);
|
||||
|
||||
for (Integer number = numberOfInterest;;) {
|
||||
if (number >= remain) {
|
||||
newArray.add(remain);
|
||||
break;
|
||||
}
|
||||
else {
|
||||
newArray.add(number);
|
||||
remain -= number;
|
||||
}
|
||||
for (int i = 0; i < size; i++) {
|
||||
savingArr[i] = curPartition.get(i);
|
||||
}
|
||||
|
||||
mainArray.add(newArray);
|
||||
result.add(savingArr);
|
||||
return;
|
||||
}
|
||||
|
||||
result = new int[mainArray.size()][];
|
||||
for (int i = Math.min(max, remaining); i >= 1; i--) {
|
||||
curPartition.add(i);
|
||||
int newRemaining = remaining - i;
|
||||
|
||||
for (int i = 0; i < mainArray.size(); i++) {
|
||||
ArrayList<Integer> array = mainArray.get(i);
|
||||
fillPartition(i, newRemaining, curPartition, result);
|
||||
|
||||
result[i] = new int[array.size()];
|
||||
|
||||
for (int j = 0; j < array.size(); j++) {
|
||||
result[i][j] = array.get(j);
|
||||
}
|
||||
curPartition.removeLast();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user