From 002d773b7b800f6d9e889b62f948e6444b11afb1 Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Mon, 14 Sep 2020 21:45:07 -0700 Subject: [PATCH 1/5] Prelim grouped_anagrams method --- lib/exercises.rb | 19 +++++++++++++++---- test/exercises_test.rb | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..0c6d209 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,11 +1,22 @@ # 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^2) - Nested loop +# Space Complexity: O(n) def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + # create hash map with the uh...keys that represent all the letters... and then the values are an array of strings + anagram_hash = {} + + strings.each do |str| + bucket = anagram_hash.keys.find { |bucket| /^[#{bucket}]+$/.match(str) } + if !bucket.nil? + anagram_hash[bucket] << str + else + anagram_hash[str] = [str] + end + end + return anagram_hash.values end # This method will return the k most common elements @@ -25,5 +36,5 @@ def top_k_frequent_elements(list, k) # Time Complexity: ? # Space Complexity: ? def valid_sudoku(table) - raise NotImplementedError, "Method hasn't been implemented yet!" + # raise NotImplementedError, "Method hasn't been implemented yet!" end diff --git a/test/exercises_test.rb b/test/exercises_test.rb index dd93104..a649110 100644 --- a/test/exercises_test.rb +++ b/test/exercises_test.rb @@ -68,7 +68,7 @@ end end - describe "top_k_frequent_elements" do + xdescribe "top_k_frequent_elements" do it "works with example 1" do # Arrange list = [1,1,1,2,2,3] From 6203d8796c4921b6671ca88295e0f73bcfb9bb77 Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Mon, 14 Sep 2020 22:15:42 -0700 Subject: [PATCH 2/5] Passing all tests for grouped_anagrams --- lib/exercises.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 0c6d209..c7cd2a2 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -5,11 +5,14 @@ # Space Complexity: O(n) def grouped_anagrams(strings) - # create hash map with the uh...keys that represent all the letters... and then the values are an array of strings anagram_hash = {} strings.each do |str| - bucket = anagram_hash.keys.find { |bucket| /^[#{bucket}]+$/.match(str) } + bucket = anagram_hash.keys.find do |b| + unique_chars = b.chars.sort.join + unique_chars == str.chars.sort.join + end + if !bucket.nil? anagram_hash[bucket] << str else From 50d8b5d882db76375bca6953a5740f4b11259483 Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Mon, 14 Sep 2020 22:35:34 -0700 Subject: [PATCH 3/5] Beginning top_k method --- lib/exercises.rb | 13 ++++++++++++- test/exercises_test.rb | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index c7cd2a2..cc6c2a7 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -27,7 +27,18 @@ def grouped_anagrams(strings) # Time Complexity: ? # Space Complexity: ? def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" + elements_hash = {} + + #establish the hash + list.each do |element| + if elements_hash[element] + elements_hash[element] += 1 + else + elements_hash[element] = 1 + end + end + + # grab the key for the highest number for total of k times end diff --git a/test/exercises_test.rb b/test/exercises_test.rb index a649110..dd93104 100644 --- a/test/exercises_test.rb +++ b/test/exercises_test.rb @@ -68,7 +68,7 @@ end end - xdescribe "top_k_frequent_elements" do + describe "top_k_frequent_elements" do it "works with example 1" do # Arrange list = [1,1,1,2,2,3] From 63553b3325f7f491d60a567fb1ee017770326f6c Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Tue, 15 Sep 2020 18:08:37 -0700 Subject: [PATCH 4/5] Tests almost pass --- lib/exercises.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/exercises.rb b/lib/exercises.rb index cc6c2a7..6dd0ce3 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -39,6 +39,8 @@ def top_k_frequent_elements(list, k) end # grab the key for the highest number for total of k times + + return elements_hash.keys.max_by(k) { |key| elements_hash[key] } end From a257d6c55048124a55d5285e7cf4eb9e64f520db Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Thu, 24 Sep 2020 13:17:16 -0700 Subject: [PATCH 5/5] Added comments, big O time/space --- lib/exercises.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 6dd0ce3..5085018 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -24,12 +24,11 @@ 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) +# Space Complexity: O(n) def top_k_frequent_elements(list, k) elements_hash = {} - #establish the hash list.each do |element| if elements_hash[element] elements_hash[element] += 1 @@ -38,8 +37,7 @@ def top_k_frequent_elements(list, k) end end - # grab the key for the highest number for total of k times - + #this doesn't pass tests — instead of grabbing the FIRST most frequent, it grabs the most frequent, period. I couldn't figure out how to do the other way return elements_hash.keys.max_by(k) { |key| elements_hash[key] } end