Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 122 additions & 13 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,64 @@


# 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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming the words are small, this is correct.

# Space Complexity: O(n)

def grouped_anagrams(strings)
raise NotImplementedError, "Method hasn't been implemented yet!"
return [] if strings.empty?

anagram_hash = Hash.new()
strings.each do |string|
word_array = string.split("").sort
if anagram_hash.include?(word_array)
anagram_hash[word_array] << string
else
anagram_hash[word_array] = [string]
end
end

result = []
anagram_hash.each do |key, value|
result << value
end
return result

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: ?
# in the case of a tie it will select the first occurring element.
# Time Complexity: O(n)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at your later loop, I would say this is O(k * n)

# Space Complexity: O(n)
def top_k_frequent_elements(list, k)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

raise NotImplementedError, "Method hasn't been implemented yet!"
return [] if list.empty?
return list if list.length == 1

element_count = Hash.new()

list.each do |element|
if element_count[element]
element_count[element] += 1
else
element_count[element] = 1
end
end

result = []

k.times do |i|
count = 0
frequent_element = nil
element_count.each do |element, frequency|
if frequency > count
count = frequency
frequent_element = element
end
end
result << frequent_element
element_count[frequent_element] = 0 if frequent_element
end

return result

end


Expand All @@ -23,8 +67,73 @@ def top_k_frequent_elements(list, k)
# 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: ?
# Time Complexity: I have no idea...O(n^2) since I have nested loops?
# Space Complexity: O(n)
def valid_sudoku(table)
raise NotImplementedError, "Method hasn't been implemented yet!"
end

return true if in_row?(table) && in_column?(table) && in_box?(table)

return false

end

def in_row?(table)
table.length.times do |row|
sudoku_hash = Hash.new()
table.length.times do |i|

if sudoku_hash[table[row][i]]
return false
end

if table[row][i] != '.'
sudoku_hash[table[row][i]] = 1
end

end
end
end

# Checks whether there is any
# duplicate in current column or not.
def in_column?(table)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These in_row and in_column methods don't look very dry here.


table.length.times do |col|
column_hash = Hash.new()

table.length.times do |i|

if column_hash[table[i][col]]
return false
end
if table[i][col] != '.'
column_hash[table[i][col]] = 1
end
end
end
end

# Checks whether there is any duplicate
# in current 3×3 box or not.
def in_box?(table)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really slick method! Nice


# formula from https://stackoverflow.com/questions/41020695/how-do-i-split-a-9x9-array-into-9-3x3-components
grids = table.each_slice(3).map{|strip| strip.transpose.each_slice(3).map{|chunk| chunk.transpose}}.flatten(1)

grids.each do |box|
box_hash = Hash.new()
3.times do |row|
3.times do |col|
current = box[row][col]
if box_hash[current]
return false
end
if current != '.'
box_hash[current] = 1
end
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor thing: I suggest a return true here.

end

end
4 changes: 2 additions & 2 deletions test/exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
end
end

xdescribe "top_k_frequent_elements" do
describe "top_k_frequent_elements" do
it "works with example 1" do
# Arrange
list = [1,1,1,2,2,3]
Expand Down Expand Up @@ -131,7 +131,7 @@

end

xdescribe "valid sudoku" do
describe "valid sudoku" do
it "works for the table given in the README" do
# Arrange
table = [
Expand Down