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
39 changes: 34 additions & 5 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@

from array import array
from distutils.log import error
import string

Choose a reason for hiding this comment

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

It looks like these import statements didn't get used (they were probably added by an overzealous IDE), so you can remove them



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)
"""
pass
anagram_dict = {}
for sorted_word in strings:
a = tuple(sorted(sorted_word))
if a in anagram_dict:
anagram_dict[a].append(sorted_word)
else:
anagram_dict[a] = [sorted_word]
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: ?

Choose a reason for hiding this comment

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

Looks like the complexity calculations got missed here, but I haven't been dinging people for it.

"""
pass
if len(nums) == 0:
return []

count = {}
freq = [[] for i in range(len(nums) + 1)]

for n in nums:
count[n] = 1 + count.get(n, 0)

for n, c in count.items():
freq[c].append(n)

res = []
for i in range(len(freq) - 1, 0, -1):
for n in freq[i]:
res.append(n)
if len(res) == k:
return res


def valid_sudoku(table):
Expand All @@ -26,4 +55,4 @@ def valid_sudoku(table):
Space Complexity: ?
"""
pass
print(top_k_frequent_elements([1, 2, 2, 2, 3, 3, 3], 2))

Choose a reason for hiding this comment

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

Make sure to remove testing code like this before checking in, just to keep the code clean for style purposes.