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
45 changes: 34 additions & 11 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,50 @@
# Authoring recursive algorithms. Add comments including time and space complexity for each method.

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n), where n is the value of the number
# Space complexity: O(n), where n is the value of the number
def factorial(n)
raise NotImplementedError, "Method not implemented"
if n == 0
return 1
elsif n.nil? || n < 0
raise ArgumentError, "This value is not valid."
end

return n * factorial(n-1)

end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n), where n is the length of the string

Choose a reason for hiding this comment

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

Technically s[0, s.length-1] creates a new string of length n-1, so this solution is actually n^2.

# Space complexity: O(n), because there will be n layers to the call stack.
def reverse(s)
raise NotImplementedError, "Method not implemented"

if s.length == 0
return s
else
return s[s.length-1] + reverse(s[0, s.length-1])
end

end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n), where n is the length of the string
# Space complexity: O(n), because there will be n layers to the call stack.
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"

Choose a reason for hiding this comment

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

This is not in place.

To do in-place you can create a helper method:
reverse_helper(string, left_index = 0, right_index = string.length -1)

Then each iteration you can swap the characters at left_index and right_index and then call the helper recursively with new values for left_index and right_index. The base case is when left_index == right_index.


if s.length == 0
return s
else
return s[s.length-1] + reverse(s[0, s.length-1])
end

end

# Time complexity: ?

Choose a reason for hiding this comment

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

You can't predict the complexity?

# Space complexity: ?
def bunny(n)
raise NotImplementedError, "Method not implemented"
if n % 2 == 0

Choose a reason for hiding this comment

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

This should actually be:

return 0 if n == 0

return 2 + bunny(n-1)

return 0
else
return n + bunny(2-1)
end
end

# Time complexity: ?
Expand All @@ -46,4 +69,4 @@ def is_palindrome(s)
# Space complexity: ?
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
end
end
8 changes: 4 additions & 4 deletions specs/recursion_writing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
end
end

xdescribe "reverse" do
describe "reverse" do
it "will reverse 'cat'" do
# Arrange
string = "cat"
Expand Down Expand Up @@ -84,7 +84,7 @@
end


xdescribe "reverse_in_place" do
describe "reverse_in_place" do
it "will reverse 'cat'" do
# Arrange
string = "cat"
Expand Down Expand Up @@ -129,7 +129,7 @@
end
end

xdescribe "bunny" do
describe "bunny" do
it "returns 0 for 0 bunnies" do
# Arrange
count = 0
Expand Down Expand Up @@ -331,4 +331,4 @@
# Assert
expect(answer).must_equal 3
end
end
end