Skip to content
Open
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
50 changes: 41 additions & 9 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,36 @@
def grouped_anagrams(strings):
""" 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)
Space Complexity: O(1)

Choose a reason for hiding this comment

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

As we possibly add an entry to anagram_dict for each word in strings (in the case no words are anagrams of each other), the space complexity here is O(n).

"""
pass
anagram_dict = {}

for string in strings:
word_sorted = "".join(sorted(string))
if word_sorted not in anagram_dict:
anagram_dict[word_sorted] = [string]
else:
anagram_dict[word_sorted] = [string]

Choose a reason for hiding this comment

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

Oof, this causes a pretty bad bug that means only the last string of each anagram group is returned, rather than all the strings in that group. I think you meant to append to the list here rather than replacing the value.

However, the solution is otherwise correct, so I'm only going to mark this Yellow. I will upgrade to Green upon this bug being fixed.


return list(anagram_dict.values())

def top_k_frequent_elements(nums, k):
""" 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: ?
Time Complexity: O(n)
Space Complexity: O(1)

Choose a reason for hiding this comment

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

Likewise, in this case we add an entry to most_common for each number in nums so the space complexity here is O(n).

"""
pass
most_common = {}
for num in nums:
if num in most_common:

most_common[num] += 1
else:
most_common[num] = 1
most_common = sorted(most_common.items(), key=lambda x: x[1], reverse=True)
return [x[0] for x in most_common[:k]]



def valid_sudoku(table):
Expand All @@ -22,8 +40,22 @@ def valid_sudoku(table):
Each element can either be a ".", or a digit 1-9
The same digit cannot appear twice or more in the same
row, column or 3x3 subgrid
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n2)

Choose a reason for hiding this comment

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

This would be correct (even given your current buggy implementation) if we were taking in arbitrary nxn grids, but as we are only taking in 3x3 grids, the run time is constant and thus O(1).

Space Complexity: O(1)
"""
pass
for row in table:
for element in row:
if element != ".":
if element not in "123456789":
return False
else:
if row.count(element) >1:
return False
if [row[i] for i in range(9)].count(element)>1:

Choose a reason for hiding this comment

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

I know this problem is optional but I just want to note that the list comprehension [row[i] for i in range(9)] will not actually return the columns, but just a copy of row again (since all rows are 9 elements long).

To get the columns, you'll have to iterate over table not row.

return False
if [table[i][j] for i in range(3) for j in range(3)].count(element) >1:

Choose a reason for hiding this comment

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

Likewise, this will always return the first subgrid ((0, 0) to (2, 2)) every iteration, so to fully solve this, you will likely need to more explicitly iterate through all the subgrids rather than using a list comprehension.

return False
return True