From 9abb4407766dd9b3fd8399c68f3a064e80a9ee7e Mon Sep 17 00:00:00 2001 From: mulhoo Date: Tue, 29 Sep 2020 22:47:50 -0700 Subject: [PATCH 1/4] tests pass exercise 1 --- lib/exercises.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..56cb5ce 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: ? def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + hash = {} + + strings.each do |string| + child = {} + + string.each_char do |char| + child[char] ? child[char] += 1 : child[char] = 1 + end + + hash[child] ? hash[child] << string : hash[child] = [string] + end + + return hash.values end # This method will return the k most common elements From b210bd99c5cd86d2692f78ee76a3687c91a785d7 Mon Sep 17 00:00:00 2001 From: mulhoo Date: Tue, 29 Sep 2020 22:49:11 -0700 Subject: [PATCH 2/4] added bigO --- lib/exercises.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 56cb5ce..43c610f 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,7 +1,7 @@ # 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: On +# Space Complexity: On def grouped_anagrams(strings) hash = {} From 943967c957af9f02ee7358892a8ab234a5270a67 Mon Sep 17 00:00:00 2001 From: mulhoo Date: Tue, 29 Sep 2020 23:36:45 -0700 Subject: [PATCH 3/4] one test failing --- lib/exercises.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 43c610f..6c230ab 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -24,7 +24,19 @@ def grouped_anagrams(strings) # Time Complexity: ? # Space Complexity: ? def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" + result = [] + list = list.sort.uniq + + return list if list.length == k + return result if list.length == 0 + + i = 0 + k.times do + result << list[i] + i += 1 + end + + return result end From 3e604fdc75f8dc1abe5ac134138c6227f6d6ed52 Mon Sep 17 00:00:00 2001 From: mulhoo Date: Tue, 29 Sep 2020 23:52:22 -0700 Subject: [PATCH 4/4] added time complexity p2, test still failing --- lib/exercises.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 6c230ab..d78bb65 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -21,8 +21,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: On +# Space Complexity: On def top_k_frequent_elements(list, k) result = [] list = list.sort.uniq