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

Daniela Sanchez - Leaves #45

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
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: ?
# Time Complexity: n
# Space Complexity: 1
def remove_duplicates(list)
raise NotImplementedError, "Not implemented yet"
current = 0
result = 0

until list.length - 1 < current
if list[result] != list[current]
result += 1
list[result] = list[current]
end
current += 1
end
return list[0..result]
end

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: n, as my algorithm needs to iterate through all elements.

Choose a reason for hiding this comment

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

I would say it's O(n) if the strings are small and O(m * n) where m is the length of the longest string if they're not.

# Space Complexity: n, as I'm creating an array to store the characters in each iteration. More elements, longer the new array.
def longest_prefix(strings)

Choose a reason for hiding this comment

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

You're getting stuck in an infinite loop here.

raise NotImplementedError, "Not implemented yet"
end
same = true
index = 0

while same
array_characters = strings.map do |string|
string[index]
end

current = 0
until array_characters.length - 1 < current

Choose a reason for hiding this comment

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

you should also repeat until same is false.

if array_characters[current] == array_characters[0]
current += 1
else
same = false
end
end
index += 1
end

if same == true
return strings.first[0..index]
else
return strings.first[0...index]
end
end