-
Notifications
You must be signed in to change notification settings - Fork 43
Leaves-Yasmin #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Leaves-Yasmin #24
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first works well. The second happens to pass the tests, but doesn't actually work in general. Take a look at my comments and let me know if you have questions.
- Keys that are close have hash values that are far apart | ||
- Hashing appears to be random (hard to deduce the original object – this is why hashing is also used in cryptography)| | ||
|
||
3. Is there a perfect hash function? If so what is it? |Yes, A hash function for which there are no collisions is called a "perfect hash function"| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Except for any general purpose hash, no hash function can be perfect.
# Time Complexity: o(n) | ||
# Space Complexity: o(n) | ||
|
||
def grouped_anagrams(strings) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Nice
k.times do |element| | ||
output << hash.sort[element][0] | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe hash.sort
sorts elements by the key instead of the value, so this will return the elements sorted by the keys instead of the frequency of their occurrence in the original array.
It's happening to pass the tests, but it's not working in general.
Consider this:
it "will work for [3, 3, 3, 3, 3, 1, 1, 2, 2, 2]" do
list = [3, 3, 3, 3, 3, 1, 1, 2, 2]
k = 2
answer = top_k_frequent_elements(list, k)
expect(answer.sort).must_equal [2, 3]
end
Also even if this did work, you're sorting the hash each iteration of the loop... which is excessive.
# Time Complexity: o(n) | ||
# Space Complexity: o(n) | ||
def top_k_frequent_elements(list, k) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't actually work (see below) and is O(k * n log n) in time complexity as you're sorting the hash k times.
Hash Table Practice
Congratulations! You're submitting your assignment!
Comprehension Questions