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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) where n the number passed as arg
# Space complexity: O(n) where n the number passed as arg
def factorial(n)
raise NotImplementedError, "Method not implemented"
raise ArgumentError.new() if n < 0
return n <= 1 ? 1 : n * factorial(n - 1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) where is the number of chars in the string
# Space complexity: O(n) where is the number of chars in the string
def reverse(s)
raise NotImplementedError, "Method not implemented"
return "" if s.empty?
return s[-1] << reverse(s[0...-1])

Choose a reason for hiding this comment

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

Technically s[0...-1] creates a new string of length n-1, so this algorithm is actually n^2

end

# Time complexity: ?
# Space complexity: ?
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n) where is the number of chars in the string

Choose a reason for hiding this comment

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

👍

# Space complexity: O(n) where is the number of chars in the string
def reverse_inplace(s, left = 0, right = nil)
right ||= s.length - 1
return s if left >= right
temp = s[right]
s[right] = s[left]
s[left] = temp
return reverse_inplace(s, left + 1, right - 1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) where n the number of bunnies passed as arg
# Space complexity: O(n) where n the number of bunnies passed as arg
def bunny(n)
raise NotImplementedError, "Method not implemented"
return 0 if n == 0
return 2 + bunny(n - 1)
end

# Time complexity: ?
# Space complexity: ?
def nested(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n) where n is the number of chars in string
# Space complexity: O(n) where n is the number of chars in string
def nested(s, left = 0, right = nil)
right ||= s.length - 1
return true if left >= right
return false if s.length.odd? || s[right] == s[left]

Choose a reason for hiding this comment

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

👍

nested(s, left + 1, right - 1)
end

# Time complexity: ?
# Space complexity: ?
def search(array, value)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n) where is the number of elements in array
# Space complexity: O(n) where is the number of elements in array
def search(array, value, index = 0)
if index > array.length - 1
return false
end
if array[index] == value
return true
end
search(array, value, index + 1)
end

# Time complexity: ?
# Space complexity: ?
def is_palindrome(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n) where is the number of chars in string
# Space complexity: O(n) where is the number of chars in string
def is_palindrome(s, left = 0, right = nil)
right ||= s.length - 1
return true if right <= left
return false if s[right] != s[left]
is_palindrome(s, left + 1, right - 1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(log10(n)) where n is the smallest number. Also could be described as O(n) where n is the number digit places in the smallest number (123 = 3 digit places)
# Space complexity: O(log10(n)) where n is the smallest number. Also could be described as O(n) where n is the number digit places in the smallest number (123 = 3 digit places)
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
end
return 0 if n == 0 || m == 0
if n % 10 == m % 10
counter = 1
else
counter = 0
end
return counter + digit_match(n / 10, m / 10)
end
82 changes: 40 additions & 42 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 @@ -210,7 +208,7 @@
end
end

xdescribe "search" do
describe "search" do
it "will return false for empty array" do
# Arrange
item = "a"
Expand All @@ -224,43 +222,43 @@
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
describe "is_palindrome" do
it "will return true for emptystring" do
# Arrange
string = ""
Expand Down Expand Up @@ -295,7 +293,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 +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