-
Notifications
You must be signed in to change notification settings - Fork 76
Roza-cedar #62
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?
Roza-cedar #62
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 |
|---|---|---|
|
|
@@ -5,17 +5,56 @@ def grouped_anagrams(strings): | |
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| """ | ||
| pass | ||
| if len(strings) == 0: | ||
| return [] | ||
| if len(strings) == 1: | ||
| return [[strings[0]]] | ||
| list1 = [strings[0]] | ||
| listOfLists = [list1] | ||
|
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. These are rather generic names that more describe what the data type is rather than what it is for. You don't even need |
||
| for word in strings[1:]: | ||
| flag = False | ||
|
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. Likewise, this could be better named something like |
||
| for lists in listOfLists: | ||
| # check in the existing inner lists | ||
| if anagram_helper(word) == anagram_helper(lists[0]): | ||
|
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. This solution redoes a great deal of work as it recalculates the frequency map through Additionally going back over every list makes this algorithm balloon to O(n^2) in time complexity since in the worst case, every word is in its own list and there are none that share anagrams. I think you can achieve a much better solution that is O(n) through using a dictionary, because with this solution you don't gain advantage of the O(1) lookup time of dictionaries. |
||
| lists.append(word) | ||
| flag = True | ||
| break | ||
| # if not in the existing, create a new group | ||
| if not flag: | ||
| listOfLists.append([word]) | ||
| return listOfLists | ||
|
|
||
| def anagram_helper(word2): | ||
| freq_dict = {} | ||
| for letter in word2: | ||
| if letter not in freq_dict: | ||
| freq_dict[letter] = 1 | ||
| else: | ||
| freq_dict[letter] += 1 | ||
| return freq_dict | ||
|
|
||
|
|
||
| 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 log n)for sorted, O(n) to build freq dic, adding the two, the worst case will be O(n log n) | ||
| Space Complexity: O(n) | ||
| """ | ||
| pass | ||
|
|
||
|
|
||
| if len(nums) == 0: | ||
| return [] | ||
| freq_dict = {} | ||
| for elem in nums: | ||
| if elem in freq_dict: | ||
| freq_dict[elem] += 1 | ||
| else: | ||
| freq_dict[elem] = 1 | ||
| # sorted returns list of tuples (key, value) | ||
| sorted_dict = sorted(freq_dict.items(), key=lambda item: item[1], reverse=True) | ||
| return_list = [] | ||
| for index in range(k): | ||
| return_list.append(sorted_dict[index][0]) | ||
| return return_list | ||
|
|
||
| def valid_sudoku(table): | ||
| """ This method will return the true if the table is still | ||
| a valid sudoku table. | ||
|
|
@@ -25,5 +64,119 @@ def valid_sudoku(table): | |
| 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. Likewise, complexity calculations are missing here. |
||
| """ | ||
| pass | ||
|
|
||
| #for horizontal | ||
| seen_dict = {} | ||
| for row in table: | ||
| for elem in row: | ||
| if elem == ".": | ||
| continue | ||
| if elem in seen_dict: | ||
| return False | ||
| else: | ||
| seen_dict[elem] = 1 | ||
| seen_dict = {} | ||
|
|
||
| # for vertical | ||
| seen_dict = {} | ||
| for col in range(len(table[0])): | ||
| for row in range(len(table)): | ||
| if table[row][col] == ".": | ||
| continue | ||
| if table[row][col] in seen_dict: | ||
| return False | ||
| else: | ||
| seen_dict[table[row][col]] = 1 | ||
| seen_dict = {} | ||
|
|
||
| # # for 3x3 grid | ||
| seen_dict = {} | ||
| for i in range(3): | ||
| for j in range(3): | ||
| if table[i][j] == ".": | ||
| continue | ||
| if table[i][j] in seen_dict: | ||
| return False | ||
| else: | ||
| seen_dict[table[i][j]] = 1 | ||
|
|
||
| seen_dict = {} | ||
| for i in range(3): | ||
| for j in range(3, 6): | ||
| if table[i][j] == ".": | ||
| continue | ||
| if table[i][j] in seen_dict: | ||
| return False | ||
| else: | ||
| seen_dict[table[i][j]] = 1 | ||
|
|
||
| seen_dict = {} | ||
| for i in range(3): | ||
| for j in range(6,9): | ||
| if table[i][j] == ".": | ||
| continue | ||
| if table[i][j] in seen_dict: | ||
| return False | ||
| else: | ||
| seen_dict[table[i][j]] = 1 | ||
|
|
||
| seen_dict = {} | ||
| for i in range(3,6): | ||
| for j in range(3): | ||
| if table[i][j] == ".": | ||
| continue | ||
| if table[i][j] in seen_dict: | ||
| return False | ||
| else: | ||
| seen_dict[table[i][j]] = 1 | ||
|
|
||
| seen_dict = {} | ||
| for i in range(3,6): | ||
| for j in range(3, 6): | ||
| if table[i][j] == ".": | ||
| continue | ||
| if table[i][j] in seen_dict: | ||
| return False | ||
| else: | ||
| seen_dict[table[i][j]] = 1 | ||
|
|
||
| seen_dict = {} | ||
| for i in range(3,6): | ||
| for j in range(6, 9): | ||
| if table[i][j] == ".": | ||
| continue | ||
| if table[i][j] in seen_dict: | ||
| return False | ||
| else: | ||
| seen_dict[table[i][j]] = 1 | ||
|
|
||
| seen_dict = {} | ||
| for i in range(6,9): | ||
| for j in range(3): | ||
| if table[i][j] == ".": | ||
| continue | ||
| if table[i][j] in seen_dict: | ||
| return False | ||
| else: | ||
| seen_dict[table[i][j]] = 1 | ||
|
|
||
| seen_dict = {} | ||
| for i in range(6,9): | ||
| for j in range(3, 6): | ||
| if table[i][j] == ".": | ||
| continue | ||
| if table[i][j] in seen_dict: | ||
| return False | ||
| else: | ||
| seen_dict[table[i][j]] = 1 | ||
|
|
||
| seen_dict = {} | ||
| for i in range(6,9): | ||
| for j in range(6, 9): | ||
| if table[i][j] == ".": | ||
| continue | ||
| if table[i][j] in seen_dict: | ||
| return False | ||
| else: | ||
| seen_dict[table[i][j]] = 1 | ||
|
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. This works, but usually when you repeat almost the same code so many times, it's usually worth thinking about how you can cut down on duplication through perhaps defining a helper function. Code repetition like this tends to lead to errors and higher maintenance as the different versions of almost the same code fall out of sync with each other. |
||
| return True | ||
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.
Calling out the missing complexity calculations here, but I haven't been dinging people for it.