Skip to content

Branches - Michaela #26

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 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
44 changes: 38 additions & 6 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: O(n)
# Space Complexity: O(1)
def remove_duplicates(list)
raise NotImplementedError, "Not implemented yet"
index = 0
prev_num = 0
list.length.times do
item = list[index]
if index == 0
prev_num = item
index += 1
else
if item == prev_num
list.delete_at(index)

Choose a reason for hiding this comment

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

delete_at actually shifts all the subsequent elements over 1 index. So it actually has a runtime of O(n). Since it's inside a loop running n times... Your method has a time complexity of O(n2)

else
index += 1
prev_num = item
end
end
end
return list
end

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(a * b) where a is the number of letters in the first word, and b is the number of words in the array
# Space Complexity: O(1)
def longest_prefix(strings)

Choose a reason for hiding this comment

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

This actually doesn't work.
Consider strings = ["carborator","carvonulted","carsome", "calsome"].

It won't work for this.

raise NotImplementedError, "Not implemented yet"
longest_prefix = ""
num_letters = strings[0].length

num_letters.times do |i|
status = true
check_letter = strings[0][i]
strings.each do |string|
if string[i] != check_letter

Choose a reason for hiding this comment

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

If you find a letter that won't work you can stop here because any subsequent other letters can't be in the prefix.

status = false
end
end
if status == true
longest_prefix += check_letter
end
end

return longest_prefix
end

38 changes: 27 additions & 11 deletions test/practice_exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,51 @@
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 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

it "will work when the first word is the longest" do
strings = ["racecar","race","racer"]

output = longest_prefix(strings)

expect(output).must_equal "race"
end

it "will work with words that are the same" do
strings = ["flower","flower","flower"]

output = longest_prefix(strings)

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