Skip to content
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

Rock - Priscille #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
84 changes: 75 additions & 9 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,40 @@
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(n)
"""
Comment on lines 2 to 7

Choose a reason for hiding this comment

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

👍 Just note, if the words are limited in length you have the time/space complexity right. If not then the time complexity is O(n * m log m) where m is the length of a word.

pass
anagrams_map = {}
for word in strings:
sorted_word = "".join(sorted(word))
if sorted_word not in anagrams_map:
anagrams_map[sorted_word] = [word]
else:
anagrams_map[sorted_word].append(word)
return list(anagrams_map.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(n)
"""
Comment on lines 18 to 23

Choose a reason for hiding this comment

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

👍 Good use of the max function!

Given that I would say that this is O(nk) given the loop and max function.

pass
frequency_hash = {}
most_frequent_items = []
if len(nums) == 0:
return []
for item in nums:
if item in frequency_hash:
frequency_hash[item] += 1
else:
frequency_hash[item] = 1

for i in range(k):
highest_frequency = max(frequency_hash, key = lambda num: frequency_hash[num])
most_frequent_items.append(highest_frequency)
frequency_hash.pop(highest_frequency)
return most_frequent_items


def valid_sudoku(table):

Choose a reason for hiding this comment

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

👍 Nice work, just a note since the board is always 9x9 and never grows, the time/space complexity can be considered O(1).

Expand All @@ -22,8 +44,52 @@ 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(n^2)
Space Complexity: O(n^2)
"""
pass

#row
for row in range(9):
row_map = {}
for column in range(9):
tile = table[row][column]
if tile == ".":
continue
elif tile not in row_map:
row_map[tile] = 1
else:
return False

#column
for column in range(9):
column_map = {}
for row in range(9):
tile = table[row][column]
if tile == ".":
continue
elif tile not in column_map:
column_map[tile] = 1
else:
return False

#sub-boxes
def squares(R, C):
squares_map = {}
for row in range(R, R+3):
for column in range(C, C+3):
tile= table[row][column]
if tile == ".":
continue
elif tile not in squares_map:
squares_map[tile] = 1
else:
return False
return True
for row in range(0,9,3):
for column in range(0,9,3):
if not squares(row, column):
return False
return True