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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(nlogn)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nope this is O(n) since n just decreases by one with each iteration.

# Space complexity: O(nlogn)
def factorial(n)
raise NotImplementedError, "Method not implemented"
if n < 0
raise ArgumentError, "Error!!"
end

if n == 0 || n == 1
return 1
else
return n * factorial(n - 1)
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is O(n^2) since s[0...-1] creates a new string of length n-1 each iteration. Both for space and time.

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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def reverse_inplace(s)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is not in place since you create a new string.

Think about making a helper which takes the string, a left index and right index. The base case is when left_index >= right_index Each recursive call you swap the characters at left_index or right_index and then make a recursive call with the string, and left_index + 1 and right_index -1.

raise NotImplementedError, "Method not implemented"
if s.length == 0
return s
else
return s[-1] + reverse(s[0...-1])
end
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 0
else
return 2 + bunny(n - 1)
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def nested(s)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You seem to be mixing an iterative and recursive solution, see my notes on the reverse in place method.

raise NotImplementedError, "Method not implemented"
if s.length == 0
return true
elsif find_pair(s).nil?
return false
end

if find_pair(s)
s.slice!(find_pair(s), 2)
return nested(s)
end
end

def find_pair(s)
i = 0
while i < s.length
if s[i] == "(" && s[i + 1] == ")"
return i
end
i += 1
end
return nil
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"
end
raise NotImplementedError, "Method not implemented"
end
76 changes: 37 additions & 39 deletions specs/recursion_writing_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'minitest/autorun'
require 'minitest/reporters'
require "minitest/autorun"
require "minitest/reporters"
require "minitest/skip_dsl"
require_relative '../lib/recursive-methods'
require_relative "../lib/recursive-methods"

describe "factorial" do
it "will find the factorial of 0" do
Expand All @@ -23,8 +23,7 @@
answer = factorial(num)

# Assert
expect(answer).must_equal 5*4*3*2*1

expect(answer).must_equal 5 * 4 * 3 * 2 * 1
end

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

xdescribe "reverse" do
describe "reverse" do
it "will reverse 'cat'" do
# Arrange
string = "cat"
Expand Down Expand Up @@ -83,8 +82,7 @@
end
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 +127,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 +162,7 @@
end
end

xdescribe "nested" do
describe "nested" do
it "will return true for empystring" do
# Arrange
string = ""
Expand Down Expand Up @@ -224,40 +222,40 @@
end

it "will return true when looking for something in the array" do
# Arrange
item = "a"
array = ["b", "c", "a"]
# Arrange
item = "a"
array = ["b", "c", "a"]

# Act
answer = search(array, item)
# Act
answer = search(array, item)

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

it "will return false when looking for something not in the array" do
# Arrange
item = "x"
array = ["b", "c", "a"]

# Act
answer = search(array, item)

# Assert
expect(answer).must_equal false
end

it "will return true when finding something at the front of the array" do
# Arrange
item = "b"
array = ["b", "c", "a"]
# Act
answer = search(array, item)
# Assert
expect(answer).must_equal true
end
end

it "will return true when finding something at the front of the array" do
# Arrange
item = "b"
array = ["b", "c", "a"]

# Act
answer = search(array, item)

# Assert
expect(answer).must_equal true
end
end

xdescribe "is_palindrome" do
Expand Down Expand Up @@ -304,8 +302,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 +314,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,7 +326,7 @@
# Act
answer = digit_match(num1, num2)

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