develop #2
Generated
+1
-1
@@ -8,7 +8,7 @@
|
|||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" project-jdk-name="25" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -9,8 +9,8 @@
|
|||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>25</maven.compiler.source>
|
<maven.compiler.source>21</maven.compiler.source>
|
||||||
<maven.compiler.target>25</maven.compiler.target>
|
<maven.compiler.target>21</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
@@ -21,6 +21,13 @@
|
|||||||
<version>5.11.4</version>
|
<version>5.11.4</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.platform</groupId>
|
||||||
|
<artifactId>junit-platform-launcher</artifactId>
|
||||||
|
<version>1.11.4</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -5,71 +5,68 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
public class BonusExercises {
|
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) {
|
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-Z]+)+$";
|
||||||
Pattern pattern = Pattern.compile(regex);
|
Pattern pattern = Pattern.compile(regex);
|
||||||
Matcher matcher = pattern.matcher(email);
|
Matcher matcher = pattern.matcher(email);
|
||||||
|
|
||||||
return matcher.matches();
|
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) {
|
public String findDate(String string) {
|
||||||
// todo
|
if (string == null) {return null;}
|
||||||
|
String regex = "\\b(\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])|\\d{4}/(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])|(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/\\d{4})\\b";
|
||||||
|
Pattern pattern = Pattern.compile(regex);
|
||||||
|
Matcher matcher = pattern.matcher(string);
|
||||||
|
if (matcher.find()) {
|
||||||
|
return matcher.group();
|
||||||
|
}
|
||||||
return null;
|
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) {
|
public int findValidPasswords(String string) {
|
||||||
// todo
|
String regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[^A-Za-z0-9])\\S{8,}$";
|
||||||
return -1;
|
Pattern pattern = Pattern.compile(regex);
|
||||||
|
Matcher matcher = pattern.matcher(string);
|
||||||
|
String[] passwords = string.split(" ");
|
||||||
|
int count = 0;
|
||||||
|
for (String pass: passwords){
|
||||||
|
if (pattern.matcher(pass).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) {
|
public List<String> findPalindromes(String string) {
|
||||||
List<String> list = new ArrayList<>();
|
List<String> list = new ArrayList<>();
|
||||||
// todo
|
String regex = "\\b[a-zA-Z]{3,}\\b";
|
||||||
|
Pattern pattern = Pattern.compile(regex);
|
||||||
|
Matcher matcher = pattern.matcher(string);
|
||||||
|
while (matcher.find()) {
|
||||||
|
if (CheckPalindrome(matcher.group())) {
|
||||||
|
list.add(matcher.group());
|
||||||
|
}
|
||||||
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean CheckPalindrome(String str) {
|
||||||
|
str = str.toLowerCase();
|
||||||
|
int i = 0;
|
||||||
|
int j = str.length() - 1;
|
||||||
|
while (i < str.length() / 2 ) {
|
||||||
|
if (str.charAt(i) != str.charAt(j)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
j--;
|
||||||
|
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// you can test your code here
|
// you can test your code here
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,98 +1,75 @@
|
|||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
public class MainExercises
|
public class MainExercises
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
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 " "
|
|
||||||
|
|
||||||
example 1, input = 3:
|
|
||||||
*
|
|
||||||
**
|
|
||||||
***
|
|
||||||
|
|
||||||
example 2, input = 5:
|
|
||||||
*
|
|
||||||
**
|
|
||||||
* *
|
|
||||||
* *
|
|
||||||
*****
|
|
||||||
|
|
||||||
the output has to be a two-dimensional array of characters, so don't just print the triangle!
|
|
||||||
*/
|
|
||||||
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(j == 0 || j == i || i == n - 1) {
|
||||||
|
triangle[i][j] = '*';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
triangle[i][j] = ' ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return triangle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
SPIRAL TRAVERSAL OF A RECTANGULAR MATRIX
|
|
||||||
|
|
||||||
Given a rectangular matrix (2D array) of integers, this method traverses
|
|
||||||
it in a spiral order (clockwise from outside to inside) and returns
|
|
||||||
the elements as a 1D array.
|
|
||||||
|
|
||||||
EXAMPLE:
|
|
||||||
Input matrix:
|
|
||||||
1 2 3
|
|
||||||
4 5 6
|
|
||||||
7 8 9
|
|
||||||
|
|
||||||
Spiral order: start at top-left (1), go right →, then down ↓,
|
|
||||||
then left ←, then up ↑, then repeat inward.
|
|
||||||
|
|
||||||
Result: {1, 2, 3, 6, 9, 8, 7, 4, 5}
|
|
||||||
|
|
||||||
so you should walk in that matrix in a curl and then add the numbers in order you've seen them in a 1D array
|
|
||||||
|
|
||||||
RECTANGULAR MATRIX ASSUMPTION:
|
|
||||||
This method assumes the input matrix is RECTANGULAR (all rows have
|
|
||||||
the same number of columns). In Java, we can verify this because
|
|
||||||
2D arrays might be jagged (rows of different lengths).
|
|
||||||
|
|
||||||
IMPORTANT: In Java, we do NOT need to pass rows and cols!
|
|
||||||
The 2D array 'matrix' knows its own dimensions:
|
|
||||||
- Number of rows: matrix.length
|
|
||||||
- Number of columns: matrix[0].length (if rectangular)
|
|
||||||
*/
|
|
||||||
public int[] spiralTraversal(int[][] matrix) {
|
public int[] spiralTraversal(int[][] matrix) {
|
||||||
// todo
|
int[] result = new int[matrix.length * matrix[0].length];
|
||||||
return null;
|
int index = 0;
|
||||||
|
while (matrix.length > 0) {
|
||||||
|
for (int i = 0; i < matrix[0].length; i++) {
|
||||||
|
result[index++] = matrix[0][i];
|
||||||
|
}
|
||||||
|
matrix = rotateMatrix(matrix);
|
||||||
|
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
private int[][] rotateMatrix(int[][] matrix) {
|
||||||
integer partitioning is a combinatorics problem in discreet maths
|
int m = matrix.length;
|
||||||
the problem is to generate sum numbers which their summation is the input number
|
int n = matrix[0].length;
|
||||||
|
int[][] newMatrix = new int[n][m -1];
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 1; j < m; j++) {
|
||||||
|
newMatrix[i][j -1] = matrix[j][n -1 -i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newMatrix;
|
||||||
|
}
|
||||||
|
|
||||||
e.g. 1 -> all partitions of integer 3 are:
|
|
||||||
3
|
|
||||||
2, 1
|
|
||||||
1, 1, 1
|
|
||||||
|
|
||||||
e.g. 2 -> for number 4 goes as:
|
|
||||||
4
|
|
||||||
3, 1
|
|
||||||
2, 2
|
|
||||||
2, 1, 1
|
|
||||||
1, 1, 1, 1
|
|
||||||
|
|
||||||
Note: As you can see in the examples, we want to generate distinct partitions,
|
|
||||||
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.
|
|
||||||
|
|
||||||
Hint: You can determine the size and order of the arrays by finding the pattern
|
|
||||||
of partitions and their count. Trust me, this one's fun and easy :)
|
|
||||||
|
|
||||||
If you're familiar with Lists and ArrayLists, you can also edit the method's
|
|
||||||
body to use them instead of arrays.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public int[][] intPartitions(int n) {
|
public int[][] intPartitions(int n) {
|
||||||
// todo
|
List<int[]> result = new ArrayList<>();
|
||||||
return null;
|
int[] partition = new int[n];
|
||||||
|
int length = 0;
|
||||||
|
partition[length] = n;
|
||||||
|
while (true) {
|
||||||
|
result.add(Arrays.copyOf(partition, length+1));
|
||||||
|
int rem = 0;
|
||||||
|
while (length >= 0 && partition[length] == 1){
|
||||||
|
rem += partition[length];
|
||||||
|
length--;
|
||||||
|
}
|
||||||
|
if ( length < 0 ) break;
|
||||||
|
partition[length]--;
|
||||||
|
rem++;
|
||||||
|
while (rem > partition[length]) {
|
||||||
|
partition[length+1] = partition[length];
|
||||||
|
rem -= partition[length];
|
||||||
|
length++;
|
||||||
|
}
|
||||||
|
partition[length+1] = rem;
|
||||||
|
length++;
|
||||||
|
}
|
||||||
|
return result.toArray(new int[0][]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user