Skip to content
Open

Kim #11

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
61 changes: 47 additions & 14 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,63 @@


# This method will return an array of arrays.
# Each subarray will have strings which are anagrams of each other
# Time Complexity: ?
# Space Complexity: ?
# This method will return an top_frequent_nums of top_frequent_numss.
# Each subtop_frequent_nums will have strings which are anagrams of each other
# Time Complexity: O(m * n log n) - where m is the length of the list, and n is the average length of a word in the list
# Space Complexity: O(n) - where n is the total amount of characters in list

def grouped_anagrams(strings)

Choose a reason for hiding this comment

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

👍

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

anagrams = {}
strings.each do |string|
sorted_string = string.split("").sort.join("")
anagrams[sorted_string] ? anagrams[sorted_string] << string : anagrams[sorted_string] = [string]
end

return 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: ?
# This method will return the k most common nums
# in the case of a tie it will select the first occuring num.
# Time Complexity: O(m * n) - where m is the value of k, and n is the length of the list

Choose a reason for hiding this comment

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

Exactly! Well done!

# Space Complexity: O(n) - where n is the length of the list
def top_k_frequent_elements(list, k)
raise NotImplementedError, "Method hasn't been implemented yet!"
end
return list if list.empty?

num_frequency = {}

list.each do |num|
num_frequency[num] ? num_frequency[num] += 1 : num_frequency[num] = 1
end

top_frequent_nums = []

k.times do
max_frequency = 0
max_frequent_num = nil

num_frequency.each do |key, value|
if value > max_frequency
max_frequency = value
max_frequent_num = key
end
end

top_frequent_nums << max_frequent_num
num_frequency[max_frequent_num] = 0
end

return top_frequent_nums
end

# This method will return the true if the table is still
# a valid sudoku table.
# Each element can either be a ".", or a digit 1-9
# The same digit cannot appear twice or more in the same
# Each num 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: ?
def valid_sudoku(table)
raise NotImplementedError, "Method hasn't been implemented yet!"
end
end
93 changes: 46 additions & 47 deletions test/exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
it "will return [] for an empty array" do
#Arrange
list = []

# Act-Assert
expect(grouped_anagrams(list)).must_equal []
end
Expand All @@ -18,9 +18,9 @@
answer = grouped_anagrams(list)

expected_answer = [
["ate","eat","tea"],
["nat","tan"],
["bat"]
["ate", "eat", "tea"],
["nat", "tan"],
["bat"],
]

# Assert
Expand All @@ -42,7 +42,7 @@
["tar"],
["pop"],
["pan"],
["pap"]
["pap"],
]

# Assert
Expand All @@ -59,7 +59,7 @@
answer = grouped_anagrams(list)

expected_answer = [
[ "aet", "ate", "eat", "eta", "tae", "tea"]
["aet", "ate", "eat", "eta", "tae", "tea"],
]
# Assert
answer.each_with_index do |array, index|
Expand All @@ -68,17 +68,17 @@
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]
list = [1, 1, 1, 2, 2, 3]
k = 2

# Act
answer = top_k_frequent_elements(list, k)

# Assert
expect(answer.sort).must_equal [1,2]
expect(answer.sort).must_equal [1, 2]
end

it "works with example 2" do
Expand Down Expand Up @@ -128,22 +128,21 @@
# Assert
expect(answer.sort).must_equal [1]
end

end

xdescribe "valid sudoku" do
it "works for the table given in the README" do
# Arrange
table = [
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
]

# Act
Expand All @@ -156,15 +155,15 @@
it "fails for the table given in the README" do
# Arrange
table = [
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
["8", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
]

# Act
Expand All @@ -177,15 +176,15 @@
it "fails for a duplicate number in a sub-box" do
# Arrange
table = [
["5","3",".",".","7",".",".",".","."],
["6",".","3","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", "3", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
]

# Act
Expand All @@ -198,15 +197,15 @@
it "fails for a duplicate number in a bottom right sub-box" do
# Arrange
table = [
["5","3",".",".","7",".",".",".","."],
["6",".","2","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9","8",".","5"],
[".",".",".",".","8",".",".","7","9"]
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", "2", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", "8", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
]

# Act
Expand All @@ -216,4 +215,4 @@
expect(valid).must_equal false
end
end
end
end