Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 76 additions & 12 deletions src/main/java/challenges/FundamentalsPractice.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import java.util.*;

import javax.swing.event.SwingPropertyChangeSupport;

public class FundamentalsPractice {

// Example: sum of two integers
Expand All @@ -25,7 +27,31 @@ public static int sum(int a, int b) { // The values in parentheses are set for y
* Ex: Given [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15], you would RETURN: [3, 2, 1]
*/
public static int[] fizzbuzz(int[] arr) {
return null;

int[] fizzBuzzes = {0, 0, 0};

for (int i = 0; i < arr.length; i++) {
if (arr[i] % 3 == 0 && arr[i] % 5 == 0) {
System.out.println("FizzBuzz");
fizzBuzzes[2]++;
}

else if (arr[i] % 3 == 0) {
System.out.println("Fizz");
fizzBuzzes[0]++;
}

else if (arr[i] % 5 == 0) {
System.out.println("Buzz");
fizzBuzzes[1]++;
}

else {
System.out.println(arr[i]);
}
}

return fizzBuzzes;
}

/**
Expand All @@ -39,24 +65,37 @@ public static int[] fizzbuzz(int[] arr) {
* char[] charArray = str.toCharArray(); // charArray is ['h', 'e', 'l', 'l', 'o']
*/
public static String reverseString(String input) {
// Your code goes here
return null;
char[] charArray = input.toCharArray();
char[] reversedChar = new char[charArray.length];

for (int i = charArray.length - 1; i >= 0; i--) {
reversedChar[charArray.length - i - 1] = charArray[i];
}

return new String(reversedChar);
}

/**
* maxInArray - Given an array of integers, return the largest value.
* Ex: [1, 5, 3, 9, 2] -> 9
*/
public static int maxInArray(int[] arr) {
// Your code goes here
return 0;
int biggest = arr[0];

for (int i = 1; i < arr.length; i++) {
if (arr[i] > biggest) {
biggest = arr[i];
}
}
return biggest;
}

public static boolean stringsAreSame(String a, String b) {
// If strings are equal, return true. Else, return false
// Hint: use .equals() to compare strings not ==
// Do not care about capitals HI is equal to hi look up how to do.
return false;

return a.equalsIgnoreCase(b);
}

/**
Expand All @@ -66,8 +105,14 @@ public static boolean stringsAreSame(String a, String b) {
* - strings can be accessed like this: variable.charAt(0) returns the first character
*/
public static int countVowels(String input) {
// Your code goes here
return 0;
int vowels = 0;

for (char i = 0; i < input.length(); i++) {
if (input.charAt(i) == 'a' || input.charAt(i) == 'e' || input.charAt(i) == 'i' || input.charAt(i) == 'o' || input.charAt(i) =='u') {
vowels++;
}
}
return vowels;
}

/**
Expand All @@ -76,8 +121,18 @@ public static int countVowels(String input) {
* Hint: to check if n is prime, divide it by all integers from 2 to sqrt(n).
*/
public static boolean isPrime(int n) {
// Your code goes here
return false;
if (n == 1) {
return false;
}

for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}

return true;

}

/**
Expand All @@ -88,8 +143,12 @@ public static boolean isPrime(int n) {
* THIS ONE IS HARD
*/
public static long factorial(int n) {
// Your code goes here
return 0L;
int previousFactorial = 1;

for (int i = n; i > 0; i--) {
previousFactorial = previousFactorial * i;
}
return previousFactorial;
}


Expand All @@ -114,6 +173,11 @@ public static void main(String[] args) {
int[] nums = {1, 5, 3, 9, 2};
System.out.println("maxInArray([1,5,3,9,2]) = " + maxInArray(nums)); // Expected: 9

// stringsAreSame
String one = "hello";
String two = "HELLO";
System.out.println("stringAreSame = " + stringsAreSame(one, two));

// countVowels
String word = "banana";
System.out.println("countVowels('banana') = " + countVowels(word)); // Expected: 3
Expand Down
65 changes: 64 additions & 1 deletion src/main/java/challenges/PasswordChecker.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package challenges;
import java.util.ArrayList;
import java.util.Scanner;

/*
Expand Down Expand Up @@ -36,6 +37,12 @@ public static void main(String[] args) {
// TODO: Output the strength rating
// System.out.println(strength);

System.out.println("enter your password: ");
String password = scanner.nextLine();

String strength = checkPasswordStrength(password);
System.out.println(strength);

scanner.close();
}

Expand All @@ -52,6 +59,62 @@ public static String checkPasswordStrength(String password) {
// - Check for uppercase, lowercase, digit, and symbol
// - Return the appropriate rating

return ""; // Placeholder
if (password.length() < 8) {
return "Weak";
}

int types = 0;

ArrayList<Character> upperCase = new ArrayList<>();
ArrayList<Character> lowerCase = new ArrayList<>();
ArrayList<Character> digit = new ArrayList<>();
ArrayList<Character> symbol = new ArrayList<>();

for (char c : password.toCharArray()) {
if (Character.isUpperCase(c)) {
upperCase.add(c);
}

else if (Character.isLowerCase(c)) {
lowerCase.add(c);
}

else if (Character.isDigit(c)) {
digit.add(c);
}

else {
symbol.add(c);
}
}

if (upperCase.size() > 0) {
types++;
}

if (lowerCase.size() > 0) {
types++;
}

if (digit.size() > 0) {
types++;
}

if (symbol.size() > 0) {
types++;
}

if (types == 4) {
return "Strong";
}

else if (types == 3) {
return "Moderate";
}

else {
return "Weak";
}

}
}