diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index d02a746..fac11c8 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -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._]+)(? is palindrome - */ public List findPalindromes(String string) { List 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; } diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..37f3b29 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -1,98 +1,103 @@ +import java.util.ArrayList; + 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) { - // todo - return null; + char[][] result = new char[n][]; + + for (int i = 0; i < n; i++) + { + char[] line = new char[i+1]; + if (i == n-1 || i == 0) + { + for (int j = 0; j <= i; j++) + { + line[j] = '*'; + } + } + else + { + line[0] = '*'; + line[i] = '*'; + + for (int j = i - 1; j > 0; j--) { + line[j] = ' '; + } + } + + result[i] = line; + } + + return result; } - - - /* - 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) { - // todo - return null; + int rows = matrix.length, cols = matrix[0].length; + int elementCount = rows * cols, curElement = 0; + int[] result = new int[elementCount]; + + for (int leyer = 0; curElement < elementCount; leyer++) { + int curi = leyer, maxi = rows - 1 - leyer; + int curj = leyer, maxj = cols - 1 - leyer; + + for (;curj < maxj && curElement < elementCount; curj++, curElement++) + { + result[curElement] = matrix[curi][curj]; + } + for (;curi < maxi && curElement < elementCount; curi++, curElement++) + { + result[curElement] = matrix[curi][curj]; + } + for (;curj > leyer && curElement < elementCount; curj--, curElement++) + { + result[curElement] = matrix[curi][curj]; + } + for (;curi > leyer && curElement < elementCount; curi--, curElement++) + { + result[curElement] = matrix[curi][curj]; + } + + if (curi == maxi || curj == maxj) { + if (curi == maxi && curj == maxj) result[curElement] = matrix[curi][curj]; + break; + } + } + + return result; } - /* - integer partitioning is a combinatorics problem in discreet maths - the problem is to generate sum numbers which their summation is the input number - - 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) { - // todo - return null; + ArrayList result = new ArrayList<>(); + ArrayList curPartition = new ArrayList<>(); + + fillPartition(n, n, curPartition, result); + + return result.toArray(new int[result.size()][]); + } + + private void fillPartition(int max, int remaining, ArrayList curPartition, ArrayList result) { + if (remaining == 0) { + int size = curPartition.size(); + int[] savingArr = new int[size]; + + for (int i = 0; i < size; i++) { + savingArr[i] = curPartition.get(i); + } + + result.add(savingArr); + return; + } + + for (int i = Math.min(max, remaining); i >= 1; i--) { + curPartition.add(i); + int newRemaining = remaining - i; + + fillPartition(i, newRemaining, curPartition, result); + + curPartition.removeLast(); + } }