-
Notifications
You must be signed in to change notification settings - Fork 76
Rebeca Muniz #Cedar #CS Fun #66
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
|
||
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: ? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
@@ -26,4 +55,4 @@ def valid_sudoku(table): | |
Space Complexity: ? | ||
""" | ||
pass | ||
print(top_k_frequent_elements([1, 2, 2, 2, 3, 3, 3], 2)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
There was a problem hiding this comment.
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