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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def factorial(n)
raise NotImplementedError, "Method not implemented"
if n == 0 || n == 1
return 1
elsif n < 0
raise ArgumentError.new("Parameter must be a positive number")
else
return n * factorial(n - 1)
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(2^n)
def reverse(s)
raise NotImplementedError, "Method not implemented"
if s.length <= 1
return s
else
return s[-1] + reverse(s[0...-1])
end
end


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

# Time complexity: ?
# Space complexity: ?

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

# Time complexity: ?
# Space complexity: ?

# Time complexity: O(n)
# Space complexity: O(n^2)
def nested(s)
raise NotImplementedError, "Method not implemented"
if s == ""
return true
end
if s[0] != "(" || s[-1] != ")"
return false
end
return nested(s[1...-1])
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n^2)
def search(array, value)
raise NotImplementedError, "Method not implemented"
if array.length == 0
return false
end
if value == array[0]
return true
else
return search(array[1..-1], value)
end
end

# Time complexity: ?
# Space complexity: ?

# Time complexity: O(n)
# Space complexity: O(2^n)
def is_palindrome(s)
raise NotImplementedError, "Method not implemented"
if s == ""
return true
end
if s[0] != s[-1]
return false
end
return is_palindrome(s[1...-1])
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1)
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
if n == 0 || m == 0
return 0
end
if n%10 == m%10
value = 1
else
value = 0
end
return value + digit_match(n/10, m/10)
end
13 changes: 7 additions & 6 deletions specs/recursion_writing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

end


it "will raise an ArgumentError if given a number not >= 0" do
# Arrange
num = -1
Expand All @@ -38,7 +39,7 @@
end
end

xdescribe "reverse" do
describe "reverse" do
it "will reverse 'cat'" do
# Arrange
string = "cat"
Expand Down Expand Up @@ -129,7 +130,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 +165,7 @@
end
end

xdescribe "nested" do
describe "nested" do
it "will return true for empystring" do
# Arrange
string = ""
Expand Down Expand Up @@ -210,7 +211,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 +261,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 +296,7 @@
end
end

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