From 085b9e96dd52f07244f89916de90ec2aa49fce12 Mon Sep 17 00:00:00 2001 From: rjfredr Date: Thu, 6 Feb 2025 12:01:45 -0800 Subject: [PATCH 1/3] rj sides finished --- src/Main.java | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Main.java b/src/Main.java index 036c766..8a0f9f6 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,7 +6,7 @@ public class Main { // The time complexity is: - // YOUR ANSWER HERE + // O(n^2) where n = x*x public static void timesTable(int x) { for(int i = 1; i <= x; i++) { for(int j = 1; j <= x; j++) { @@ -27,7 +27,7 @@ public static void printLetters(String word) { } // The time complexity is: - // YOUR ANSWER HERE + // O(n^2) where n= bannedPasswords.length()*password.equals(bannedPassword) public static boolean isBanned(String password) { String[] bannedPasswords = {"password", "hello", "qwerty"}; boolean banned = false; @@ -51,7 +51,7 @@ public static int computeProduct(int[] nums) { } // The time complexity is: - // YOUR ANSWER HERE + // O(n) where n=O(1*n*1) public static void describeProduct(int[] nums) { System.out.println("About to compute the product of the array..."); int product = computeProduct(nums); @@ -71,6 +71,7 @@ public static int computeFactorial(int n) { // Assume that the largest number is no bigger than the length // of the array + // O(n^2) where n=computerFactorial(num)*num public static void computeAllFactorials(int[] nums) { for(int num : nums) { int result = computeFactorial(num); @@ -94,7 +95,7 @@ public static void checkIfContainedArrayList(ArrayList arr, String targe // assume n = wordsA.length = wordsB.length // assume that each String is bounded by a constant length // The time complexity is: - // YOUR ANSWER HERE + // O(n^3) where n= wordsA.length*wordsB.length*wordA.equals() public static boolean containsOverlap(String[] wordsA, String[] wordsB) { for(String wordA : wordsA) { for(String wordB : wordsB) { @@ -125,7 +126,7 @@ public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { } // The time complexity is: - // YOUR ANSWER HERE + // O(n) where n=chars.length public static void printCharacters(char[] chars) { for (int i = 0; i < chars.length; i++) { char character = chars[i]; @@ -140,7 +141,7 @@ public static double computeAverage(double a, double b) { // assume that each String is bounded by a constant length // The time complexity is: - // YOUR ANSWER HERE + // O(1) where 1=set.contains public static void checkIfContainedHashSet(HashSet set, String target) { if (set.contains(target)) { @@ -172,9 +173,14 @@ public static String emailLookup(String[] names, String[] emails, String queryNa // Write this method to efficiently return the corresponding email or "Person not found" if appropriate // assume that each String is bounded by a constant length // What is the time complexity of your solution? - // YOUR ANSWER HERE + // O(1) where 1=namestoemails.contains() public static String emailLookupEfficient(HashMap namesToEmails, String queryName) { - return null; + if (namesToEmails.containsKey(queryName)) { + return namesToEmails.get(queryName); + } + else { + return "Person not found"; + } } // What is the time complexity of this method? @@ -195,6 +201,11 @@ public static boolean hasCommon(HashSet wordSet, ArrayList wordL // What is the time complexity of your new solution? // YOUR ANSWER HERE public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordList) { + for (String word : wordList) { + if (wordSet.contains(word)) { + return true; + } + } return false; } From c8166aa4d0515b98f34d00d1c4b9b321d2252eed Mon Sep 17 00:00:00 2001 From: Abdi <89213120+abdirashidexe@users.noreply.github.com> Date: Thu, 6 Feb 2025 12:02:07 -0800 Subject: [PATCH 2/3] abdi's portion --- src/Main.java | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Main.java b/src/Main.java index 036c766..888d8fa 100644 --- a/src/Main.java +++ b/src/Main.java @@ -17,7 +17,7 @@ public static void timesTable(int x) { } // The time complexity is: - // YOUR ANSWER HERE + // O(n) where n is length of letters public static void printLetters(String word) { char[] letters = word.toCharArray(); @@ -41,7 +41,7 @@ public static boolean isBanned(String password) { // The time complexity is: - // YOUR ANSWER HERE + // O(n) where n is length of nums.size() public static int computeProduct(int[] nums) { int total = 1; for(int num : nums) { @@ -60,7 +60,7 @@ public static void describeProduct(int[] nums) { // The time complexity is: - // YOUR ANSWER HERE + // O(n) where n is n public static int computeFactorial(int n) { int result = 1; for(int i = 1; i <= n; i++) { @@ -81,7 +81,7 @@ public static void computeAllFactorials(int[] nums) { // assume that each String is bounded by a constant length // The time complexity is: - // YOUR ANSWER HERE + // O(n) where n is arr.contains public static void checkIfContainedArrayList(ArrayList arr, String target) { if (arr.contains(target)) { System.out.println(target + " is present in the list"); @@ -108,7 +108,7 @@ public static boolean containsOverlap(String[] wordsA, String[] wordsB) { // assume that each String is bounded by a constant length // The time complexity is: - // YOUR ANSWER HERE + // O(n) where n is wordsA.length + wordsB.length O(2n) --> O(n) public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { Set wordsSet = new HashSet<>(); for(String word : wordsA) { @@ -133,7 +133,7 @@ public static void printCharacters(char[] chars) { } } // The time complexity is: - // YOUR ANSWER HERE + // O(1) public static double computeAverage(double a, double b) { return (a + b) / 2.0; } @@ -156,7 +156,7 @@ public static void checkIfContainedHashSet(HashSet set, String target) // Otherwise, it returns "Person not found" // assume that each String is bounded by a constant length // What is the time complexity of this method? - // YOUR ANSWER HERE + // O(n^2) where n is names[i].equals(queryName) * names.length public static String emailLookup(String[] names, String[] emails, String queryName) { for(int i = 0; i < names.length; i++) { if (names[i].equals(queryName)) { @@ -180,7 +180,7 @@ public static String emailLookupEfficient(HashMap namesToEmails, // What is the time complexity of this method? // assume that each String is bounded by a constant length // (assume the set and list have the same number of elements) - // YOUR ANSWER HERE + // O(n^2) where n is wordSet.length * wordList.contains(word) public static boolean hasCommon(HashSet wordSet, ArrayList wordList) { for(String word : wordSet) { if(wordList.contains(word)) { @@ -195,6 +195,7 @@ public static boolean hasCommon(HashSet wordSet, ArrayList wordL // What is the time complexity of your new solution? // YOUR ANSWER HERE public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordList) { + return false; } @@ -203,7 +204,8 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordSet, ArrayList Date: Thu, 6 Feb 2025 12:04:06 -0800 Subject: [PATCH 3/3] changes --- src/Main.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Main.java b/src/Main.java index 8a0f9f6..267bccf 100644 --- a/src/Main.java +++ b/src/Main.java @@ -199,7 +199,7 @@ public static boolean hasCommon(HashSet wordSet, ArrayList wordL // Do not change the datatype of wordSet or wordList. // assume that each String is bounded by a constant length // What is the time complexity of your new solution? - // YOUR ANSWER HERE + // O(n) where n=1(wordSet.contains)*for(String word: wordList) public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordList) { for (String word : wordList) { if (wordSet.contains(word)) { @@ -221,7 +221,7 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList