From 6237bcda48f314516640c05d1335fa915eceb7d1 Mon Sep 17 00:00:00 2001 From: Nidhi Date: Thu, 12 Sep 2019 15:13:02 -0700 Subject: [PATCH 1/3] passed tests for hash table --- lib/exercises.rb | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 2cb2bfa..c9dd4c4 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -5,10 +5,29 @@ # Time Complexity: ? # Space Complexity: ? + def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + output = [] + anagram = {} + + strings.each do |word| + key = word.split("").sort + puts + if anagram.has_key?(key) == true + anagram[key].push(word) + else + anagram[key] = [word] + end + end + + anagram.each_key do |key| + output.push(anagram[key]) + end + + return output 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: ? From c891bc596ae922173cad482e1df950239f8b3624 Mon Sep 17 00:00:00 2001 From: Nidhi Date: Thu, 12 Sep 2019 15:22:05 -0700 Subject: [PATCH 2/3] added space and time complexity --- lib/exercises.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index c9dd4c4..aff7ad1 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -2,8 +2,9 @@ # 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 * mlogm) +# n for .each do, mlogm for sort +# Space Complexity: O(n) def grouped_anagrams(strings) @@ -12,7 +13,6 @@ def grouped_anagrams(strings) strings.each do |word| key = word.split("").sort - puts if anagram.has_key?(key) == true anagram[key].push(word) else From 12e1c05d77fe39cb002dd71c2d77dfe67c859f71 Mon Sep 17 00:00:00 2001 From: Nidhi Date: Tue, 17 Sep 2019 08:30:17 -0700 Subject: [PATCH 3/3] working on valid sudoku --- lib/exercises.rb | 54 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index aff7ad1..e65ea5b 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -45,5 +45,57 @@ def top_k_frequent_elements(list, k) # Time Complexity: ? # Space Complexity: ? def valid_sudoku(table) - raise NotImplementedError, "Method hasn't been implemented yet!" + if check_rows(table) == false || check_columns(table) == false || check_subgrid(table) == false + return false + end + return true +end + +def check_unique(array) + new_array = array.keep_if{|element| element != "."} + new_array = new_array.uniq + if new_array.length < array.length + return false + end + return true +end + +def check_rows(table) + table.each do |row| + if check_unique(row) == false + return false + end + end + return true +end + +def check_columns(table) + i = 0 + j = 0 + column = [] + for i < table.length + for j < table.length + column.push(table[j][i]) + j += 1 + if j == table.length - 1 + if check_unique(column) == false + return false + end + end + end + i += 1 + j = 0 + column = [] + end + return true +end + +def check_subgrid(table) + subgrids = {} + i = 0 + for i < table.length + subgrids[i] = [] + + + end end \ No newline at end of file