From 7fa387d0932f71224f4f2a4e8ef6b9a4d2d696d2 Mon Sep 17 00:00:00 2001 From: Taylor Tompkins Date: Mon, 19 Jul 2021 19:46:14 -0700 Subject: [PATCH] didnt finish sudoku --- lib/exercises.rb | 68 +++++++++++++++++++++++++++++++++++++----- test/exercises_test.rb | 2 +- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..7c6ba30 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,19 +1,28 @@ # 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*k) +# Space Complexity: O(n) def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + hash = Hash.new { |k, v| k[v] = [] } + strings.each do |word| + hash[word.chars.sort.join] << word + end + return hash.values 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*k) +# Space Complexity: O(n) def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" + hash = {} + list.each do |num| + hash[num] ? hash[num] += 1 : hash[num] = 1 + end + sorted = hash.sort_by{ |k, v| -v } + sorted[0...k].map { |num, count| num } end @@ -25,5 +34,50 @@ def top_k_frequent_elements(list, k) # Time Complexity: ? # Space Complexity: ? def valid_sudoku(table) - raise NotImplementedError, "Method hasn't been implemented yet!" + store = { + rows: {}, + cols: {}, + square: {} + } + i = 0 + j = 0 + while i < 9 + while j < 9 + box = table[i][j] + if !store[:rows][i] && box != "." + store[:rows][i] = [] + store[:rows][i].push(box) + elsif box != "." && !store[:rows][i].include?(box) + store[:rows][i].push(box) + elsif store[:rows][i] && store[:rows][i].include?(box) + return false + end + if !store[:cols][j] && box != "." + store[:cols][j] = [] + store[:cols][j].push(box) + elsif box != "." && !store[:cols][j].include?(box) + store[:cols][j].push(box); + elsif store[:cols][j] && store[:cols][j].include?(box) + return false + end + + square_row = ((i+1).to_f / 3.0).ceil + square_col = ((j+1).to_f / 3.0).ceil + square_id = "#{square_row}-#{square_col}" + # square_id = j+1 + puts store[:square] + + if !store[:square][square_id] && box != "." + store[:square][square_id] = [] + store[:square][square_id].push(box) + elsif box != "." && !store[:square][square_id].include?(box) + store[:square][square_id].push(box) + elsif store[:square][square_id] && store[:square][square_id].include?(box) + return false + end + j += 1 + end + i += 1 + end + return true end diff --git a/test/exercises_test.rb b/test/exercises_test.rb index 74646dc..8025075 100644 --- a/test/exercises_test.rb +++ b/test/exercises_test.rb @@ -151,7 +151,7 @@ end end - xdescribe "valid sudoku" do + describe "valid sudoku" do it "works for the table given in the README" do # Arrange table = [