From a7e1f19fdaaa47260c11d574fe09afd0896d0d19 Mon Sep 17 00:00:00 2001 From: Faezeh Ashtiani Date: Sat, 19 Sep 2020 22:03:22 -0700 Subject: [PATCH 1/3] exercise 1 and 2 --- lib/exercises.rb | 56 ++++++++++++++++++++++++++++++++++++------ test/exercises_test.rb | 8 ++++++ test/test_helper.rb | 2 +- 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..e3ed29c 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,19 +1,61 @@ # 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!" + # create an anagrams hash of arrays + anagrams = {} + # for each string, split the characters and sort them + # join the characers, and test the hash keys + # if the key already exists add it to the array of that key + # if not create one + strings.each do |word| + sorted_word = word.split(//).sort.join + if anagrams[sorted_word] + anagrams[sorted_word] << word + else + anagrams[sorted_word] = [word] + end + end + + # return the arrays + anagrams.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!" + # create a counter hash for the numbers + counter = {} + # go through the list of numbers + # test the hash keys, if it is not there, add it with a number 1 value + # otherwise increment the value by 1 + list.each do |num| + if !counter[num] + counter[num] = 1 + else + counter[num] += 1 + end + end + puts "this is counter" + p counter + + puts "the K is " + k.to_s + + # find the K max counts + highest_counts = counter.values.max(k) + puts "this is highest_counts" + p highest_counts + # return the values of those keys + most_frequent = [] + highest_counts.each do |n| + most_frequent << counter.key(n) + counter.delete(counter.key(n)) + end + return most_frequent end diff --git a/test/exercises_test.rb b/test/exercises_test.rb index dd93104..9ee777b 100644 --- a/test/exercises_test.rb +++ b/test/exercises_test.rb @@ -129,6 +129,14 @@ expect(answer.sort).must_equal [1] end + it "will work for [3, 3, 3, 3, 3, 1, 1, 2, 2, 2]" do + list = [3, 3, 3, 3, 3, 1, 1, 2, 2, 2] + k = 2 + + answer = top_k_frequent_elements(list, k) + + expect(answer.sort).must_equal [2, 3] + end end xdescribe "valid sudoku" do diff --git a/test/test_helper.rb b/test/test_helper.rb index ebdd402..e9edac8 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -6,4 +6,4 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -require_relative '../lib/exercises' \ No newline at end of file +require_relative '../lib/exercises' From 527f5fd8e1544eb9cc91642b5f40d02e24db13d9 Mon Sep 17 00:00:00 2001 From: Faezeh Ashtiani Date: Sat, 19 Sep 2020 22:14:49 -0700 Subject: [PATCH 2/3] took out extra comments --- lib/exercises.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index e3ed29c..c59dd2c 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -40,15 +40,9 @@ def top_k_frequent_elements(list, k) counter[num] += 1 end end - puts "this is counter" - p counter - - puts "the K is " + k.to_s # find the K max counts highest_counts = counter.values.max(k) - puts "this is highest_counts" - p highest_counts # return the values of those keys most_frequent = [] highest_counts.each do |n| From 4fd355c37b42fc175cc3c060ddbedf23da7737a1 Mon Sep 17 00:00:00 2001 From: Faezeh Ashtiani Date: Sat, 26 Sep 2020 17:04:35 -0700 Subject: [PATCH 3/3] got the sudoku solution from chris --- lib/exercises.rb | 86 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 2 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index c59dd2c..fa1bd24 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -40,7 +40,12 @@ def top_k_frequent_elements(list, k) counter[num] += 1 end end - + + # list.each do |element| + # counts[element] ||= 0 + # counts[element] += 1 + # end + # find the K max counts highest_counts = counter.values.max(k) # return the values of those keys @@ -60,6 +65,83 @@ def top_k_frequent_elements(list, k) # row, column or 3x3 subgrid # Time Complexity: ? # Space Complexity: ? +def get_valid_digit_count + return { + 1 => 1, + 2 => 1, + 3 => 1, + 4 => 1, + 5 => 1, + 6 => 1, + 7 => 1, + 8 => 1, + 9 => 1, + } +end + +def check_subgrid(table, subgrid) + current_row = subgrid[0] + current_col = subgrid[1] + digit_count = get_valid_digit_count + begin + + while current_row < subgrid[0] + 3 + while current_col < subgrid[1] + 3 + if table[current_row][current_col] =~ /\d/ + digit_count[ table[current_row][current_col].to_i ] -= 1 + end + + current_col += 1 + end + current_col = subgrid[1] + current_row += 1 + end + rescue NoMethodError + return false + end + + return !digit_count.values.any? { |value| value < 0 } +end + def valid_sudoku(table) - raise NotImplementedError, "Method hasn't been implemented yet!" + row_count = get_valid_digit_count + col_count = get_valid_digit_count + + begin + (0...table.length).each do |i| + row_count = get_valid_digit_count + col_count = get_valid_digit_count + (0...table.length).each do |j| + if table[i][j] =~ /\d/ + row_count[ table[i][j].to_i ] -= 1 + end + + if table[j][i] =~ /\d/ + col_count[ table [j][i].to_i ] -= 1 + end + end + if row_count.values.any? { |value| value < 0 } + return false + end + if col_count.values.any? { |value| value < 0 } + + return false + end + end + + + rescue NoMethodError # if the table has something not 0-9 + return false + end + + [[0,0], [0,3], [0,6], + [3,0], [3,3], [3,6], + [6,0], [6,3], [6,6]].each do |grid| + if !check_subgrid(table, grid) + puts "failing on #{grid}" + return false + end + end + + return true end