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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) - > the function will run as many times as the given number.
# Space complexity: O(1) -> we're not creating new variables but the method is taking up space in the stack until the base case equals true, then the stacks will popped of 1 by 1 starting from the top of the stack.
def factorial(n)
raise NotImplementedError, "Method not implemented"
# raise NotImplementedError, "Method not implemented"
if n < 0
raise ArgumentError
elsif n == 1 || n == 0
return 1
end
return n * factorial(n - 1)
end

# Time complexity: ?
# Space complexity: ?
def fib(n)

Choose a reason for hiding this comment

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

This works, but see my video on time and space complexity.

if n < 2
return n
else
return fib(n - 1) + fib(n - 2)
end
end

# Time complexity: O(n), where n isnthe length of the input

Choose a reason for hiding this comment

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

This is actually O(n^2) because s[0...-1] creates a new string and you do it n times.

# Space complexity: O(1) because additional space in memory is not being consumed
def reverse(s)
raise NotImplementedError, "Method not implemented"
# raise NotImplementedError, "Method not implemented"
if s.nil? || s.length == 0 || s.length == 1
return s
else
return s[-1] + reverse(s[0...-1])
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n), where n isnthe length of the input
# Space complexity: O(1) because additional space in memory is not being consumed
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"
# raise NotImplementedError, "Method not implemented"
if s.nil? || s.length == 0 || s.length == 1
return s
else
return s[-1] + reverse(s[0...-1])

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 since you create a new string.

Think about making a helper which takes the string, a left index and right index. The base case is when left_index >= right_index Each recursive call you swap the characters at left_index or right_index and then make a recursive call with the string, and left_index + 1 and right_index -1.

end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1) because additional space in memory is not being consumed

Choose a reason for hiding this comment

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

O(n) since you use the system stack for the recursion.

def bunny(n)
raise NotImplementedError, "Method not implemented"
if n == 0 || n.nil?
return 0
# if 50 is not equal to 100 (50 * 2)
else
return 2 + bunny(n - 1)
end
end

# Time complexity: ?
Expand All @@ -36,14 +65,24 @@ def search(array, value)
raise NotImplementedError, "Method not implemented"
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) where n is the size of the input
# Space complexity: O(1) because additional space in memory is not being consumed

Choose a reason for hiding this comment

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

O(n^2) for both time and space since you use the system stack and create a new string with each iteration.

def is_palindrome(s)
raise NotImplementedError, "Method not implemented"
# raise NotImplementedError, "Method not implemented"
if s.nil? || s.length == 0 || s.length == 1
return true
else
if s[0] == s[-1]
is_palindrome(s[1..-2])
else
return false
end
end
end

# Time complexity: ?
# 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 @@ -260,7 +260,7 @@
end
end

xdescribe "is_palindrome" do
describe "is_palindrome" do
it "will return true for emptystring" do
# Arrange
string = ""
Expand Down