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
76 changes: 58 additions & 18 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(n)
# Space complexity: O(n)
def factorial(n)
Comment on lines +3 to 5

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
elsif n <= 1
return 1
else
n * factorial(n - 1)
end
end

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

def reverse(s)
Comment on lines +15 to 18

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 s.empty? || s.length == 1
return s
else
s[-1] + reverse(s[1..-2]) + s[0]
end
end

# Time complexity: ?
# Space complexity: ?
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"
# s = "bear"
# reverse_in_place!(s)
# s == "raeb"
# "bear" => "reab" => "raeb"
# reverse("bear") calls reverse("ea") calls reverse("")
# reverse_in_place("bear")
# calls r_i_p_helper("bear", 0)
# calls r_i_p_helper("reab", 1)
# calls r_i_p_helper("raeb", 2) -> returns "raeb"
# r_i_p_helper("", 0) s.length = 0, idx = 0
# ...("a", 0) s.length = 1, idx = 0
# ...("raeb", 2) s.length = 4, idx = 2
# ...("butter", 0) => ("rutteb", 1) => ("rettub", 2) ("rettub", 3) => return
def reverse_in_place_helper(s, index)

Choose a reason for hiding this comment

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

👍 Nice

if index == s.length / 2
return s
else
temp = s[index]
s[index] = s[s.length - index - 1]
s[s.length - index - 1] = temp
reverse_in_place_helper(s, index + 1)
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def reverse_in_place(s)
Comment on lines +50 to +52

Choose a reason for hiding this comment

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

👍

reverse_in_place_helper(s, 0)
end

# Time complexity: O(n)
# Space complexity: O(n)
def bunny(n)
Comment on lines +56 to 58

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
return 0
else
return 2 + bunny(n - 1)
end
end


# Time complexity: ?
# Space complexity: ?
def nested(s)
raise NotImplementedError, "Method not implemented"
raise NotImplementedError, "Method not implemented"
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
67 changes: 32 additions & 35 deletions test/recursion_writing_test.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
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"

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new


describe "factorial" do
it "will find the factorial of 0" do
# Arrange
Expand All @@ -26,8 +25,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 Down Expand Up @@ -86,14 +84,13 @@
end
end


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

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

# Assert
expect(answer).must_equal "tac"
Expand All @@ -104,7 +101,7 @@
string = "a"

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

# Assert
expect(answer).must_equal "a"
Expand All @@ -115,7 +112,7 @@
string = ""

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

# Assert
expect(answer).must_equal ""
Expand All @@ -125,7 +122,7 @@
string = "apple"

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

# Assert
expect(answer).must_equal "elppa"
Expand Down Expand Up @@ -227,40 +224,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 @@ -334,7 +331,7 @@
# Assert
expect(answer).must_equal 3
end

it "returns 1 for (0, 0)" do
# Arrange
num1 = 0
Expand All @@ -346,7 +343,7 @@
# Assert
expect(answer).must_equal 1
end

it "returns 1 for (10, 20)" do
# Arrange
num1 = 10
Expand Down