From 603a5150f002ce387a38fc54657a770e608872f5 Mon Sep 17 00:00:00 2001 From: Ida Date: Sat, 15 May 2021 18:47:55 -0700 Subject: [PATCH 1/3] First and second problem complete --- lib/exercises.rb | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..8621ca3 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,19 +1,44 @@ # 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) +# Space Complexity: o(n) def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + return [] if strings.length == 0 + + anagrams = Hash.new([]) + strings.each do |element| + sorted_letters = element.split('').sort + if anagrams[sorted_letters] + anagrams << element + else + anagrams[sorted_letters] = element + end + return anagrams.values + # strings.each.with_index { |el, i| anagrams[el.downcase.chars.sort.join] += [i] } + # anagrams.map { |key, indexes| indexes.map { |i| strings[i] } } 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: ? +# Time Complexity: o(n) +# Space Complexity: o(n) def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" + return [] if list.length == 0 + hash_list = {} + + list.each do |number| + if hash_list[number] + hash_list[number] += 1 + else + hash_list[number] = 1 + end + end + + sortedhash = Hash[hash_list.sort_by { |key, value| -value }[0...k]] + + return sortedhash.keys end From f001395a51722083566eff3a5672edae3fb761f7 Mon Sep 17 00:00:00 2001 From: idago123 <31869817+idago123@users.noreply.github.com> Date: Thu, 20 May 2021 12:17:11 -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 8621ca3..362e870 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -9,7 +9,7 @@ def grouped_anagrams(strings) anagrams = Hash.new([]) strings.each do |element| - sorted_letters = element.split('').sort + sorted_letters = element.sort if anagrams[sorted_letters] anagrams << element else From c54418b3cbed0e27e1c2528b2102f371426f7c8d Mon Sep 17 00:00:00 2001 From: Ida Date: Thu, 20 May 2021 12:17:54 -0700 Subject: [PATCH 3/3] made small change --- lib/exercises.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 8621ca3..aafc9bd 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -11,9 +11,10 @@ def grouped_anagrams(strings) strings.each do |element| sorted_letters = element.split('').sort if anagrams[sorted_letters] - anagrams << element + anagrams[sorted_letters] << element else anagrams[sorted_letters] = element + end end return anagrams.values # strings.each.with_index { |el, i| anagrams[el.downcase.chars.sort.join] += [i] }