Skip to content

Leaves - Elizabeth #44

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

Open
wants to merge 1 commit 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
48 changes: 41 additions & 7 deletions lib/practice_exercises.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,47 @@

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: 0(1)
def remove_duplicates(list)
raise NotImplementedError, "Not implemented yet"
i = 0
j = 1
until list[j] == nil
if list[i] == list[j]
list.delete_at(list[j])

Choose a reason for hiding this comment

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

delete_at removes an element by shifting all subsequent elements left by one index. So it's an O(n) method. Since it's nested inside a loop running n times, remove_duplicates is an O(n2) method.

else
i+=1
j+=1
end
end
return list
end

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n^4)
# Space Complexity: 0(n)
def longest_prefix(strings)
raise NotImplementedError, "Not implemented yet"
i = 1
j = 0
k = 0
output = ""
characters = []
length = strings.length
strings.each do |word|
characters << word.chars
end

characters.each do |item|

Choose a reason for hiding this comment

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

This isn't working, take a look at the loops a bit. You can fairly comfortably do this by having one loop go through all the words, and the 2nd looping through the letters in the 1st word. When you find a letter than not all the words have in the right position, you've found your prefix.

until j == length-1
until k == length-1
if item[k] != characters[i][k]
return output
else

i += 1
end
output += item[k]
k += 1
j += 1
end
end
return output
end
end