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
69 changes: 62 additions & 7 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,76 @@

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

# hash function that assigns each letter a value
def alphabet_keys()
Comment on lines +4 to +8

Choose a reason for hiding this comment

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

Nice helper!

alphabet = "abcdefghijklmnopqrstuvwxyz".split('')
alphabet_keys = {}
alphabet.length.times do |i|
alphabet_keys[alphabet[i]] = i + 1
end

return alphabet_keys
end

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!"
return [] if strings == []

alphabet_keys = alphabet_keys()
string_keys = {}

# iterate through each string
strings.each do |str|
key = 0
# use alphabet_keys to assign each word a key by adding together values of each letter in the word
str.split('').each do |l|
key += alphabet_keys[l]
end
# if key exists, add word to that key
if string_keys[key]
string_keys[key].push(str)
else # otherwise create a new key-value pair
string_keys[key] = [str]
end
end

grouped_anagrams = []
string_keys.each do |k, v|
grouped_anagrams.push(v)
end

return grouped_anagrams
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)
# Space Complexity: O(n)
def top_k_frequent_elements(list, k)
Comment on lines +49 to 51

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!"
end
return [] if list.empty?
frequency = {}
list.each do |i|
frequency[i] ? (frequency[i] += 1) : (frequency[i] = 1)
end

nums = []
unique = list.uniq
max = frequency.values.max

until max == 0 || nums.length == k
unique.each do |i|
break if nums.length == k
if frequency[i] == max
nums << i
end
end
max -= 1
end

return nums
end

# This method will return the true if the table is still
# a valid sudoku table.
Expand Down
12 changes: 12 additions & 0 deletions test/exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@
expect(answer.sort).must_equal [1,2]
end

it "works with example 1.5" do
# Arrange
list = [2,1,1,2,2,3]
k = 2

# Act
answer = top_k_frequent_elements(list, k)

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

it "works with example 2" do
# Arrange
list = [1]
Expand Down