From 47af3a05a88e972ea395cbebd1fdcd05f6110cc3 Mon Sep 17 00:00:00 2001 From: Laurel S Date: Tue, 19 Jul 2022 20:48:06 -0700 Subject: [PATCH 1/2] Passes exercises 1 and 2 --- hash_practice/exercises.py | 39 ++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index 48bf95e..163d3cc 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -1,19 +1,50 @@ +from ntpath import join +from sqlalchemy import true + + 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: ? """ - pass + word_dict = {} + for word in strings: + sorted_word = ''.join(sorted(word)) + if sorted_word in word_dict: + word_dict[sorted_word].append(word) + print(f"{word} found in dict") + else: + word_dict[sorted_word] = [word] + result = [] + for k, v in word_dict.items(): + result.append(v) + return result + +words = ["eat", "tea", "tan", "ate", "nat", "bat"] + +grouped_anagrams(words) 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) (This is the result of using the list sort method) + Space Complexity: O(n) """ - pass + if not nums or k < 1: + return [] + freq_map = {} + for num in nums: + freq_map[num] = 1 + freq_map.get(num, 0) + freq_list = [(num, freq) for num, freq in freq_map.items()] + freq_list.sort(key = lambda x: x[1], reverse=True) + top_el = [freq_list[i][0] for i in range(k)] + return top_el + +numbers = [1, 1, 1, 2, 2, 3] +k = 2 +# top_k_frequent_elements(numbers, k) def valid_sudoku(table): From af1acd53922932a78a667af3900a4ba05fa80943 Mon Sep 17 00:00:00 2001 From: Laurel S Date: Tue, 19 Jul 2022 20:57:48 -0700 Subject: [PATCH 2/2] Adds time complexity analysis --- hash_practice/exercises.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index 163d3cc..5826773 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -6,30 +6,27 @@ 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 * m log m) + ^ Not sure on the time complexity. Here, n represents the number of words in the + input string, while m represents the length of an individual word. + Space Complexity: O(n) """ word_dict = {} for word in strings: sorted_word = ''.join(sorted(word)) if sorted_word in word_dict: word_dict[sorted_word].append(word) - print(f"{word} found in dict") else: word_dict[sorted_word] = [word] - result = [] - for k, v in word_dict.items(): - result.append(v) + result = [v for k,v in word_dict.items()] return result -words = ["eat", "tea", "tan", "ate", "nat", "bat"] - -grouped_anagrams(words) 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: O(n log n) (This is the result of using the list sort method) + Time Complexity: O(n log n) (This is the result of using the list sort method and + n is the number of elements in nums) Space Complexity: O(n) """ if not nums or k < 1: @@ -42,10 +39,6 @@ def top_k_frequent_elements(nums, k): top_el = [freq_list[i][0] for i in range(k)] return top_el -numbers = [1, 1, 1, 2, 2, 3] -k = 2 -# top_k_frequent_elements(numbers, k) - def valid_sudoku(table): """ This method will return the true if the table is still