Skip to content
Open
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
12 changes: 6 additions & 6 deletions .github/PULL_REQUEST_TEMPLATE
Original file line number Diff line number Diff line change
Expand Up @@ -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. |

63 changes: 55 additions & 8 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
@@ -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)
Comment on lines +4 to 7

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're sorting the words and English words are small this method actually depends on the number of words, not the length of them.

So it's O(n * m log m) where m is the length of the words, or if you assume the words are short O(n)

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a bit more compact.

Suggested change
if hash.key?(key)
if hash[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)
Comment on lines +38 to 40

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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
Expand Down