From 20e80df4380b176d9ec1831a68690dc097717a7c Mon Sep 17 00:00:00 2001 From: Ariana Bray Date: Sun, 15 Sep 2019 20:31:47 -0700 Subject: [PATCH 1/5] passing all tests except last sudoku test --- lib/exercises.rb | 136 +++++++++++++++++++++++++++++++++++++---- test/exercises_test.rb | 4 +- 2 files changed, 127 insertions(+), 13 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 2cb2bfa..62f841f 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,20 +1,64 @@ - - # 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.empty? + + anagram_hash = Hash.new() + strings.each do |string| + word_array = string.split("").sort + if anagram_hash.include?(word_array) + anagram_hash[word_array] << string + else + anagram_hash[word_array] = [string] + end + end + + result = [] + anagram_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: ? +# in the case of a tie it will select the first occurring element. +# 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.empty? + return list if list.length == 1 + + element_count = Hash.new() + + list.each do |element| + if element_count[element] + element_count[element] += 1 + else + element_count[element] = 1 + end + end + + result = [] + + k.times do |i| + count = 0 + frequent_element = nil + element_count.each do |element, frequency| + if frequency > count + count = frequency + frequent_element = element + end + end + result << frequent_element + element_count[frequent_element] = 0 if frequent_element + end + + return result + end @@ -26,5 +70,75 @@ def top_k_frequent_elements(list, k) # Time Complexity: ? # Space Complexity: ? def valid_sudoku(table) - raise NotImplementedError, "Method hasn't been implemented yet!" -end \ No newline at end of file + + return true if in_row?(table) && in_column?(table) && in_box?(table) + + return false + +end + + def in_row?(table) + 9.times do |row| + sudoku_hash = Hash.new() + 9.times do |i| + # If already encountered before, + # return false + if sudoku_hash[table[row][i]] + return false + end + + # If it is not an empty cell, insert value + # at the current cell into the hash + if table[row][i] != '.' + sudoku_hash[table[row][i]] = 1 + end + + end + end + end + + # Checks whether there is any + # duplicate in current column or not. + def in_column?(table) + + 9.times do |col| + column_hash = Hash.new() + + 9.times do |i| + + # If already encountered before, + # return false + if column_hash[table[i][col]] + return false + end + + # If it is not an empty cell, insert + # value at the current cell in the set + if table[i][col] != '.' + column_hash[table[i][col]] = 1 + end + end + end + end + + # Checks whether there is any duplicate + # in current 3×3 box or not. + def in_box?(table) + + box_hash = Hash.new() + + + 3.times do |row| + 3.times do |col| + current = table[row][col] + if box_hash[current] + return false + end + + if current != '.' + box_hash[current] = 1 + end + end + end + +end diff --git a/test/exercises_test.rb b/test/exercises_test.rb index a649110..710370e 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] @@ -131,7 +131,7 @@ end - xdescribe "valid sudoku" do + describe "valid sudoku" do it "works for the table given in the README" do # Arrange table = [ From 8c1a0cb03ba2c44dac17fdd70d751fb06119a6f7 Mon Sep 17 00:00:00 2001 From: Ariana Bray Date: Sun, 15 Sep 2019 20:33:49 -0700 Subject: [PATCH 2/5] added time and space complexity for sudoku --- lib/exercises.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 62f841f..8067786 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -67,8 +67,8 @@ def top_k_frequent_elements(list, k) # Each element can either be a ".", or a digit 1-9 # The same digit cannot appear twice or more in the same # row, column or 3x3 subgrid -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: I have no idea...O(n^2) since I have nested loops? +# Space Complexity: O(n) def valid_sudoku(table) return true if in_row?(table) && in_column?(table) && in_box?(table) From 5b3ad40a060d841c8b41d053ffe91577800d0580 Mon Sep 17 00:00:00 2001 From: Ariana Bray Date: Sun, 15 Sep 2019 20:52:58 -0700 Subject: [PATCH 3/5] figured out final failing sudoku test --- lib/exercises.rb | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 8067786..c7911bd 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -3,6 +3,8 @@ # Time Complexity: O(n) # Space Complexity: O(n) +require 'pry' + def grouped_anagrams(strings) return [] if strings.empty? @@ -78,9 +80,9 @@ def valid_sudoku(table) end def in_row?(table) - 9.times do |row| + table.length.times do |row| sudoku_hash = Hash.new() - 9.times do |i| + table.length.times do |i| # If already encountered before, # return false if sudoku_hash[table[row][i]] @@ -101,10 +103,10 @@ def in_row?(table) # duplicate in current column or not. def in_column?(table) - 9.times do |col| + table.length.times do |col| column_hash = Hash.new() - 9.times do |i| + table.length.times do |i| # If already encountered before, # return false @@ -125,20 +127,23 @@ def in_column?(table) # in current 3×3 box or not. def in_box?(table) - box_hash = Hash.new() - - - 3.times do |row| - 3.times do |col| - current = table[row][col] - if box_hash[current] - return false + # formula from https://stackoverflow.com/questions/41020695/how-do-i-split-a-9x9-array-into-9-3x3-components + grids = table.each_slice(3).map{|strip| strip.transpose.each_slice(3).map{|chunk| chunk.transpose}}.flatten(1) + + grids.each do |box| + box_hash = Hash.new() + 3.times do |row| + 3.times do |col| + current = box[row][col] + if box_hash[current] + return false + end + if current != '.' + box_hash[current] = 1 + end end + end - if current != '.' - box_hash[current] = 1 - end end - end end From ba8733cb1dce5c0099c95d788da17a9e89664fcb Mon Sep 17 00:00:00 2001 From: Ariana Bray Date: Sun, 15 Sep 2019 20:59:12 -0700 Subject: [PATCH 4/5] cleaned up code --- lib/exercises.rb | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index c7911bd..d3aefd6 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -83,14 +83,11 @@ def in_row?(table) table.length.times do |row| sudoku_hash = Hash.new() table.length.times do |i| - # If already encountered before, - # return false + if sudoku_hash[table[row][i]] return false end - # If it is not an empty cell, insert value - # at the current cell into the hash if table[row][i] != '.' sudoku_hash[table[row][i]] = 1 end @@ -108,14 +105,9 @@ def in_column?(table) table.length.times do |i| - # If already encountered before, - # return false if column_hash[table[i][col]] return false end - - # If it is not an empty cell, insert - # value at the current cell in the set if table[i][col] != '.' column_hash[table[i][col]] = 1 end From ee55dce3ae477e5ba14ddd2ce71ed48b68cd2a5d Mon Sep 17 00:00:00 2001 From: Ariana Bray Date: Sun, 15 Sep 2019 21:02:21 -0700 Subject: [PATCH 5/5] removed pry --- lib/exercises.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index d3aefd6..d38b641 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -3,8 +3,6 @@ # Time Complexity: O(n) # Space Complexity: O(n) -require 'pry' - def grouped_anagrams(strings) return [] if strings.empty?