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
65 changes: 56 additions & 9 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,75 @@
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^2)

Choose a reason for hiding this comment

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

We want to be careful about reusing the same variable for complexity calculations. It seems like n is being used as both the number of strings in strings and the number of letters in a word. I would suggest using something like m for the number of letters in a word for clarity.

Looking at the overall Big O, and line 12 in particular, calling the sorted function on a list is an O(m*log(m)) operation. If n is the length of the input strings and m is the length of a word, our overall time complexity would be O(n*m*log(m))

Space Complexity: O(n)
"""
pass
if not strings:
return []
anagrams = {}
for string in strings:
sorted_string = "".join(sorted(string))
if sorted_string in anagrams:
anagrams[sorted_string].append(string)
else:
anagrams[sorted_string] = [string]
return list(anagrams.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)

Choose a reason for hiding this comment

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

Since this solution uses sorted on line 33, our overall time complexity would be O(n) to fill counts + sorted's complexity O(n*log(n)). That gives us O(n + n*log(n)), which we'd simplify to O(n*log(n)).

Space Complexity: O(n)
"""
pass
if not nums:
return []
counts = {}
for num in nums:
if num in counts:
counts[num] += 1
else:
counts[num] = 1
sorted_counts = sorted(counts.items(), key=lambda x: x[1], reverse=True)
return [x[0] for x in sorted_counts[:k]]

def valid_row(row):
if not row:
return False
for i in range(1, 10):
if str(i) not in row:
return False
Comment on lines +39 to +41

Choose a reason for hiding this comment

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

The Sudoku problem is optional, so no need to revisit, but something to consider if you were wondering about the test failures: this function will return False if a row does not contain all the numbers 1-9.

A row can be valid without being fully filled in yet, and when valid_row is called from valid_subgrid it looks like the row input is 3 items long and would always return false. We want to be sure that there are no duplicate values in a given row or subgrid. One way to do that could be to create a frequency map of the characters in the row, then check that no character other than . has a count greater than 1.

return True

def valid_subgrid(subgrid):
if not subgrid:
return False
for row in subgrid:
if not valid_row(row):
return False
return True

def valid_sudoku(table):
""" This method will return the true if the table is still
a 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(n^2)
Space Complexity: O(n)
"""
pass

if not table:
return False
for row in table:
if not valid_row(row):
return False
for col in zip(*table):
if not valid_row(col):
return False
for i in range(0, 9, 3):
for j in range(0, 9, 3):
if not valid_subgrid(table[i:i+3], table[j:j+3]):

Choose a reason for hiding this comment

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

It doesn't look like the function declaration for valid_subgrid takes in a second parameter, I'm not sure the second slice here table[j:j+3] gets validated.

return False
return True