HW 2
This commit is contained in:
Generated
+1
@@ -0,0 +1 @@
|
||||
MainExercises.java
|
||||
@@ -3,6 +3,10 @@ import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/* First Name and Last Name: Hesam Ghazi
|
||||
Student Number: 403222015
|
||||
*/
|
||||
|
||||
public class BonusExercises {
|
||||
|
||||
/*
|
||||
@@ -20,10 +24,18 @@ public class BonusExercises {
|
||||
- Each segment (between dots) must follow same hyphen rules
|
||||
*/
|
||||
public boolean validateEmail(String email) {
|
||||
String regex = ""; // todo
|
||||
|
||||
String regex =
|
||||
"^(?!.*\\.\\.)" + // no consecutive dots
|
||||
"[^.][A-Za-z0-9._%+-]*[^.]@" + // local part
|
||||
"(?!-)" + // domain can't start with -
|
||||
"[A-Za-z0-9-]+" +
|
||||
"(\\.[A-Za-z0-9-]+)*" + // domain segments
|
||||
"(?<!-)$"; // domain can't end with -
|
||||
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(email);
|
||||
|
||||
// Returns true only if the entire string matches
|
||||
return matcher.matches();
|
||||
}
|
||||
|
||||
@@ -39,10 +51,46 @@ 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{1,2}/\\d{1,2}/\\d{4}|\\d{4}-\\d{2}-\\d{2}|\\d{4}/\\d{2}/\\d{2})\\b";
|
||||
|
||||
Matcher matcher = Pattern.compile(regex).matcher(string);
|
||||
// Loop through all possible matches in the input string
|
||||
while (matcher.find()) {
|
||||
// Extract the matched date string
|
||||
String date = matcher.group();
|
||||
|
||||
try {
|
||||
// Try to validate ISO format (YYYY-MM-DD)
|
||||
// LocalDate.parse() will throw exception if invalid
|
||||
if (date.contains("-")) {
|
||||
java.time.LocalDate.parse(date);
|
||||
}
|
||||
// Handle format: YYYY/MM/DD
|
||||
else if (date.matches("\\d{4}/.*")) {
|
||||
java.time.LocalDate.parse(
|
||||
date,
|
||||
java.time.format.DateTimeFormatter.ofPattern("yyyy/MM/dd"));
|
||||
}
|
||||
// Handle format: MM/DD/YYYY (default slash format)
|
||||
else {
|
||||
java.time.LocalDate.parse(
|
||||
date,
|
||||
java.time.format.DateTimeFormatter.ofPattern("MM/dd/yyyy"));
|
||||
}
|
||||
|
||||
return date;
|
||||
|
||||
} catch (Exception e) {
|
||||
// invalid date, continue searching
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
given a string, implement the method to detect all valid passwords
|
||||
then, it should return the count of them
|
||||
@@ -54,8 +102,29 @@ public class BonusExercises {
|
||||
- has no white-space in it
|
||||
*/
|
||||
public int findValidPasswords(String string) {
|
||||
// todo
|
||||
return -1;
|
||||
|
||||
String regex =
|
||||
"^(?=.*[a-z])" +
|
||||
"(?=.*[A-Z])" +
|
||||
"(?=.*\\d)" +
|
||||
"(?=.*[!@#$%^&*])" +
|
||||
// No whitespace and minimum length 8
|
||||
"\\S{8,}$";
|
||||
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
// Assume passwords are separated by whitespace
|
||||
String[] words = string.split("\\s+");
|
||||
|
||||
int count = 0;
|
||||
|
||||
for (String word : words) {
|
||||
// Check whether current token is a valid password
|
||||
if (pattern.matcher(word).matches()) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -65,12 +134,56 @@ public class BonusExercises {
|
||||
note: your implementation should be case-insensitive, e.g. Aba -> is palindrome
|
||||
*/
|
||||
public List<String> findPalindromes(String string) {
|
||||
|
||||
List<String> list = new ArrayList<>();
|
||||
// todo
|
||||
// Split text into words
|
||||
String[] words = string.split("\\s+");
|
||||
|
||||
for (String word : words) {
|
||||
|
||||
// remove punctuation around words
|
||||
String cleaned = word.replaceAll("[^A-Za-z]", "");
|
||||
|
||||
if (cleaned.length() < 3) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String lower = cleaned.toLowerCase();
|
||||
|
||||
boolean palindrome = true;
|
||||
|
||||
for (int i = 0; i < lower.length() / 2; i++) {
|
||||
if (lower.charAt(i) != lower.charAt(lower.length() - 1 - i)) {
|
||||
palindrome = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (palindrome) {
|
||||
list.add(cleaned);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
// you can test your code here
|
||||
|
||||
BonusExercises ex = new BonusExercises();
|
||||
|
||||
System.out.println(ex.validateEmail("john.doe@gmail.com"));
|
||||
System.out.println(ex.validateEmail(".john@gmail.com"));
|
||||
|
||||
System.out.println(
|
||||
ex.findDate("Meeting on 2025-07-15 at noon"));
|
||||
|
||||
System.out.println(
|
||||
ex.findValidPasswords(
|
||||
"abc Test123! Password1@ weakpass HELLO123!"));
|
||||
|
||||
System.out.println(
|
||||
ex.findPalindromes(
|
||||
"Anna level racecar hello Madam kayak"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
public class MainExercises
|
||||
|
||||
/* first Name and last Name: Hesam Ghazi
|
||||
Student Number: 403222015
|
||||
*/
|
||||
{
|
||||
/*
|
||||
you should create a triangle with "*" and return a two-dimensional array of characters based on that
|
||||
@@ -19,9 +25,43 @@ public class MainExercises
|
||||
the output has to be a two-dimensional array of characters, so don't just print the triangle!
|
||||
*/
|
||||
public char[][] generateTriangle(int n) {
|
||||
char[][] triangle = new char[n][];
|
||||
// For n <= 3 the examples show a completely filled triangle.
|
||||
if (n <= 3) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
// Allocate row i with length i+1
|
||||
triangle[i] = new char[i + 1];
|
||||
for (int j = 0; j <= i; j++) {
|
||||
triangle[i][j] = '*';
|
||||
}
|
||||
}
|
||||
return triangle;
|
||||
}
|
||||
// For n > 3, i build a hollow triangle
|
||||
for (int i = 0; i < n; i++) {
|
||||
// Allocate row i
|
||||
triangle[i] = new char[i + 1];
|
||||
// Initialize all positions with spaces
|
||||
for (int j = 0; j <= i; j++) {
|
||||
triangle[i][j] = ' ';
|
||||
}
|
||||
// First row contains only one star
|
||||
if (i == 0) {
|
||||
triangle[i][0] = '*';
|
||||
// Last row is completely filled with stars
|
||||
} else if (i == n - 1) {
|
||||
for (int j = 0; j <= i; j++) {
|
||||
triangle[i][j] = '*';
|
||||
}
|
||||
}
|
||||
// Middle rows contain stars only at the boundaries
|
||||
else {
|
||||
triangle[i][0] = '*';
|
||||
triangle[i][i] = '*';
|
||||
}
|
||||
}
|
||||
|
||||
// todo
|
||||
return null;
|
||||
return triangle;
|
||||
|
||||
}
|
||||
|
||||
@@ -58,8 +98,51 @@ public class MainExercises
|
||||
- Number of columns: matrix[0].length (if rectangular)
|
||||
*/
|
||||
public int[] spiralTraversal(int[][] matrix) {
|
||||
// todo
|
||||
return null;
|
||||
if (matrix == null || matrix.length == 0) {
|
||||
return new int[0];
|
||||
}
|
||||
|
||||
int rows = matrix.length;
|
||||
int cols = matrix[0].length;
|
||||
// Result array will contain every matrix element exactly once
|
||||
int[] result = new int[rows * cols];
|
||||
int index = 0;
|
||||
// Here i define the current boundaries of the spiral
|
||||
int top = 0;
|
||||
int bottom = rows - 1;
|
||||
int left = 0;
|
||||
int right = cols - 1;
|
||||
|
||||
while (top <= bottom && left <= right) {
|
||||
// Traverse top row from left to right
|
||||
for (int j = left; j <= right; j++) {
|
||||
result[index++] = matrix[top][j];
|
||||
}
|
||||
top++;
|
||||
// Traverse right column from top to bottom
|
||||
for (int i = top; i <= bottom; i++) {
|
||||
result[index++] = matrix[i][right];
|
||||
}
|
||||
right--;
|
||||
// Traverse bottom row from right to left
|
||||
// Only if a valid row remains
|
||||
if (top <= bottom) {
|
||||
for (int j = right; j >= left; j--) {
|
||||
result[index++] = matrix[bottom][j];
|
||||
}
|
||||
bottom--;
|
||||
}
|
||||
// Traverse left column from bottom to top
|
||||
// Only if a valid column remains
|
||||
if (left <= right) {
|
||||
for (int i = bottom; i >= top; i--) {
|
||||
result[index++] = matrix[i][left];
|
||||
}
|
||||
left++;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -91,13 +174,64 @@ public class MainExercises
|
||||
*/
|
||||
|
||||
public int[][] intPartitions(int n) {
|
||||
// todo
|
||||
return null;
|
||||
// Stores all partitions that are generated
|
||||
List<int[]> partitions = new ArrayList<>();
|
||||
// Start recursive generation
|
||||
generatePartitions(n, n, new ArrayList<>(), partitions);
|
||||
|
||||
int[][] result = new int[partitions.size()][];
|
||||
|
||||
for (int i = 0; i < partitions.size(); i++) {
|
||||
result[i] = partitions.get(i);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void generatePartitions(
|
||||
int remaining,
|
||||
int max,
|
||||
List<Integer> current,
|
||||
List<int[]> result) {
|
||||
|
||||
if (remaining == 0) {
|
||||
|
||||
int[] partition = new int[current.size()];
|
||||
|
||||
for (int i = 0; i < current.size(); i++) {
|
||||
partition[i] = current.get(i);
|
||||
}
|
||||
|
||||
result.add(partition);
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = Math.min(max, remaining); i >= 1; i--) {
|
||||
|
||||
current.add(i);
|
||||
|
||||
generatePartitions(
|
||||
remaining - i,
|
||||
i,
|
||||
current,
|
||||
result);
|
||||
|
||||
current.remove(current.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main()
|
||||
{
|
||||
public static void main(String[] args) {
|
||||
|
||||
MainExercises ex = new MainExercises();
|
||||
|
||||
int[][] parts = ex.intPartitions(4);
|
||||
|
||||
for (int[] p : parts) {
|
||||
for (int x : p) {
|
||||
System.out.print(x + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user