From 2ccf4ff53be8489fb43049aa11abbbfc42386d9e Mon Sep 17 00:00:00 2001 From: Faiza Ahsan Date: Tue, 17 Sep 2019 21:00:26 -0700 Subject: [PATCH 1/6] Added code for grouped anagrams and top kth element --- test/exercises_test.rb | 2 +- test/test_helper.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/exercises_test.rb b/test/exercises_test.rb index a649110..fe513a0 100644 --- a/test/exercises_test.rb +++ b/test/exercises_test.rb @@ -5,7 +5,7 @@ it "will return [] for an empty array" do #Arrange list = [] - + # Act-Assert expect(grouped_anagrams(list)).must_equal [] end diff --git a/test/test_helper.rb b/test/test_helper.rb index ebdd402..e7742d0 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,4 +1,4 @@ - + require 'minitest' require 'minitest/autorun' require 'minitest/reporters' From 0a00a9f25d416cabb4def444678b79572313a8a3 Mon Sep 17 00:00:00 2001 From: Faiza Ahsan Date: Tue, 17 Sep 2019 21:05:18 -0700 Subject: [PATCH 2/6] Added code for grouped_anagrams and to kth --- lib/exercises.rb | 62 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 5 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 2cb2bfa..dc7460f 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,12 +1,38 @@ - - # 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!" + if strings.length == 0 + return [] + end + + hash = Hash.new + array_of_words = [] + + # in a loop + # split the word at ("") and sort it and save the results into an array (if 2 words are anagrams of each other, sorting them means the letters of both words will be re-arranged in the same sequence) + strings.each do |word| + array_of_words = word.split("").sort + + # check if the hash has the element of the array as a key, if not then add it as a key in the hash + if hash.include?(array_of_words) + hash[array_of_words].push(word) + # check if the 2nd element is in the hash, if not then add it as a value to the key that was previously added + else + hash[array_of_words] = [word] + end + end + + # puts "Hash" + # puts hash + + # save the values of the hash into an array and return that array + anagrams = [] + hash.each do |key, value| + anagrams.push(value) + end + return anagrams end # This method will return the k most common elements @@ -14,7 +40,33 @@ def grouped_anagrams(strings) # Time Complexity: ? # Space Complexity: ? def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" + + if list.length == 0 + return [] + end + + hash = Hash.new + # loop through the list + # add each element in the list as a key in k with a count value (to count how many times that particular element exists in the list) + # if element doesn't exist in the hash, then add it with a default value of 1 + + list.each do |element| + if hash.include?(element) + hash[element] += 1 + else + hash[element] = 1 + end + end + + # in the hash, we can sort the key value pairs by order of occurence (most occurrences first) + # we can push the highest occurening keys into an array and return the array + + top_k = [] + hash.sort_by { |key, value| value } + hash.each do |key, value| + top_k << key + end + return top_k[0..(k - 1)] end From 3851c3e626fa96041698e63b235ed0b94891e3d2 Mon Sep 17 00:00:00 2001 From: Faiza Ahsan Date: Tue, 17 Sep 2019 21:22:17 -0700 Subject: [PATCH 3/6] Modified code for top kth element --- lib/exercises.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index dc7460f..085bbdc 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -62,7 +62,7 @@ def top_k_frequent_elements(list, k) # we can push the highest occurening keys into an array and return the array top_k = [] - hash.sort_by { |key, value| value } + hash.sort_by { |key, value| -value } hash.each do |key, value| top_k << key end From 2570683b7fc1a87de6cee2f72a7bb47eec5bdede Mon Sep 17 00:00:00 2001 From: Faiza Ahsan Date: Wed, 18 Sep 2019 18:35:18 -0700 Subject: [PATCH 4/6] added original way I did the grouped anagramns problem without the hash --- lib/exercises.rb | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 085bbdc..dbf2f5d 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: O(n) because it will need to check every item in the array +# Space Complexity: O(1) def grouped_anagrams(strings) if strings.length == 0 return [] @@ -35,6 +35,32 @@ def grouped_anagrams(strings) return anagrams end +# def grouped_anagrams(list) +# if list.length == 0 +# return [] +# end + +# parent = [[list[0]]] + +# for i in (1...list.length) +# new_word_srtd = list[i].chars.sort.join +# is_anagram = false + +# for j in (0...parent.length) +# old_words_srtd = parent[j][0].chars.sort.join +# if old_words_srtd == new_word_srtd +# is_anagram = true +# parent[j].push(list[i]) +# break +# end +# end +# if is_anagram == false +# parent.push([list[i]]) +# end +# end +# return parent +# 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: ? @@ -73,7 +99,7 @@ def top_k_frequent_elements(list, k) # This method will return the true if the table is still # a valid sudoku table. # Each element can either be a ".", or a digit 1-9 -# The same digit cannot appear twice or more in the same +# The same digit cannot appear twice or more in the same # row, column or 3x3 subgrid # Time Complexity: ? # Space Complexity: ? From 9b8e8880e0a80c652bc5484fa9cc9cc931473cd8 Mon Sep 17 00:00:00 2001 From: Faiza Ahsan Date: Thu, 19 Sep 2019 13:40:33 -0700 Subject: [PATCH 5/6] Unskipped some tests and modiefied some code for grouped anagrams --- lib/exercises.rb | 10 +++++----- test/exercises_test.rb | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index dbf2f5d..bfcfd9e 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -8,19 +8,19 @@ def grouped_anagrams(strings) end hash = Hash.new - array_of_words = [] + temp_word = "" # in a loop # split the word at ("") and sort it and save the results into an array (if 2 words are anagrams of each other, sorting them means the letters of both words will be re-arranged in the same sequence) strings.each do |word| - array_of_words = word.split("").sort + temp_word = word.split("").sort # check if the hash has the element of the array as a key, if not then add it as a key in the hash - if hash.include?(array_of_words) - hash[array_of_words].push(word) + if hash.include?(temp_word) + hash[temp_word].push(word) # check if the 2nd element is in the hash, if not then add it as a value to the key that was previously added else - hash[array_of_words] = [word] + hash[temp_word] = [word] end end diff --git a/test/exercises_test.rb b/test/exercises_test.rb index fe513a0..5e94e7c 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 a5917dbfb17982cc2f6a968976fc224f8dd859f6 Mon Sep 17 00:00:00 2001 From: Faiza Ahsan Date: Thu, 19 Sep 2019 13:44:24 -0700 Subject: [PATCH 6/6] Added Space/Time Complexity for top_kth_elements --- lib/exercises.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index bfcfd9e..083ac09 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -63,8 +63,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: O(n) because it will have to loop throught the entire array +# Space Complexity: O(1) def top_k_frequent_elements(list, k) if list.length == 0