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
123 changes: 92 additions & 31 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,110 @@
# 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"
if n < 0
raise ArgumentError
end
return 1 if n == 1 || n == 0
return n * factorial(n-1)
end

# Time complexity: ?
# Space complexity: ?
def reverse(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: 0(n)
# Space complexity: 0(n)
def reverse(s, i = 0, j = -1, n_s = "")

Choose a reason for hiding this comment

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

👍

if i == s.length
else
n_s << s[j]
reverse(s, i + 1, j -1, n_s)
end
return n_s
end

# Time complexity: ?
# Space complexity: ?
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: 0(n)
# Space complexity: 0(n)
def reverse_inplace(s, i = 0, j = -1)

Choose a reason for hiding this comment

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

👍

if i == s.length/2
else
temp = s[i]
s[i] = s[j]
s[j] = temp
reverse_inplace(s, i + 1, j - 1)
end
return s
end

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

Choose a reason for hiding this comment

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

👍

if n == i
return j
else
return bunny(n, i + 1, j + 2)
end
end

# Time complexity: ?
# Space complexity: ?
def nested(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n)
# Space complexity: O(n)
def nested(s, i = 0, ope = 0, close = 0)

Choose a reason for hiding this comment

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

👍

return false if s.length % 2 != 0
if i < s.length
if s[i] == "("
ope += 1
else
if i >= 1
close += 1
else
return false
end
end
return nested(s, i + 1, ope, close)
end
return ope == close
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 array.length == 0
if array[i] == value
return true
end
if i < array.length
return search(array, value, i + 1)
else
return false
end
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, i = 0, j = -1, coun = 1)

Choose a reason for hiding this comment

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

👍

return true if s == ""
if i < s.length / 2
if s[i] == s[j]
return is_palindrome(s, i + 1, j - 1, coun + 1)
else
return false
end
end
return coun % 2 == 0
end

# Time complexity: ?
# Space complexity: ?
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n)
# Space complexity: O(n)
def digit_match(n, m, i = 0, coun = 0)
if (n.class != Array) && (m.class != Array)
n = n.digits
m = m.digits
end
if i < n.length
if n[i] == m[i]
return digit_match(n, m, i + 1, coun + 1)
else
return digit_match(n, m, i + 1, coun)
end
else
return coun
Comment on lines +101 to +108

Choose a reason for hiding this comment

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

This assumes the two numbers start with the same number of digits. Maybe this would be better, if you started at the last digit.

end
end
34 changes: 17 additions & 17 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 All @@ -304,8 +304,8 @@
# Act
answer = digit_match(num1, num2)

# Assert
expect(answer).must_equal 4
# Assert
expect(answer).must_equal 4
end

it "returns 0 for nonmatching numbers" do
Expand All @@ -316,8 +316,8 @@
# Act
answer = digit_match(num1, num2)

# Assert
expect(answer).must_equal 0
# Assert
expect(answer).must_equal 0
end

it "returns 3 for 841 and 62530841" do
Expand All @@ -328,8 +328,8 @@
# Act
answer = digit_match(num1, num2)

# Assert
expect(answer).must_equal 3
# Assert
expect(answer).must_equal 3
end

it "returns 1 for (0, 0)" do
Expand All @@ -340,8 +340,8 @@
# Act
answer = digit_match(num1, num2)

# Assert
expect(answer).must_equal 1
# Assert
expect(answer).must_equal 1
end

it "returns 1 for (10, 20)" do
Expand All @@ -352,7 +352,7 @@
# Act
answer = digit_match(num1, num2)

# Assert
expect(answer).must_equal 1
# Assert
expect(answer).must_equal 1
end
end