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

Leaves - Raisah V. #36

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
38 changes: 30 additions & 8 deletions lib/practice_exercises.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: The time complexity is O(n) because the algorithms time varies linearly with the length of the list array.

Choose a reason for hiding this comment

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

Actually the time complexity is O(n2) because the delete_at method takes O(n) time. This is because it has to shift all elements to the left one index.

# Space Complexity: O(n) where n is the length of the array. The space complexity varies with the length of the array.
def remove_duplicates(list)
raise NotImplementedError, "Not implemented yet"
return list if list.length == 1 || list.length == 0

list.length.times do |i|
if list[i] == list [i - 1]
list.delete_at(i)
end
end

return list
end

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: The time complexity is O(nm) because in the worst case scenario the time that the algorithm takes can change linearly based on the length of the first string in the array passed AND the length of the array itself.

Choose a reason for hiding this comment

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

Perfect! Well done!

# Space Complexity: O(1) because the space complexity is constant even as the array and string sizes change.
def longest_prefix(strings)
raise NotImplementedError, "Not implemented yet"
common_prefix = ""
i = 0
match = true

while match == true
value = strings[0][i]
strings.each do |s|
if s[i] != value || s[i] == nil
match = false
end
end

common_prefix += value if match == true
i += 1
end

return common_prefix
end

31 changes: 21 additions & 10 deletions test/practice_exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,45 @@
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]
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 strings the same value" do
strings = ["flower","flower","flower"]

output = longest_prefix(strings)

expect(output).must_equal "flower"
output = longest_prefix(strings)

expect(output).must_equal "flower"
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