From cc30a83036e2f973ea59eca85f4c343fb8add70c Mon Sep 17 00:00:00 2001 From: Stephanie Date: Sun, 27 Sep 2020 12:19:46 -0700 Subject: [PATCH 1/3] all test not passing --- lib/exercises.rb | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..0858bbf 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,11 +1,21 @@ # 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) n being the quantity of strings +# Space Complexity: O(1) bc we are only creating one new hash def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + letter_hash = {} + + strings.each do |word| + if letter_hash[word.split("").sort] == nil + letter_hash[word.split("").sort] = [word] + else + letter_hash[word.split("").sort] << word + end + end + + return letter_hash.values end # This method will return the k most common elements @@ -13,7 +23,25 @@ def grouped_anagrams(strings) # Time Complexity: ? # Space Complexity: ? def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" + return [] if list == [] + hash = {} + + list.each do |num| + if hash[num] + hash[num] += 1 + else + hash[num] = 1 + end + end + + sorted = hash.sort_by{|num, frequency| -frequency} + + result = [] + k.times do |key, value| + result << sorted[key][0] + end + + return result end From 81b1c528339f1079e35b32db9010108b7519dfd5 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Sun, 27 Sep 2020 12:21:42 -0700 Subject: [PATCH 2/3] added bigO --- lib/exercises.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 0858bbf..72f3ae0 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -20,8 +20,8 @@ def grouped_anagrams(strings) # 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) +# Space Complexity: O(n) def top_k_frequent_elements(list, k) return [] if list == [] hash = {} From 85fea5a53615a9d72a2f90e086852ab8c302b56b Mon Sep 17 00:00:00 2001 From: Stephanie Date: Sun, 27 Sep 2020 12:23:26 -0700 Subject: [PATCH 3/3] added comment --- lib/exercises.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/exercises.rb b/lib/exercises.rb index 72f3ae0..98a14fc 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -22,6 +22,7 @@ def grouped_anagrams(strings) # in the case of a tie it will select the first occuring element. # Time Complexity: O(n log n) # Space Complexity: O(n) +# not sure why this one is failing a test bc it returns the correct response in repl?? def top_k_frequent_elements(list, k) return [] if list == [] hash = {}