Skip to content
Open
Show file tree
Hide file tree
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
46 changes: 39 additions & 7 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,52 @@

def grouped_anagrams(strings):
def grouped_anagrams(d1):
""" This method will return an array of arrays.
Each subarray will have strings which are anagrams of each other
Time Complexity: ?
Space Complexity: ?
"""
Comment on lines +2 to 7

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍 Space/time complexity?

pass
# empty dictionary for anagrams together
dict = {}

# traversal
for val in d1:
# sorts list
key = ''.join(sorted(val))

if key in dict.keys():
dict[key].append(val)
else:
dict[key] = []
dict[key].append(val)

# traverse dictionary and join keys together
result = []
for key,value in dict.items():
# result = result + ' '.join(value) + ' '
result.append(value)
return result

def top_k_frequent_elements(nums, k):
def top_k_frequent_elements(numbers, 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 +29 to 34

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍 However because you're finding max of a list of n elements k times, this is O(nk) for time complexity.

pass

freq_map = {}
if numbers == []:
return numbers
for i in numbers:
if i in freq_map:
freq_map[i] += 1
elif i not in freq_map:
freq_map[i] = 1

new_list = []
for i in range(k):
highest_value = max(freq_map, key=freq_map.get)
new_list.append(highest_value)
freq_map.pop(highest_value)
return new_list

def valid_sudoku(table):
""" This method will return the true if the table is still
Expand Down
2 changes: 1 addition & 1 deletion tests/test_top_k_frequent_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_it_works_with_example_1():
k = 2

# Act
answer = top_k_frequent_elements(nums=numbers, k=k)
answer = top_k_frequent_elements(numbers=numbers, k=k)

# Assert
answer.sort()
Expand Down