diff --git a/src/main/java/challenges/FundamentalsPractice.java b/src/main/java/challenges/FundamentalsPractice.java index 64e2aca..f486e32 100644 --- a/src/main/java/challenges/FundamentalsPractice.java +++ b/src/main/java/challenges/FundamentalsPractice.java @@ -7,6 +7,8 @@ import java.util.*; +import javax.swing.event.SwingPropertyChangeSupport; + public class FundamentalsPractice { // Example: sum of two integers @@ -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; } /** @@ -39,8 +65,14 @@ 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); } /** @@ -48,15 +80,22 @@ public static String reverseString(String input) { * 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); } /** @@ -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; } /** @@ -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; + } /** @@ -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; } @@ -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 diff --git a/src/main/java/challenges/PasswordChecker.java b/src/main/java/challenges/PasswordChecker.java index c42aafb..4b78d45 100644 --- a/src/main/java/challenges/PasswordChecker.java +++ b/src/main/java/challenges/PasswordChecker.java @@ -1,4 +1,5 @@ package challenges; +import java.util.ArrayList; import java.util.Scanner; /* @@ -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(); } @@ -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 upperCase = new ArrayList<>(); + ArrayList lowerCase = new ArrayList<>(); + ArrayList digit = new ArrayList<>(); + ArrayList 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"; + } + } } \ No newline at end of file