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
50 changes: 43 additions & 7 deletions lib/practice_exercises.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,49 @@
require "pry"
# Time Complexity: o(n)
# Space Complexity: o(1)

# Time Complexity: ?
# Space Complexity: ?
# num = [1, 2, 2, 3, 4, 4]
def remove_duplicates(list)
raise NotImplementedError, "Not implemented yet"
i = 0
while i <= list.length
if list[i] == list[i + 1]
list.delete_at(i)

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 the subsequent elements 1 index to the left, it's an O(n) method. Since you have it in a loop that goes n times this method is O(n2)

end
i += 1
end

# print list.length
return list
end

# Time Complexity: ?
# Space Complexity: ?
# remove_duplicates(num)
#==============================================================

# Time Complexity: o(n^2)
# Space Complexity: ?o(n)

def longest_prefix(strings)
raise NotImplementedError, "Not implemented yet"
end
total_words = strings.length
prefix = ""

if total_words == 0
return prefix
end

if total_words == 1
return strings[0]
end

total_words.times do |index|

Choose a reason for hiding this comment

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

shouldn't one of these loops go the number of characters in a string, rather than the number of strings times?

You also have an issue with the if statement, you're comparing the wrong things.

word = strings[index]
i = index + 1
while i < total_words
next_word = strings[i]
if prefix[index] != word[index] && word[index] == next_word[index]
prefix += word[index]
end
i += 1
end
end
return prefix
end
10 changes: 5 additions & 5 deletions test/practice_exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@
it "works for 1 element strings" do
expect(remove_duplicates([1])).must_equal [1]
end

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]).reject{|num| num == nil }).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"]
strings = ["flower", "flow", "flight"]

output = longest_prefix(strings)

expect(output).must_equal "fl"
end

it "will work for the strings with the common prefix in the rear" do
strings = ["flower","flow","flight", "fpastafl"]
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"]
strings = ["dog", "racecar", "car"]

output = longest_prefix(strings)

Expand Down