Skip to content

Kristina - Maple#60

Open
k0axaca wants to merge 1 commit intoAda-C16:masterfrom
k0axaca:master
Open

Kristina - Maple#60
k0axaca wants to merge 1 commit intoAda-C16:masterfrom
k0axaca:master

Conversation

@k0axaca
Copy link

@k0axaca k0axaca commented Jul 21, 2022

Hash Table Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
Why is a good Hash Function Important? Good Hash Functions minimize collisions.
How can you judge if a hash function is good or not? A hash function is good if it maps different keys to different values, executes in constant time, and appears random.
Is there a perfect hash function? If so what is it? No.
Describe a strategy to handle collisions in a hash table You could utilize linear probing to minimize collisions.
Describe a situation where a hash table wouldn't be as useful as a binary search tree When there are not unique keys available.
What is one thing that is more clear to you on hash tables now Time complexity in relation to hash tables is cleared now, for example O(1) lookup time.

@chimerror
Copy link

Grabbing this to grade!

Copy link

@chimerror chimerror left a comment

Choose a reason for hiding this comment

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

Good work!

I added some corrections to your time complexity calculations, but overall this demonstrates sufficient exercise of the information presented and thus is being marked Green.

anagrams = []

for ele in strings:
if grouped_words.get(create_anagram_key(ele)) == None:

Choose a reason for hiding this comment

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

Just a minor efficiency thing, if you find yourself calling the same function multiple times with the exact same arguments like you do with create_anagram_key(ele) here, it's usually worth saving the result into a local variable, since every call will recalculate the same data that doesn't change.

Each subarray will have strings which are anagrams of each other
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n*m)

Choose a reason for hiding this comment

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

I'm going to assume that n is the number of words in the list and that by m you mean the number of letters in each word. That being said, note that the call to sorted will actually take O(m * log(m)) time, making this more accurately O(n * m * log(m)).

However, we can make a simplifying assumption since we know that the input list is made up of English words. English words tend not to get long, only about 5 letters per word on average, so the effect of m is going to be dwarfed by the effect of n, since there can easily be hundreds or thousands of words in the list. With that assumption, we can just say the time complexity is just O(n).

In the case of a tie it will select the first occuring element.
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n)

Choose a reason for hiding this comment

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

Don't forget to account for the call to sort on line 42, which will take O(n * log(n)) time and thus be the dominant part of the algorithm for the final time complexity.

Copy link

@kyra-patton kyra-patton left a comment

Choose a reason for hiding this comment

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

✨🌟 Nice work, Kristina. I left a couple comments. Let me know what questions you have.

🟢

Comment on lines +16 to +19
if grouped_words.get(create_anagram_key(ele)) == None:
grouped_words[create_anagram_key(ele)] = [ele]
else:
grouped_words[create_anagram_key(ele)].append(ele)

Choose a reason for hiding this comment

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

Instead of calling your helper function three times, consider calling it just once, at the top of the for loop body.

Suggested change
if grouped_words.get(create_anagram_key(ele)) == None:
grouped_words[create_anagram_key(ele)] = [ele]
else:
grouped_words[create_anagram_key(ele)].append(ele)
anagram_key = create_anagram_key(ele)
if grouped_words.get(anagram_key) == None:
grouped_words[anagram_key] = [ele]
else:
grouped_words[anagram_key].append(ele)

Each subarray will have strings which are anagrams of each other
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n*m)

Choose a reason for hiding this comment

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

Yes! Where n is the length of strings and m is the length of the longest word in strings. However, if we assume each word in strings is a valid English word, since those are all of a finite length, we can reduce this to say the solution is O(n)

Comment on lines +30 to +31
Time Complexity: O(n)
Space Complexity: O(n)

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants