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 - Eve L. #25

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
59 changes: 53 additions & 6 deletions lib/practice_exercises.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,60 @@

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def remove_duplicates(list)

Choose a reason for hiding this comment

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

Great work, this works, well done.

raise NotImplementedError, "Not implemented yet"
raise ArgumentError if !list
length = list.length

if length > 0
unique_index = 0
unique_element = list.first

length.times do |index|
if list[index] != unique_element
unique_element = list[index]
unique_index += 1
list[unique_index] = unique_element
end

if index > unique_index
list[index] = nil
end
end

end
return list
end

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n * m) where n is the number of strings in input array, m is the length

Choose a reason for hiding this comment

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

Well done!

# (number of characters) of either prefix string or current string.
# Space Complexity: O(1) because this method uses constant number of temp variables and returns
# only 1 string (it might contain m characters but still only 1 string)

def longest_prefix(strings)
raise NotImplementedError, "Not implemented yet"
raise ArgumentError if !strings

prefix = ""
if strings.length > 0
prefix = strings[0]
(strings.length - 1).times do |i|
current_string = strings[i + 1]

if prefix.length < current_string.length
length = prefix.length
else
length = current_string.length
prefix = prefix[0...length]
end

length.times do |j|
if prefix[j] != current_string[j]
prefix = prefix[0...j]
break
end
end
end
end

return prefix
end

8 changes: 8 additions & 0 deletions test/practice_exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,13 @@

expect(output).must_equal ""
end

it "will update shorter prefix strings" do
strings = ["bbb","bb"]

output = longest_prefix(strings)

expect(output).must_equal "bb"
end
end
end