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

Myriam #9

Open
wants to merge 2 commits 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
62 changes: 58 additions & 4 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,73 @@

# 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(m*n log n), where m is each of the items on strings and n is the amount of characters on each string

Choose a reason for hiding this comment

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

👍

# Space Complexity: O(m*n), where m is each of the items on strings and n is the amount of characters on each string

def grouped_anagrams(strings)
raise NotImplementedError, "Method hasn't been implemented yet!"

groups = {}

strings.each_with_index do |word, index|
key = word.chars.sort

if groups[key]
groups[key] << word
else
groups[key] = [word]
end
end

return groups.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: ?

Choose a reason for hiding this comment

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

No answer on time & space complexity?

# Space Complexity: ?
def top_k_frequent_elements(list, k)
raise NotImplementedError, "Method hasn't been implemented yet!"

if list.empty?
return []
end

list_count = {}

list.each_with_index do |item, index|
if list_count[item]
list_count[item] +=1
else
list_count[item] = 1
end
end

values = list_count.values
keys = list_count.keys
max_value = 0
count_repeated = 0

max_value = values.first
values.each do |value|
if value == max_value
count_repeated += 1
if count_repeated == list_count.length
keys.sort
return keys.first(k)
end
end
end

index_values = values.map.with_index.sort.map(&:last)
k_indexes = index_values.last(k)
k_values = []

k_indexes.each do |index_to_print|
k_values.push(keys[index_to_print])
end

return k_values

end


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