From bac6f91b92e02c53dc32b7f8ecafe779361bd5d9 Mon Sep 17 00:00:00 2001 From: denisseai Date: Thu, 17 Sep 2020 20:33:10 -0700 Subject: [PATCH 1/3] HW done --- lib/exercises.rb | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..f3a47fb 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,19 +1,57 @@ # 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: a lot +# Space Complexity: O(n) def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + hash = {} + + strings.each do | string | + + a_key = string.chars.sort + + if hash.key? (a_key) + hash[a_key] << string + else + hash[a_key] = [string] + end + end + + arrayArrays = [] + hash.each do | key , value| + arrayArrays << hash[key] + end + + return arrayArrays end # 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: ? +# resource: https://stackoverflow.com/questions/4264133/descending-sort-by-value-of-a-hash-in-ruby def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" + # Hash.new(0) sets default value for any key to 0, while {} sets nil + hash = Hash.new(0) + k_array = [] + + return [] if list.empty? + + # populate the hash and count occurrence + list.each do |num| + hash[num] += 1 + end + + # set hash's values in descending order + descending_hash = hash.sort_by {|key, value| -value} + + # populate the array with most common elements dictated by k + k.times do |i| + k_array << descending_hash[i][0] + end + + return k_array end From d6b108370b7b18986e26b8f8cd0fd08a31feeef1 Mon Sep 17 00:00:00 2001 From: denisseai <26889152+denisseai@users.noreply.github.com> Date: Thu, 24 Sep 2020 20:06:41 -0700 Subject: [PATCH 2/3] Update lib/exercises.rb Co-authored-by: Chris M --- lib/exercises.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index f3a47fb..5289d81 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -11,7 +11,7 @@ def grouped_anagrams(strings) a_key = string.chars.sort - if hash.key? (a_key) + if hash[a_key] hash[a_key] << string else hash[a_key] = [string] From df2184e258ac53f8f075795b43b0556df0182701 Mon Sep 17 00:00:00 2001 From: denisseai <26889152+denisseai@users.noreply.github.com> Date: Thu, 24 Sep 2020 20:07:17 -0700 Subject: [PATCH 3/3] Update lib/exercises.rb Co-authored-by: Chris M --- lib/exercises.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 5289d81..bc2b9e8 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -18,12 +18,7 @@ def grouped_anagrams(strings) end end - arrayArrays = [] - hash.each do | key , value| - arrayArrays << hash[key] - end - - return arrayArrays + return hash.values end # This method will return the k most common elements