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

Branches - Erika #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
46 changes: 39 additions & 7 deletions lib/practice_exercises.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@

# Time Complexity: ?
# Space Complexity: ?
require 'pry'
# Time Complexity: O(n)
# Space Complexity: O(n)
def remove_duplicates(list)
raise NotImplementedError, "Not implemented yet"
index = 0
while index <= list.length
if list[index] == list[index + 1]
list.delete_at(index)

Choose a reason for hiding this comment

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

delete_at shifts each subsequent element left by one index. So that's an O(n) method. Since you have it nested in a loop running n times, remove_duplicates becomes an O(n2) method.

end
index += 1
end
return list
end

# Time Complexity: ?
# Space Complexity: ?


# Time Complexity: O(n * m)

Choose a reason for hiding this comment

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

Correct

# Space Complexity: O(1)

def longest_prefix(strings)
raise NotImplementedError, "Not implemented yet"
prefix = strings[0]
original = prefix
a_length = strings.length
a_length -= 1


a_length.times do |index|
comparison_word = strings[index + 1]
w_length = prefix.length
w_length.times do |i|
if prefix[i] != comparison_word[i]
i -= 1
prefix = prefix[0..i]
if prefix == original

Choose a reason for hiding this comment

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

How could this if statement be true given the line above.

return ""
end
break
end
end
end
return prefix
end