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: 53 additions & 29 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,73 @@
# 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)

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) if n > 1
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.

This works, but the time/space complexities are actually O(n2) because you are generating a new array with each recursive call.

raise NotImplementedError, "Method not implemented"
return s if s.length <= 1
return s[-1] + reverse(s[1..-2]) + 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)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n)
# Space complexity: O(n)
def reverse_inplace(s, i = 0, j = s.length - 1)

Choose a reason for hiding this comment

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

👍

if i >= j
return s
else
x = s[j]
y = s[i]
s[i] = x
s[j] = y
reverse_inplace(s, i += 1, j -= 1)
end
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 0 if n == 0
return 2 + bunny(n - 1) if n > 0
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
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 false if s.length.odd?
return true if s.length == 0
return false if s[0] != "(" || s[-1] != ")"
return nested(s[1..-2]) if s.length > 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)
def is_palindrome(s, start_index = 0, last_index = s.length - 1)

Choose a reason for hiding this comment

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

👍

return false if s[start_index] != s[last_index]
return true if start_index > last_index
return is_palindrome(s, start_index += 1, last_index -= 1)
end

# Time complexity: ?
# Space complexity: ?
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
end
# Time complexity: O(n) - where n is the number with less digits
# Space complexity: O(n) - where n is the number with less digits
def digit_match(n, m, match_count = 0)

Choose a reason for hiding this comment

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

👍

n = n.to_s
m = m.to_s

return match_count if n[-1] == nil || m[-1] == nil
return digit_match(n[0..-2], m[0..-2], match_count += 1) if n[-1] == m[-1]
return digit_match(n[0..-2], m[0..-2], match_count)
end
35 changes: 18 additions & 17 deletions test/recursion_writing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "minitest/skip_dsl"
require_relative '../lib/recursive-methods'


describe "factorial" do
it "will find the factorial of 0" do
# Arrange
Expand Down Expand Up @@ -33,12 +34,12 @@

# Act-Assert
expect {
answer = factorial(num)
factorial(num)
}.must_raise ArgumentError
end
end

xdescribe "reverse" do
describe "reverse" do
it "will reverse 'cat'" do
# Arrange
string = "cat"
Expand Down Expand Up @@ -84,52 +85,52 @@
end


xdescribe "reverse_in_place" do
describe "reverse_in_place" do
it "will reverse 'cat'" do
# Arrange
string = "cat"

# Act
answer = reverse_inplace(string)
reverse_inplace(string)

# Assert
expect(answer).must_equal "tac"
expect(string).must_equal "tac"
end

it "will reverse 'a'" do
# Arrange
string = "a"

# Act
answer = reverse_inplace(string)
reverse_inplace(string)

# Assert
expect(answer).must_equal "a"
expect(string).must_equal "a"
end

it "will reverse empty string " do
# Arrange
string = ""

# Act
answer = reverse_inplace(string)
reverse_inplace(string)

# Assert
expect(answer).must_equal ""
expect(string).must_equal ""
end
it "will reverse 'apple'" do
# Arrange
string = "apple"

# Act
answer = reverse_inplace(string)
reverse_inplace(string)

# Assert
expect(answer).must_equal "elppa"
expect(string).must_equal "elppa"
end
end

xdescribe "bunny" do
describe "bunny" do
it "returns 0 for 0 bunnies" do
# Arrange
count = 0
Expand Down Expand Up @@ -164,8 +165,8 @@
end
end

xdescribe "nested" do
it "will return true for empystring" do
describe "nested" do
it "will return true for empty string" 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