diff --git a/.github/PULL_REQUEST_TEMPLATE b/.github/PULL_REQUEST_TEMPLATE index 12a5cea..3e6414e 100644 --- a/.github/PULL_REQUEST_TEMPLATE +++ b/.github/PULL_REQUEST_TEMPLATE @@ -6,10 +6,10 @@ Congratulations! You're submitting your assignment! Question | Answer :------------- | :------------- -Why is a good Hash Function Important? | -How can you judge if a hash function is good or not? | -Is there a perfect hash function? If so what is it? | -Describe a strategy to handle collisions in a hash table | -Describe a situation where a hash table wouldn't be as useful as a binary search tree | -What is one thing that is more clear to you on hash tables now | +Why is a good Hash Function Important? | To speed up look up times, critical when dealing with lots of data. | +How can you judge if a hash function is good or not? | Good hash fx are easy to compute and avoid collisions. Bad hash fx product results with multiple values assigned to same key. | +Is there a perfect hash function? If so what is it? | Nothing is perfect in life. But, if a hash could be perfect, it would be one that naps the set of actual key values to the table without any collisions. | +Describe a strategy to handle collisions in a hash table | You can do separate chaining == make each cell of a hash table point to a linked list of records that have the same hash function value. Or, you can do open addressing which is trying to find the next open slot or address in the hash table. | +Describe a situation where a hash table wouldn't be as useful as a binary search tree | When you need to iterate over all values in a list, finding the largest or smallest key. | +What is one thing that is more clear to you on hash tables now | The hash function itself, you actually need a function to create the table. It's not the same as a hash. | diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..00ba3bc 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,20 +1,67 @@ # This method will return an array of arrays. # Each subarray will have strings which are anagrams of each other -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n log n) +# Space Complexity: O(n) def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" -end +# create an empty hash to store your word + hash = {} + + #loop through strings + strings.each do |word| + key = word.chars.sort.join + #check if key is in the hash, push the word into the hash + #else, set the key as the word + if hash.key?(key) + hash[key] << word + else + hash[key] = [word] + end + end + #create a new result array + #loop through hash to get the keys + result = [] + hash.each do | key, value | + result << hash[key] + end + + #return the array + return result + end #end function + # This method will return the k most common elements # in the case of a tie it will select the first occuring element. -# Time Complexity: ? -# Space Complexity: ? +# list = [1,1,1,1,2] +#outcome = 1 +# Time Complexity: O(n log n) +# Space Complexity: O(n) def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" -end + #check if the length of the list is zero, return empty array + if list.length == 0 + return [] + end + #create new hash here + hash = Hash.new(0) + #loop through array + list.each do |num| + hash[num] += 1 + end + #got help with this result line via Martha + #you want to sort the hash by number value and frequency + #descending order + result = hash.sort_by{|num, frequency| -frequency} + #create an empty array to hold your final result + final_result = [] + #based on the number of common elements you want, that's how many times you'll loop thorugh + #push the result into the final result + #return the final result + k.times do |i| + final_result << result[i][0] + end + return final_result +end #end function # This method will return the true if the table is still