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

# Time complexity: ?
# Space complexity: ?
require 'pry'
# Time complexity: O(n)
# Space complexity: O(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"
if n < 0
raise ArgumentError
end
return 1 if n == 0
return n * factorial(n-1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
# I think this is reversing the string in place?
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"
return s if s.length <= 1
return reverse(s[1..-1]) + s[0]
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"
return s if s.length <= 1
return reverse_inplace(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.

This creates a new array, so it's not in place

end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def bunny(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"
return n if n == 0
return 2 + bunny(n-1)
end

# Time complexity: ?
# Space complexity: ?
# NOT PASSING TESTS
def nested(s)

Choose a reason for hiding this comment

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

Ok so not implemented

raise NotImplementedError, "Method not implemented"
return true if s.length == 0
# need to check if s = even number of paren-pairs()
# return true if s.length % 2 == 0
end

# Time complexity: ?
# Space complexity: ?
def search(array, value)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n)
# Space complexity: O(n)
def search(array, value, i = 0)

Choose a reason for hiding this comment

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

👍

return false if i >= array.length
return true if array[i] == value
return search(array, value, i + 1)
end

# Time complexity: ?
# Space complexity: ?
def is_palindrome(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n)
# Space complexity: O(n)
# ONLY PASSING 2 TESTS
def is_palindrome(s, l = 0, r = s.length-1 )
if s[l] == s[r]
return true
else
return false
end

while l < s.length

Choose a reason for hiding this comment

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

You don't need a while if it's recursive.

You can make the base case

return true if i >= r

return is_palindrome(s, l + 1, r - 1)
end
end

# Time complexity: ?
# Space complexity: ?
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
# NOT PASSING TESTS
def digit_match(n, m, i = -1)

Choose a reason for hiding this comment

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

You can also pull off digits by % 10.

# i want to check each index from the end and keep track of matching values
n = n.to_s
m = m.to_s
tracker = 0

if n[i] == m[i]
tracker += 1
else
tracker += 0
end

return tracker if i < -(n.length)

Choose a reason for hiding this comment

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

If i is less than n's length?


return digit_match(n, m, i - 1)
end
6 changes: 3 additions & 3 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