From eb626e39b90c0cb2b912440cb52fd67f112961d8 Mon Sep 17 00:00:00 2001 From: thenora Date: Sun, 13 Sep 2020 22:17:38 -0700 Subject: [PATCH 1/5] passing tests for anagrams --- lib/exercises.rb | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..8fcb880 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -5,7 +5,53 @@ # Space Complexity: ? def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + hash = {} + return [] if strings.empty? + + strings.each do |word| + sorted = word.chars.sort + if hash[sorted] + hash[sorted] << word + else + hash[sorted] = [word] + end + end + + # output = [] + # hash.keys.each do |key| + # output << hash[key] + # end + + # return output + + return hash.values + + +end + +def is_anagram(string1, string2) + return false if string1.length != string2.length + + letter_hash = {} + + string1.each_char do |letter| + letter_hash[letter] = letter_hash[letter].nil? ? 1 : letter_hash[letter] += 1 + end + + string2.each_char do |letter| + if letter_hash[letter] + letter_hash[letter -= 1] + else + return false + end + end + + hash.keys.each do |key| + if hash[key] != 0 + return false + end + end + end # This method will return the k most common elements From 6cde3e7a8d5231622827f817afcdb1d5d0d00fcb Mon Sep 17 00:00:00 2001 From: thenora Date: Sun, 13 Sep 2020 22:24:40 -0700 Subject: [PATCH 2/5] cleaned up code --- lib/exercises.rb | 43 ++++++------------------------------------- 1 file changed, 6 insertions(+), 37 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 8fcb880..4722c5e 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,8 +1,8 @@ # 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^2) or O(m * n) where m is the number of words and n is the characters +# Space Complexity: O(n) where n is the number of words in the array def grouped_anagrams(strings) hash = {} @@ -16,50 +16,19 @@ def grouped_anagrams(strings) hash[sorted] = [word] end end - - # output = [] - # hash.keys.each do |key| - # output << hash[key] - # end - - # return output return hash.values - - end -def is_anagram(string1, string2) - return false if string1.length != string2.length - - letter_hash = {} - - string1.each_char do |letter| - letter_hash[letter] = letter_hash[letter].nil? ? 1 : letter_hash[letter] += 1 - end - - string2.each_char do |letter| - if letter_hash[letter] - letter_hash[letter -= 1] - else - return false - end - end - - hash.keys.each do |key| - if hash[key] != 0 - return false - end - end - -end # This method will return the k most common elements -# in the case of a tie it will select the first occuring element. +# in the case of a tie it will select the first occurring element. # Time Complexity: ? # Space Complexity: ? def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" + hash = {} + + end From 77fcee91658cb693b56b8a9a782951d9a112e0ca Mon Sep 17 00:00:00 2001 From: thenora Date: Sun, 13 Sep 2020 22:41:53 -0700 Subject: [PATCH 3/5] most of problem # 2 working --- lib/exercises.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/exercises.rb b/lib/exercises.rb index 4722c5e..3dfff90 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -26,8 +26,27 @@ def grouped_anagrams(strings) # Time Complexity: ? # Space Complexity: ? def top_k_frequent_elements(list, k) + return [] if list.empty? hash = {} + list.each do |num| + if hash[num] + hash[num] =+ 1 + else + hash[num] = 1 + end + end + + answer = [] + returned_count = k + + sorted_hash = hash.values.sort + + k.times do |i| + answer << sorted_hash[i][0] + end + + return answer end From 2fd99cb14f898fd22ba84c93b1b4c89822412b95 Mon Sep 17 00:00:00 2001 From: thenora Date: Sun, 13 Sep 2020 22:44:18 -0700 Subject: [PATCH 4/5] passing all problem number 2 tests --- lib/exercises.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 3dfff90..9290e52 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -37,11 +37,10 @@ def top_k_frequent_elements(list, k) end end - answer = [] - returned_count = k - - sorted_hash = hash.values.sort + sorted_hash = hash.sort_by {|k, v| -v} + answer = [] + k.times do |i| answer << sorted_hash[i][0] end From a2a975914d95ed60563db037d440878b663fdec5 Mon Sep 17 00:00:00 2001 From: thenora Date: Sat, 19 Sep 2020 21:03:01 -0700 Subject: [PATCH 5/5] didn't finish sudoku --- lib/exercises.rb | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 9290e52..36949cc 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -23,8 +23,8 @@ def grouped_anagrams(strings) # This method will return the k most common elements # in the case of a tie it will select the first occurring element. -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O (n log n) due to sort_by / quick sort - best case. Worst case is O(n^2) if the data is already ordered +# Space Complexity: O(n) def top_k_frequent_elements(list, k) return [] if list.empty? hash = {} @@ -37,7 +37,7 @@ def top_k_frequent_elements(list, k) end end - sorted_hash = hash.sort_by {|k, v| -v} + sorted_hash = hash.sort_by {|key, value| -value} answer = [] @@ -57,6 +57,37 @@ def top_k_frequent_elements(list, k) # row, column or 3x3 subgrid # Time Complexity: ? # Space Complexity: ? + +def complete_sudoku + return { + 1 => 1, + 2 => 1, + 3 => 1, + 4 => 1, + 5 => 1, + 6 => 1, + 7 => 1, + 8 => 1, + 9 => 1 + } +end + + def valid_sudoku(table) - raise NotImplementedError, "Method hasn't been implemented yet!" + + # rows = {} + # cols = {} + # mini_square = {} + + # i = 0 # current row + # j = 0 # current column + # until i > 9 + # until j > 9 + # box = table[i][j]; + # j += 1 + # end + # i += 1 + # end end + +