From 2c6a6a3161e6847b621a55d98f30764eeaf85092 Mon Sep 17 00:00:00 2001 From: "Noor B.A" Date: Tue, 11 May 2021 11:38:47 -0700 Subject: [PATCH 1/6] solved exercises --- lib/exercises.rb | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..ecdb1e0 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,19 +1,52 @@ # 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.nil? + return [] if strings.empty? + + results = [] + + hash = {} + + strings.each do |word| + letters = word.split("").sort + + if hash[letters] + hash[letters] << word + else + hash[letters] = [word] + + end + 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) +# Space Complexity: O (n) def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" + return [] if list.nil? + return [] if list.empty? + + hash = Hash.new(0) + + list.each do |num| + hash[num] += 1 + end + + hash_sorted= hash.sort_by{|k,v| -v} + + results = hash_sorted[0..k-1].map{|k,v| k} + + return results + end From 550853da677a5b2efeba092eb5ab59c6a99e8a2e Mon Sep 17 00:00:00 2001 From: "Noor B.A" Date: Wed, 19 May 2021 08:41:36 -0700 Subject: [PATCH 2/6] updated space complixity for grouped anagram --- lib/exercises.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index ecdb1e0..2c4387f 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: O(n) +# Time Complexity: O(nm) where n is array length(words) and m is word length(letters) # Space Complexity: O(n) def grouped_anagrams(strings) @@ -15,7 +15,7 @@ def grouped_anagrams(strings) strings.each do |word| letters = word.split("").sort - if hash[letters] + if hash[letters] # if it has the sorted letters hash[letters] << word else hash[letters] = [word] From a478867b49784e72165bf1c6c65a8fd0abc4430f Mon Sep 17 00:00:00 2001 From: "Noor B.A" Date: Wed, 13 Oct 2021 16:55:21 -0700 Subject: [PATCH 3/6] cleaned code --- .vscode/launch.json | 12 ++++++++++++ lib/exercises.rb | 4 +--- lib/test.rb | 29 +++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 lib/test.rb diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..ec2c85c --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,12 @@ +{ + "version": "0.2.0", + "configurations": [ + + { + "name": "Debug Local File", + "type": "Ruby", + "request": "launch", + "cwd": "${workspaceRoot}", + "program": "${file}" } + ] +} \ No newline at end of file diff --git a/lib/exercises.rb b/lib/exercises.rb index 2c4387f..9097d64 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -8,8 +8,6 @@ def grouped_anagrams(strings) return [] if strings.nil? return [] if strings.empty? - results = [] - hash = {} strings.each do |word| @@ -29,7 +27,7 @@ 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: O (n) +# Time Complexity: O (n log n) for using sort # Space Complexity: O (n) def top_k_frequent_elements(list, k) return [] if list.nil? diff --git a/lib/test.rb b/lib/test.rb new file mode 100644 index 0000000..037ff3c --- /dev/null +++ b/lib/test.rb @@ -0,0 +1,29 @@ +# def find_judge(n, trust) +# relations = Hash.new {|h,k| h[k] = {outdegree: 0 , indegree: 0}} + +# trust.each do |a,b| +# relations[a] = { +# outdegree: + 1 +# } +# relations[b] = { +# indegree: + 1 +# } +# end + +# judge = relations.select{|relation, hash| hash[indegree] == n - 1 && hash[outdegree] == 0} + +# # print relations +# return judge + +# end + +# puts find_judge(2, [1,2]) + +# test = {indegree: 0, +# outdegree: 0} + +# print test + +h = Hash.new { |h,k| h[k] = { count: 0, rating: 0 } } +puts h +h \ No newline at end of file From 14be3a289b2148fb66ef790d202ec6ae29b4eb9a Mon Sep 17 00:00:00 2001 From: "Noor B.A" Date: Thu, 14 Oct 2021 10:24:40 -0700 Subject: [PATCH 4/6] modified top_k_ffrequent_elements --- lib/exercises.rb | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 9097d64..f3904e6 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -22,7 +22,6 @@ def grouped_anagrams(strings) end return hash.values - end # This method will return the k most common elements @@ -30,24 +29,23 @@ def grouped_anagrams(strings) # Time Complexity: O (n log n) for using sort # Space Complexity: O (n) def top_k_frequent_elements(list, k) - return [] if list.nil? - return [] if list.empty? - hash = Hash.new(0) + return [] if list.nil? || list.empty? - list.each do |num| - hash[num] += 1 + if k.nil? + raise ArgumentError,"k was not provided" end - hash_sorted= hash.sort_by{|k,v| -v} + frequent = Hash.new(0) - results = hash_sorted[0..k-1].map{|k,v| k} - - return results + list.each do |num| + frequent[num] += 1 + end + results = frequent.sort_by{|num , frequency| -frequency } + results[0...k].map{|num , frequency| num } 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 @@ -57,4 +55,4 @@ def top_k_frequent_elements(list, k) # Space Complexity: ? def valid_sudoku(table) raise NotImplementedError, "Method hasn't been implemented yet!" -end +end \ No newline at end of file From 4eb68199731de441ae92e64c781783f580313105 Mon Sep 17 00:00:00 2001 From: "Noor B.A" Date: Fri, 15 Oct 2021 10:06:18 -0700 Subject: [PATCH 5/6] finalized sudoku validation --- lib/exercises.rb | 73 +++++++++++++++++++++++++++++++++++++++--- lib/test.rb | 32 +++--------------- test/exercises_test.rb | 4 +-- 3 files changed, 75 insertions(+), 34 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index f3904e6..416639f 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -50,9 +50,74 @@ def top_k_frequent_elements(list, k) # 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 -# row, column or 3x3 subgrid -# Time Complexity: ? -# Space Complexity: ? +# row, column or the 9 3x3 subgrid +# Time Complexity: O(1) for a 9 block sudoku +# Space Complexity: O(n) for the hash table def valid_sudoku(table) - raise NotImplementedError, "Method hasn't been implemented yet!" + if table.nil? || table.empty? + raise ArgumentError,'no table provided' + end + + # row check + table.each do |row| + row_hash = {} + row.each do |input| + if validate_sudoku_input(input) + if row_hash[input] + false + else + row_hash[input] = true + end + end + end + end + + # column check + i = 0 + while i < 9 + col_hash = {} + table.each do |row| + input = row[i] + if validate_sudoku_input(input) + if col_hash[input] + false + else + col_hash[input] = true + end + end + end + i += 1 + end + + # 9 3x3 block + [0,3,6].each do |rows| # created array for rows + [0,3,6].each do |columns| # created array for columns + return false unless sudoku_3x3_block(rows, columns, table) + end + end + + true +end + + +def validate_sudoku_input(value) + if value != "." && value =~ /[^123456789]/ + false + end +end + +def sudoku_3x3_block(rows, columns, table) + block_hash = {} + (rows...rows+3).each do |row| + (columns...columns+3).each do |column| + input = table[row][column] + return false unless validate_sudoku_input(input) + if block_hash[input] + false + else + block_hash[input] = true + end + end + end + true end \ No newline at end of file diff --git a/lib/test.rb b/lib/test.rb index 037ff3c..9ecc8d8 100644 --- a/lib/test.rb +++ b/lib/test.rb @@ -1,29 +1,5 @@ -# def find_judge(n, trust) -# relations = Hash.new {|h,k| h[k] = {outdegree: 0 , indegree: 0}} - -# trust.each do |a,b| -# relations[a] = { -# outdegree: + 1 -# } -# relations[b] = { -# indegree: + 1 -# } -# end - -# judge = relations.select{|relation, hash| hash[indegree] == n - 1 && hash[outdegree] == 0} - -# # print relations -# return judge - -# end +array = ['a','b','c','d'] -# puts find_judge(2, [1,2]) - -# test = {indegree: 0, -# outdegree: 0} - -# print test - -h = Hash.new { |h,k| h[k] = { count: 0, rating: 0 } } -puts h -h \ No newline at end of file +[0,2].each do |num| + puts num +end \ No newline at end of file diff --git a/test/exercises_test.rb b/test/exercises_test.rb index 74646dc..8d09b1c 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 = [ @@ -236,4 +236,4 @@ expect(valid).must_equal false end end -end +end \ No newline at end of file From 59fd9329e9700bdf391efdfcc7018733bd75f525 Mon Sep 17 00:00:00 2001 From: "Noor B.A" Date: Fri, 15 Oct 2021 10:07:27 -0700 Subject: [PATCH 6/6] deleted scratch --- lib/test.rb | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 lib/test.rb diff --git a/lib/test.rb b/lib/test.rb deleted file mode 100644 index 9ecc8d8..0000000 --- a/lib/test.rb +++ /dev/null @@ -1,5 +0,0 @@ -array = ['a','b','c','d'] - -[0,2].each do |num| - puts num -end \ No newline at end of file