From 7a1acb511471604a5c27424e87da4fa49ab55daa Mon Sep 17 00:00:00 2001 From: Sabrina Lauredan Date: Thu, 21 Jul 2022 20:56:49 -0400 Subject: [PATCH] Required tests passing. --- hash_practice/exercises.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index 48bf95e..e9c8416 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -1,11 +1,14 @@ 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 + anagrams = {} + for word in strings: + a = tuple(sorted(word)) + if a in anagrams: + anagrams[a].append(word) + else: + anagrams[a] = [word] + return list(anagrams.values()) + def top_k_frequent_elements(nums, k): """ This method will return the k most common elements @@ -13,9 +16,16 @@ def top_k_frequent_elements(nums, k): Time Complexity: ? Space Complexity: ? """ - pass - + frequency = {} + for num in nums: + if num in frequency: + frequency[num] += 1 + else: + frequency[num] = 1 + sorted_frequency = sorted(frequency.keys(), key = frequency.get, reverse = True) + return sorted_frequency[:k] +# optional def valid_sudoku(table): """ This method will return the true if the table is still a valid sudoku table.