Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Earth/Taylor #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
68 changes: 61 additions & 7 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@

# 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*k)
# Space Complexity: O(n)

def grouped_anagrams(strings)
raise NotImplementedError, "Method hasn't been implemented yet!"
hash = Hash.new { |k, v| k[v] = [] }
strings.each do |word|
hash[word.chars.sort.join] << word
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*k)
# Space Complexity: O(n)
def top_k_frequent_elements(list, k)
raise NotImplementedError, "Method hasn't been implemented yet!"
hash = {}
list.each do |num|
hash[num] ? hash[num] += 1 : hash[num] = 1
end
sorted = hash.sort_by{ |k, v| -v }
sorted[0...k].map { |num, count| num }
end


Expand All @@ -25,5 +34,50 @@ def top_k_frequent_elements(list, k)
# Time Complexity: ?
# Space Complexity: ?
def valid_sudoku(table)
raise NotImplementedError, "Method hasn't been implemented yet!"
store = {
rows: {},
cols: {},
square: {}
}
i = 0
j = 0
while i < 9
while j < 9
box = table[i][j]
if !store[:rows][i] && box != "."
store[:rows][i] = []
store[:rows][i].push(box)
elsif box != "." && !store[:rows][i].include?(box)
store[:rows][i].push(box)
elsif store[:rows][i] && store[:rows][i].include?(box)
return false
end
if !store[:cols][j] && box != "."
store[:cols][j] = []
store[:cols][j].push(box)
elsif box != "." && !store[:cols][j].include?(box)
store[:cols][j].push(box);
elsif store[:cols][j] && store[:cols][j].include?(box)
return false
end

square_row = ((i+1).to_f / 3.0).ceil
square_col = ((j+1).to_f / 3.0).ceil
square_id = "#{square_row}-#{square_col}"
# square_id = j+1
puts store[:square]

if !store[:square][square_id] && box != "."
store[:square][square_id] = []
store[:square][square_id].push(box)
elsif box != "." && !store[:square][square_id].include?(box)
store[:square][square_id].push(box)
elsif store[:square][square_id] && store[:square][square_id].include?(box)
return false
end
j += 1
end
i += 1
end
return true
end
2 changes: 1 addition & 1 deletion test/exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down