Skip to content

Leaves - Katie K. #37

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

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def remove_duplicates(list)
raise NotImplementedError, "Not implemented yet"
item_list = []
list.each_with_index do |item|
if item_list.include?(item) == false

Choose a reason for hiding this comment

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

Since .include? has to traverse the entire array it should take O(n) time, and since it's nested into a loop the method is an O(n2) algorithm.

item_list << item
end
end
return item_list
end

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n2)

Choose a reason for hiding this comment

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

This is actually O(m * n) where m is the number of letters in the strings.

# Space Complexity: O(n)
def longest_prefix(strings)
raise NotImplementedError, "Not implemented yet"
prefix = ""
count = 0
check = true
continue = true
strings[0].each_char do |character|
letter = character
strings.each do |string|
if string[count] != letter
continue = false
break
end
end
if continue == false
break
end
count +=1
end
if count == 0
return prefix
end
return prefix = strings[0][0..count-1]
end






20 changes: 10 additions & 10 deletions test/practice_exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@
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]
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
end
Expand Down