Implemented all method for the MainExercises and some of the BonusExercises methods : #1

Open
farnam_jhn wants to merge 1 commits from develop into main
2 changed files with 224 additions and 12 deletions
+91 -3
View File
@@ -54,8 +54,60 @@ public class BonusExercises {
- has no white-space in it
*/
public int findValidPasswords(String string) {
// todo
return -1;
int validPasswordsCount = 0;
for (int i = 0; i <= string.length() - 8;) {
int bestJ = -1;
for (int j = i + 8; j <= string.length(); j++) {
String tempPassword = string.substring(i,j);
if (tempPassword.contains(" ")){
continue;
}
boolean hasUppercase = false;
boolean hasLowercase = false;
boolean hasDigit = false;
boolean hasSpecialCharacter = false;
String specialChars = "!@#$%^&*";
String digits = "1234567890";
String upperCases = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lowerCases = "abcdefghijklmnopqrstuvwxyz";
for (int k = 0; k < tempPassword.length(); k++) {
char currentChar = tempPassword.charAt(k);
if (specialChars.contains(String.valueOf(currentChar))){
hasSpecialCharacter = true;
} else if (upperCases.contains(String.valueOf(currentChar))) {
hasUppercase = true;
} else if (lowerCases.contains(String.valueOf(currentChar))) {
hasLowercase = true;
} else if (digits.contains(String.valueOf(currentChar))) {
hasDigit = true;
}
}
if (hasDigit && hasLowercase && hasUppercase && hasSpecialCharacter){
validPasswordsCount++;
i = j;
bestJ = j;
break;
}
}
if (bestJ == -1){
i++;
}
}
return validPasswordsCount;
}
/*
@@ -66,10 +118,46 @@ public class BonusExercises {
*/
public List<String> findPalindromes(String string) {
List<String> list = new ArrayList<>();
// todo
String[] wordsArray = string.split("\\s+");
for (String s : wordsArray){
String cleanedS = s;
if (cleanedS.endsWith(",")) {
cleanedS = cleanedS.substring(0, cleanedS.length() - 1);
}
if (cleanedS.isEmpty()) {
continue;
}
if (isPalindrome(cleanedS)){
list.add(cleanedS);
}
}
return list;
}
public boolean isPalindrome(String word) {
if (word.length() == 1){
return false;
}
String lowerWord = word.toLowerCase();
StringBuilder mutableInverse = new StringBuilder(lowerWord.length());
for (int i = lowerWord.length() - 1; i >= 0; i--){
mutableInverse.append(lowerWord.charAt(i));
}
String inverseOfWord = mutableInverse.toString();
return lowerWord.equals(inverseOfWord);
}
public static void main(String[] args) {
// you can test your code here
}
+133 -9
View File
@@ -1,3 +1,6 @@
import java.util.ArrayList;
import java.util.List;
public class MainExercises
{
/*
@@ -20,8 +23,23 @@ public class MainExercises
*/
public char[][] generateTriangle(int n) {
// todo
return null;
char[][] triangle = new char[n][n];
for (int i = 0; i < n; i++){
triangle[i] = new char[i+1];
for (int j = 0; j <= i; j++){
if (j == 0 ||
i == j ||
i == n -1){
triangle[i][j] = '*';
}
else {
triangle[i][j] = ' ';
}
}
}
return triangle;
}
@@ -58,8 +76,77 @@ public class MainExercises
- Number of columns: matrix[0].length (if rectangular)
*/
public int[] spiralTraversal(int[][] matrix) {
// todo
return null;
int stepedCount = 0;
int toSteped = matrix.length * matrix[0].length;
boolean[][] steped = new boolean[matrix.length][matrix[0].length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
steped[i][j] = false;
}
}
int[] result = new int[toSteped];
int currentDir = 0; // 0 : right, 1 : down, 2 : left, 3 : up
int currentX = 0, currentY = 0;
while (stepedCount < toSteped){
switch (currentDir){
case 0:
while (currentX < matrix[0].length && !steped[currentY][currentX]){
steped[currentY][currentX] = true;
result[stepedCount++] = matrix[currentY][currentX];
currentX++;
}
currentX--;
break;
case 1:
while (currentY < matrix.length && !steped[currentY][currentX]){
steped[currentY][currentX] = true;
result[stepedCount++] = matrix[currentY][currentX];
currentY++;
}
currentY--;
break;
case 2:
while (currentX >= 0 && !steped[currentY][currentX]){
steped[currentY][currentX] = true;
result[stepedCount++] = matrix[currentY][currentX];
currentX--;
}
currentX++;
break;
case 3:
while (currentY >= 0 && !steped[currentY][currentX]){
steped[currentY][currentX] = true;
result[stepedCount++] = matrix[currentY][currentX];
currentY--;
}
currentY++;
break;
}
currentDir = (currentDir + 1) % 4;
switch (currentDir){
case 0:
currentX++;
break;
case 1:
currentY++;
break;
case 2:
currentX--;
break;
case 3:
currentY--;
}
}
return result;
}
/*
@@ -89,15 +176,52 @@ public class MainExercises
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;
List<int[]> result = new ArrayList<>();
generatePartitions(n, n, new ArrayList<>(), result);
return result.toArray(new int[0][]); // converting a list that consists of arrays into a 2d array
}
private void generatePartitions(int target, int maxValue, List<Integer> current, List<int[]> result) {
// target : the value that is left from the sum. e.g. n = 4, current = [2,1] then the sum lacks 1 so target = 1
if (target == 0) {
// if we reach target = 0 it means sum of the elements in the current array is the same as target number.
// converting current list into an integer array
int[] arr = new int[current.size()];
for (int i = 0; i < current.size(); i++) {
arr[i] = current.get(i);
}
// adding the converted array into result list
result.add(arr);
return;
}
/* How it works :
* we start with the input number as and starting point
* then this number can be composed into the greatest number before it (number -1) and 1
* now the new maximum number in sum can be composed too.
* in order to track all the possible combinations and not just those with 1s i implemented a for loop that goes backward
* it's backward so we never get the same array twice.
* it starts from minimum of target and maxValue because the maxValue might cause the sum to go beyond the target number.
* */
for (int i = Math.min(target, maxValue); i >= 1; i--) {
current.add(i);
generatePartitions(target - i, i, current, result);
// Back tracking
current.remove(current.size() - 1);
}
}
public static void main()
{
{;
}
}