From c1d419ea1f2368b248def5ff1e0ebae3ae2b9e52 Mon Sep 17 00:00:00 2001 From: Ana Gabriele Date: Sun, 3 Jul 2022 13:56:05 -0700 Subject: [PATCH 1/3] implement grouped_anagrams --- hash_practice/exercises.py | 83 +++++++++++++++++++++++++++++++--- tests/test_grouped_anagrams.py | 2 +- 2 files changed, 77 insertions(+), 8 deletions(-) diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index 48bf95e..ecc9ef7 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -1,23 +1,91 @@ +from attr import has + + def grouped_anagrams(strings): - """ This method will return an array of arrays. + """ + - Use a hash table to solve a coding problem + - Identify how a hash table can produce a more attractive runtime over alternative solutions + Given an array of strings, group anagrams together. + ### Example + ``` + Input: ["eat", "tea", "tan", "ate", "nat", "bat"], + Output: + [ + ["ate","eat","tea"], + ["nat","tan"], + ["bat"] + ] + ``` + Note: + - All inputs will be in lowercase. + - The order of your output does not matter + + 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(m * nlogn) - | python sorting algorithm -> O(n. logn). + Space Complexity: O(n) """ - pass + # return empty list if no words + if len(strings) == 0: + return [] + + hash_table = {} + for item in strings: + # sort time complexity => O(n. logn). + string = "".join(sorted(item)) + if string not in hash_table: + hash_table[string] = [item] + else: + hash_table[string].append(item) + + print(hash_table.values()) + return list(hash_table.values()) + + +print(grouped_anagrams(["eat", "tae", "tea", "eta", "aet", "ate"])) def top_k_frequent_elements(nums, k): - """ This method will return the k most common elements + """ + ## Most Frequent K Elements + Given a non-empty array of integers, return the *k* most frequent elements. + ## Example 1 + ``` + Input: nums = [1,1,1,2,2,3], k = 2 + Output: [1,2] + ``` + ## Example 2 + ``` + Input: nums = [1], k = 1 + Output: [1] + ``` + ## Note + + You may assume k is always valid, 1 ≤ k ≤ number of unique elements. + + You should be able to equal or beat O(n log n), where n is the array's size. + 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: ? """ - pass + + hash_table = {} + + for item in nums: + if item not in hash_table: + hash_table[item] = 1 + else: + hash_table[item] +=1 + def valid_sudoku(table): - """ This method will return the true if the table is still + """ + + + This method will return the true if the table is still a valid sudoku table. Each element can either be a ".", or a digit 1-9 The same digit cannot appear twice or more in the same @@ -25,5 +93,6 @@ def valid_sudoku(table): Time Complexity: ? Space Complexity: ? """ + # Optional QUESTION - pass diff --git a/tests/test_grouped_anagrams.py b/tests/test_grouped_anagrams.py index 073e323..291a8cb 100644 --- a/tests/test_grouped_anagrams.py +++ b/tests/test_grouped_anagrams.py @@ -18,7 +18,7 @@ def test_will_return_correct_result_for_readme_example(): ["ate","eat","tea"], ["nat","tan"], ["bat"] - ] + ] # Assert assert len(answer) == 3 From 7524f968497e0d005c8de24358bc12e61a4f0947 Mon Sep 17 00:00:00 2001 From: Ana Gabriele Date: Sun, 3 Jul 2022 14:13:49 -0700 Subject: [PATCH 2/3] implement top_k_freqyent_elements --- hash_practice/exercises.py | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index ecc9ef7..8ea7945 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -67,19 +67,36 @@ def top_k_frequent_elements(nums, k): You should be able to equal or beat O(n log n), where n is the array's size. 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) """ + # if empty list return an empty array + + if len(nums) == 0: + return [] + # count the frequency of each element hash_table = {} + frequency = [[] for i in range(len(nums) +1)] for item in nums: - if item not in hash_table: - hash_table[item] = 1 - else: - hash_table[item] +=1 - - + # coutn how many times an item occurs , return 0 if doesn't exists + hash_table[item] = 1 + hash_table.get(item,0) + # loop thorugh key-value pair + for item, count in hash_table.items(): + # append to the the freq list + frequency[count].append(item) + + result = [] + + # loop though all the way up until zero + for index in range(len(frequency) -1, 0, -1): + for elem in frequency[index]: + result.append(elem) + if len(result) == k: + return result + +print(top_k_frequent_elements([9,9,8,8,7],2)) def valid_sudoku(table): """ From f3c3c0321c49a16481c9fa9f094e00c03cb31276 Mon Sep 17 00:00:00 2001 From: Ana Gabriele Date: Sun, 3 Jul 2022 14:26:31 -0700 Subject: [PATCH 3/3] add comment --- hash_practice/exercises.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index 8ea7945..d293f1e 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -90,6 +90,7 @@ def top_k_frequent_elements(nums, k): result = [] # loop though all the way up until zero + # range(start, stop, step) for index in range(len(frequency) -1, 0, -1): for elem in frequency[index]: result.append(elem) @@ -100,8 +101,7 @@ def top_k_frequent_elements(nums, k): def valid_sudoku(table): """ - - + OPTIONAL This method will return the true if the table is still a valid sudoku table. Each element can either be a ".", or a digit 1-9