Skip to content
Open

Niv #20

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
79 changes: 75 additions & 4 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,32 @@

# 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)
raise NotImplementedError, "Method hasn't been implemented yet!"
output = []
anagram = {}

strings.each do |word|
key = word.split("").sort
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: ?
Expand All @@ -26,5 +45,57 @@ def top_k_frequent_elements(list, k)
# Time Complexity: ?
# Space Complexity: ?
def valid_sudoku(table)

Choose a reason for hiding this comment

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

I think you are on to something with this approach

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
Comment on lines +76 to +77

Choose a reason for hiding this comment

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

I think you are confusing Ruby with something else here. Maybe you meant a while loop?

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