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

vi #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

vi #39

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
55 changes: 47 additions & 8 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,59 @@

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

def grouped_anagrams(strings)
raise NotImplementedError, "Method hasn't been implemented yet!"
end
anagram_hash = Hash.new

strings.each do |word|
sorted_string = word.chars.sort.join

#Check if key exists with the sorted string already
#In case of yes, add the word to the array value of the key
if anagram_hash[sorted_string]
anagram_hash[sorted_string] += [word]

#If not, set it as a new key/value pair in the hash
else
anagram_hash[sorted_string] = [word]
end
end
return anagram_hash.valuesend

Choose a reason for hiding this comment

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

Missing an end after the return

Suggested change
return anagram_hash.valuesend
return anagram_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)
# Space Complexity: O(n)
def top_k_frequent_elements(list, k)
Comment on lines +28 to 30

Choose a reason for hiding this comment

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

This works, but take a look at my comments on time complexity.

raise NotImplementedError, "Method hasn't been implemented yet!"
end
result = []

hash = Hash.new

list.each do |letter|
if hash[letter]
hash[letter] += 1
else
hash[letter] = 1
end
end

k.times do
max = nil
hash.each do |num, value|
Comment on lines +43 to +45

Choose a reason for hiding this comment

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

Just note that this iterates k times and each time loops through an hash of n size so this is O(nk)


if value && (max.nil? || value > max)
max = num
end
end

if max
hash[max] = nil
result << max
end
end
return result end


# This method will return the true if the table is still
Expand Down
2 changes: 1 addition & 1 deletion 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