From f5f47606004513e5dc0689f133d4236d907827c1 Mon Sep 17 00:00:00 2001 From: charlottea Date: Sat, 19 Sep 2020 17:46:43 -0700 Subject: [PATCH 1/4] upstream issues --- lib/exercises.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..760d5e2 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -3,6 +3,7 @@ # Each subarray will have strings which are anagrams of each other # Time Complexity: ? # Space Complexity: ? +# verifying i can push def grouped_anagrams(strings) raise NotImplementedError, "Method hasn't been implemented yet!" From c0f17ecf495f889ab8ba47d294b3dca689903a47 Mon Sep 17 00:00:00 2001 From: charlottea Date: Thu, 24 Sep 2020 14:49:59 -0700 Subject: [PATCH 2/4] implement grouped_anagrams and top_k_frequent_elements methods --- lib/exercises.rb | 50 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 760d5e2..2f31ac3 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -3,19 +3,61 @@ # Each subarray will have strings which are anagrams of each other # Time Complexity: ? # Space Complexity: ? -# verifying i can push def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + return [] if strings.empty? + return [strings] if strings.size == 1 + + hash1 = {} + + # this is the original str + strings.each do |str| + + # this is the sorted str + word = str.split("").sort.join("") + + # check hash for word + # if not exist, set str as hash value + if hash1[word].nil? + hash1[word] = [str] + else + # or shovel str as hash value + hash1[word] << str + end + + end + return hash1.values 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: ? def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" -end + return [] if list.empty? + + hash1 = {} + + # create hash from list where same elements(key) are tallied(value) + list.each do |num| + if hash1[num] + hash1[num] += 1 + else + hash1[num] = 1 + end + + end + # sort hash by value - descending + sorted = hash1.sort_by { |key, value| value }.reverse + answer = [] + k.times do |index| + answer << sorted[index].first + end + + return answer + + end # closing end for top_k_frequent_elements method # This method will return the true if the table is still From 8bbdfaa2df78f2104252e991f8945a666beb4e7c Mon Sep 17 00:00:00 2001 From: charlottea Date: Thu, 24 Sep 2020 15:08:13 -0700 Subject: [PATCH 3/4] space/time complexity --- lib/exercises.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 2f31ac3..ec835fd 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,8 +1,8 @@ # 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: 0(1) +# Space Complexity: 0(n) def grouped_anagrams(strings) return [] if strings.empty? @@ -32,8 +32,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: 0(1) +# Space Complexity: 0(n) def top_k_frequent_elements(list, k) return [] if list.empty? From 8f9e72f6e3cbd63958f6000d595701862ef0e842 Mon Sep 17 00:00:00 2001 From: charlottea Date: Sat, 3 Oct 2020 16:49:06 -0700 Subject: [PATCH 4/4] debugging --- lib/exercises.rb | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index ec835fd..6f8e3a0 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -34,30 +34,26 @@ def grouped_anagrams(strings) # in the case of a tie it will select the first occuring element. # Time Complexity: 0(1) # Space Complexity: 0(n) + def top_k_frequent_elements(list, k) - return [] if list.empty? + return [] if list.empty? - hash1 = {} + hash1 = {} - # create hash from list where same elements(key) are tallied(value) - list.each do |num| - if hash1[num] - hash1[num] += 1 - else - hash1[num] = 1 + list.each do |element| + if hash1[element] + hash1[element] += 1 + else + hash1[element] = 1 end - end - # sort hash by value - descending - sorted = hash1.sort_by { |key, value| value }.reverse - answer = [] + sorted = hash1.sort_by {|key, value| -value } + result = [] k.times do |index| - answer << sorted[index].first + result << sorted[index][0] end - - return answer - - end # closing end for top_k_frequent_elements method + return result +end # This method will return the true if the table is still