Skip to content
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
42 changes: 37 additions & 5 deletions lib/practice_exercises.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
require "pry"
# Time Complexity: ? O(n) where n corresponds with the length of input array

Choose a reason for hiding this comment

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

Since delete_at shifts all subsequent elements over one index, the time complexity of delete_at is O(n) and so your method is O(n2).

# Space Complexity: ? O(1) not creating any additional space in memory here, just deleting in place

# Time Complexity: ?
# Space Complexity: ?
def remove_duplicates(list)
raise NotImplementedError, "Not implemented yet"
if list.empty? || list.length == 1
return list
else
i = 0
j = 1
until list[j] == nil
if list[i] == list[j]
list.delete_at(i)
end
i += 1
j += 1
end
return list
# raise NotImplementedError, "Not implemented yet"
end
end

# Time Complexity: ?
# time complexity: ?
# Space Complexity: ?


def longest_prefix(strings)
raise NotImplementedError, "Not implemented yet"
a = ""
collection_of_letters = strings.first[0]
j = 0

strings.each_with_index do |string, i|

return a if string[i][j] != collection_of_letters

Choose a reason for hiding this comment

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

I think you mean string[j] != collection_of_letters[j] here

if j == strings.length

a << string[i][j]
end
j += 1
end
return a
# raise NotImplementedError, "Not implemented yet"
end


33 changes: 21 additions & 12 deletions test/practice_exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,44 @@
it "works for empty arrays" do
expect(remove_duplicates([])).must_equal []
end

it "will remove duplicates for longer arrays" do
expect(remove_duplicates([1, 2, 2, 3, 3, 4])).must_equal [1, 2, 3, 4]
expect(remove_duplicates([1, 2, 2, 3, 3, 4]).reject{|num| num == nil }).must_equal [1, 2, 3, 4]
end
end

describe "Longest valid substring" do
it "will work for the README strings" do
strings = ["flower","flow","flight"]

output = longest_prefix(strings)

expect(output).must_equal "fl"
end


# it "will work for Sabrina's strings" do
# strings = [“flower”,“flower”,“flower”]

# output = longest_prefix(string)

# expect(output).must_equal "flower"
# end

it "will work for the strings with the common prefix in the rear" do
strings = ["flower","flow","flight", "fpastafl"]

output = longest_prefix(strings)

expect(output).must_equal "f"
end

it "will work for the README strings" do
strings = ["dog","racecar","car"]

output = longest_prefix(strings)

expect(output).must_equal ""
end

end
end
end