diff --git a/lib/exercises.rb b/lib/exercises.rb index 2cb2bfa..fc2c9c2 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -2,29 +2,60 @@ # 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 strings if strings.length == 0 + hash = Hash.new() + strings.each do |word| + word_array = word.split("").sort + if hash.include?(word_array) + hash[word_array] << word + else + hash[word_array] = [word] + end + end + + result = [] + hash.each do |key, value| + result << value + end + return result 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!" -end + hash = Hash.new + + list.each do |element| + if hash.include?(element) + hash[element] += 1 + else + hash[element] = 1 + end + end + + hash.sort_by { |k, v| v } + top_k = [] + hash.each do |k, v| + top_k << k + end + + return top_k[0..(k - 1)] +end # 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: ? def valid_sudoku(table) raise NotImplementedError, "Method hasn't been implemented yet!" -end \ No newline at end of file +end diff --git a/test/exercises_test.rb b/test/exercises_test.rb index a649110..1c77bae 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 @@ -18,9 +18,9 @@ answer = grouped_anagrams(list) expected_answer = [ - ["ate","eat","tea"], - ["nat","tan"], - ["bat"] + ["ate", "eat", "tea"], + ["nat", "tan"], + ["bat"], ] # Assert @@ -42,7 +42,7 @@ ["tar"], ["pop"], ["pan"], - ["pap"] + ["pap"], ] # Assert @@ -59,7 +59,7 @@ answer = grouped_anagrams(list) expected_answer = [ - [ "aet", "ate", "eat", "eta", "tae", "tea"] + ["aet", "ate", "eat", "eta", "tae", "tea"], ] # Assert answer.each_with_index do |array, index| @@ -68,17 +68,17 @@ 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] + list = [1, 1, 1, 2, 2, 3] k = 2 # Act answer = top_k_frequent_elements(list, k) # Assert - expect(answer.sort).must_equal [1,2] + expect(answer.sort).must_equal [1, 2] end it "works with example 2" do @@ -128,22 +128,21 @@ # Assert expect(answer.sort).must_equal [1] end - end xdescribe "valid sudoku" do it "works for the table given in the README" do # Arrange table = [ - ["5","3",".",".","7",".",".",".","."], - ["6",".",".","1","9","5",".",".","."], - [".","9","8",".",".",".",".","6","."], - ["8",".",".",".","6",".",".",".","3"], - ["4",".",".","8",".","3",".",".","1"], - ["7",".",".",".","2",".",".",".","6"], - [".","6",".",".",".",".","2","8","."], - [".",".",".","4","1","9",".",".","5"], - [".",".",".",".","8",".",".","7","9"] + ["5", "3", ".", ".", "7", ".", ".", ".", "."], + ["6", ".", ".", "1", "9", "5", ".", ".", "."], + [".", "9", "8", ".", ".", ".", ".", "6", "."], + ["8", ".", ".", ".", "6", ".", ".", ".", "3"], + ["4", ".", ".", "8", ".", "3", ".", ".", "1"], + ["7", ".", ".", ".", "2", ".", ".", ".", "6"], + [".", "6", ".", ".", ".", ".", "2", "8", "."], + [".", ".", ".", "4", "1", "9", ".", ".", "5"], + [".", ".", ".", ".", "8", ".", ".", "7", "9"], ] # Act @@ -156,15 +155,15 @@ it "fails for the table given in the README" do # Arrange table = [ - ["8","3",".",".","7",".",".",".","."], - ["6",".",".","1","9","5",".",".","."], - [".","9","8",".",".",".",".","6","."], - ["8",".",".",".","6",".",".",".","3"], - ["4",".",".","8",".","3",".",".","1"], - ["7",".",".",".","2",".",".",".","6"], - [".","6",".",".",".",".","2","8","."], - [".",".",".","4","1","9",".",".","5"], - [".",".",".",".","8",".",".","7","9"] + ["8", "3", ".", ".", "7", ".", ".", ".", "."], + ["6", ".", ".", "1", "9", "5", ".", ".", "."], + [".", "9", "8", ".", ".", ".", ".", "6", "."], + ["8", ".", ".", ".", "6", ".", ".", ".", "3"], + ["4", ".", ".", "8", ".", "3", ".", ".", "1"], + ["7", ".", ".", ".", "2", ".", ".", ".", "6"], + [".", "6", ".", ".", ".", ".", "2", "8", "."], + [".", ".", ".", "4", "1", "9", ".", ".", "5"], + [".", ".", ".", ".", "8", ".", ".", "7", "9"], ] # Act @@ -177,15 +176,15 @@ it "fails for a duplicate number in a sub-box" do # Arrange table = [ - ["5","3",".",".","7",".",".",".","."], - ["6",".","3","1","9","5",".",".","."], - [".","9","8",".",".",".",".","6","."], - ["8",".",".",".","6",".",".",".","3"], - ["4",".",".","8",".","3",".",".","1"], - ["7",".",".",".","2",".",".",".","6"], - [".","6",".",".",".",".","2","8","."], - [".",".",".","4","1","9",".",".","5"], - [".",".",".",".","8",".",".","7","9"] + ["5", "3", ".", ".", "7", ".", ".", ".", "."], + ["6", ".", "3", "1", "9", "5", ".", ".", "."], + [".", "9", "8", ".", ".", ".", ".", "6", "."], + ["8", ".", ".", ".", "6", ".", ".", ".", "3"], + ["4", ".", ".", "8", ".", "3", ".", ".", "1"], + ["7", ".", ".", ".", "2", ".", ".", ".", "6"], + [".", "6", ".", ".", ".", ".", "2", "8", "."], + [".", ".", ".", "4", "1", "9", ".", ".", "5"], + [".", ".", ".", ".", "8", ".", ".", "7", "9"], ] # Act @@ -198,15 +197,15 @@ it "fails for a duplicate number in a bottom right sub-box" do # Arrange table = [ - ["5","3",".",".","7",".",".",".","."], - ["6",".","2","1","9","5",".",".","."], - [".","9","8",".",".",".",".","6","."], - ["8",".",".",".","6",".",".",".","3"], - ["4",".",".","8",".","3",".",".","1"], - ["7",".",".",".","2",".",".",".","6"], - [".","6",".",".",".",".","2","8","."], - [".",".",".","4","1","9","8",".","5"], - [".",".",".",".","8",".",".","7","9"] + ["5", "3", ".", ".", "7", ".", ".", ".", "."], + ["6", ".", "2", "1", "9", "5", ".", ".", "."], + [".", "9", "8", ".", ".", ".", ".", "6", "."], + ["8", ".", ".", ".", "6", ".", ".", ".", "3"], + ["4", ".", ".", "8", ".", "3", ".", ".", "1"], + ["7", ".", ".", ".", "2", ".", ".", ".", "6"], + [".", "6", ".", ".", ".", ".", "2", "8", "."], + [".", ".", ".", "4", "1", "9", "8", ".", "5"], + [".", ".", ".", ".", "8", ".", ".", "7", "9"], ] # Act @@ -216,4 +215,4 @@ expect(valid).must_equal false end end -end \ No newline at end of file +end