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

# Time complexity: ?
# Space complexity: ?
# Time complexity: 0(n)
# Space complexity: 0(n)
def factorial(n)

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"
raise ArgumentError if n < 0
return 1 if n == 1 || n == 0
return n * factorial(n-1)
end

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

Choose a reason for hiding this comment

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

👍
This works, but because you create a new array with each recursive call this is O(n2) for both time/space complexity.

raise NotImplementedError, "Method not implemented"

return s if s.length < 2
return reverse(s[1..-1]) + s[0]

Choose a reason for hiding this comment

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

s[1..-1] creates a new array and copies all the individual elements over and so is O(n) by itself.


end

# Time complexity: ?
# Space complexity: ?
def reverse_inplace(s)

Choose a reason for hiding this comment

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

  1. This is not done in place
  2. It's calling the method above.

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

# Time complexity: ?
# Space complexity: ?
def bunny(n)
raise NotImplementedError, "Method not implemented"
return 0 if n == 0
return 2 if n == 1
return bunny(n - 1) + bunny(1)

end


# Time complexity: ?
# Space complexity: ?
Comment on lines 37 to 38

Choose a reason for hiding this comment

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

def nested(s)

Choose a reason for hiding this comment

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

👍 This works, but you have similar time/space issues with the above methods.

raise NotImplementedError, "Method not implemented"
# return nested_helper(s[1..-1])
paren_end = s.index(')')
return !s.include?('(') if paren_end.nil?

paren_start = s[0...paren_end].rindex('(')

return false if paren_start.nil?

return nested(s[1..paren_start]+s[paren_end+1..-1])

end

def nested_helper(s)
return true if s.empty?
return false if s.length.odd?
return nested_helper(s)
end

# Time complexity: ?
# 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.

👍 This works, but you have similar time/space issues with the above methods.

raise NotImplementedError, "Method not implemented"
return false if array.nil?
return true if array[0] == value
return search(array[1..-1],value)
end

# Time complexity: ?
# Space complexity: ?
def is_palindrome(s)

Choose a reason for hiding this comment

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

👍 This works, but you have similar time/space issues with the above methods.

raise NotImplementedError, "Method not implemented"
return true if s.empty? || s.length ==1
return false if s[0] != s[s.length - 1]
return is_palindrome_helper(s, 1)

end

def is_palindrome_helper(s, index)
return true if s.empty? || s.length ==1
return false if s[0] != s[s.length - index]
is_palindrome_helper(s[1..s.length - index - 1], 1)

Choose a reason for hiding this comment

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

You're both slicing the array and using index as 1, I'm not sure why index is needed as a param at all.


end

# Time complexity: ?
# Space complexity: ?
def digit_match(n, m)

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"

return 1 if n == 0 && m == 0
count = 0
if n % 10 == m % 10
count += 1
end
if (n / 10 == 0 || m / 10 == 0)

Choose a reason for hiding this comment

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

🏆 Nice catch!

return count
end

return count + digit_match(n / 10, m / 10)

end

def digit_match_helper(n, m)

end
14 changes: 7 additions & 7 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 Expand Up @@ -295,7 +295,7 @@
end
end

xdescribe "digit_match" do
describe "digit_match" do
it "returns 4 for 1072503891 and 62530841" do
# Arrange
num1 = 1072503891
Expand Down