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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^2)
# Space complexity: O(n^2)
def factorial(n)
raise NotImplementedError, "Method not implemented"
if n < 0
raise ArgumentError
elsif n <= 1
return 1
else
return n * factorial(n - 1)
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def reverse(s)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "Method not implemented"
end
reversed_string = ""
if s.length >= 1
reversed_string = reverse(s[1..-1])
reversed_string += s[0]
else
return reversed_string
end
end

# Time complexity: ?
# Space complexity: ?
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n)
# Space complexity: O(1)
def reverse_inplace(s, i = 0, j = -1)

Choose a reason for hiding this comment

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

The space complexity is O(n) due to the system stack

n = s.length
if i == n / 2
return s
else
letter = s[i]
s[i] = s[j]
s[j] = letter
reverse_inplace(s, i + 1, j - 1)
end
end

# Time complexity: ?
# Space complexity: ?
def bunny(n)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n)
# Space complexity: O(n)
def bunny(n, ears = 0)

Choose a reason for hiding this comment

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

This works although I was expecting something like:

def bunny(n)
  return 0 if n == 0 
  return 2 + bunny(n-1)
end

if ears == n + n
return ears
else
ears += n
bunny(n, ears)
end
end

# Time complexity: ?
# Space complexity: ?
def nested(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n)
# Space complexity: O(n)
def nested(s, low = 0, high = (s.length - 1))
if s.length.odd?
return false
elsif low < high
if s[low] != "(" && s[high] != ")"

Choose a reason for hiding this comment

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

Small bug

Suggested change
if s[low] != "(" && s[high] != ")"
if s[low] != "(" || s[high] != ")"

return false
end
return nested(s, low + 1, high - 1)
else
return true
end
end

# Time complexity: ?
# Time complexity: O(n)
# Space complexity: ?
def search(array, value)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "Method not implemented"
end
new_array_minus_one = 0..-2

# Time complexity: ?
# Space complexity: ?
def is_palindrome(s)
raise NotImplementedError, "Method not implemented"
if array.empty?
return false
elsif array[-1] == value
return true
else
search(array[new_array_minus_one], value)
end
end

# Time complexity: O(n)
# Space complexity: O(1)
def is_palindrome(s, i = 0, j = -1)

Choose a reason for hiding this comment

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

👍 But the space complexity is O(n)

n = s.length

if i >= s.length + j
return true
elsif
s[i] != s[j]
return false
elsif s[i] == s[j]
is_palindrome(s, i + 1, j - 1)
end
end

# Time complexity: ?
# Space complexity: ?
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"

end
12 changes: 6 additions & 6 deletions test/recursion_writing_test.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 @@ -164,7 +164,7 @@
end
end

xdescribe "nested" do
describe "nested" do
it "will return true for empystring" do
# Arrange
string = ""
Expand Down Expand Up @@ -210,7 +210,7 @@
end
end

xdescribe "search" do
describe "search" do
it "will return false for empty array" do
# Arrange
item = "a"
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