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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1) because constant is being multiplied (no arrays)
def factorial(n)
raise NotImplementedError, "Method not implemented"
# solve base case
if n < 0
raise ArgumentError
end
if n == 0
return 1
end
if n <= 2
return n
end
return n * factorial(n-1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def reverse(s)
raise NotImplementedError, "Method not implemented"
if s.length <= 1
return s
end
return [reverse(s[1, s.length - 1]), s[0]].join
end
# substring on the left, first char on the right, put these into an arry and call .join
# https://apidock.com/ruby/Array/join

# Time complexity: ?
# Space complexity: ?
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n)
# Space complexity: O(1), no additional space used/no new arrays/strings created
def reverse_inplace(s, index = 0)
start_index = index
end_index = s.length - 1 - index

# middle point has been found. this is basecase
if end_index <= start_index
return s
end
# swap start and end elements
temp = s[start_index]
s[start_index] = s[end_index]
s[end_index] = temp

return reverse_inplace(s, index + 1) # this handles swapping middle
end

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

# Time complexity: ?
# Space complexity: ?
def nested(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n)
# Space complexity: O(1)
def nested(s, index = 0) # initalize index
start_index = index
end_index = s.length - 1 - index
if end_index < start_index
return true
end
if s[start_index] == '(' && s[end_index] == ')'
return nested(s, index + 1)
else
return false
end
end

# Time complexity: ?
# Space complexity: ?
def search(array, value)
raise NotImplementedError, "Method not implemented"
raise NotImplementedError, "Method not implemented"
end

# Time complexity: ?
# Space complexity: ?
def is_palindrome(s)
raise NotImplementedError, "Method not implemented"
raise NotImplementedError, "Method not implemented"
end

# Time complexity: ?
# Space complexity: ?
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
raise NotImplementedError, "Method not implemented"
end
8 changes: 4 additions & 4 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