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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) because the function will be called n times.
# Space complexity: O(n) because there will be n number of function calls placed on the system call stack.
def factorial(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.

👍

raise NotImplementedError, "Method not implemented"
raise ArgumentError if n < 0

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: O(1/2n) where n is the input string because the function will be called the length of half the input string times. O(1/2n) is reduced to O(n).
# Space complexity: O(1/2n) because there will be 1/2 n number of function calls placed on the sytem call stack. O(1/2n) is reduced to O(n).
def reverse(s, low = 0 , high = (s.length-1))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

if low < high
temp_low = s[low]
temp_high = s[high]

s[low] = temp_high
s[high] = temp_low

return reverse(s, low + 1, high - 1)
else
return s
end
end

# Time complexity: ?
# Space complexity: ?
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(1/2n) where n is the input string because the function will be called the length of half the input string times. O(1/2n) is reduced to O(n).
# Space complexity: O(1/2n) because there will be 1/2 n number of function calls placed on the sytem call stack. O(1/2n) is reduced to O(n).
def reverse_inplace(s, low = 0 , high = ( s.length - 1 ))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

if low < high
temp_low = s[low]
temp_high = s[high]

s[low] = temp_high
s[high] = temp_low

return reverse_inplace(s, low + 1, high - 1)
else
return s
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) because the function will be called n times.
# Space complexity: O(n) because there will be n number of function calls placed on the system call stack.
def bunny(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.

👍

raise NotImplementedError, "Method not implemented"
return 0 if n == 0
return 2 if n == 1
return 2 + bunny(n - 1)
end

# Time complexity: ?
# Space complexity: ?
def nested(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(1/2n) where n is the input string because the function will be called the length of half the input string times. O(1/2n) is reduced to O(n).
# Space complexity: O(1/2n) because there will be 1/2 n number of function calls placed on the sytem call stack. O(1/2n) is reduced to O(n).
def nested(s, low = 0, high = (s.length - 1))

Copy link
Copy Markdown

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.odd?

if low < high
unless s[low] == "(" && s[high] == ")"
return false
end

return nested(s, low + 1, high - 1)
else
return true
end
end

# Time complexity: ?
# Space complexity: ?
def search(array, value)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n) because the function will be called n times where n is the length of the array.
# Space complexity: O(n) because there will be n number of function calls placed on the system call stack.
def search(array, value, index = 0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

if index < array.length
return true if array[index] == value

return search(array, value, index + 1)
else
return false
end
end

# Time complexity: ?
# Space complexity: ?
def is_palindrome(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(1/2n) where n is the input string because the function will be called the length of half the input string times. O(1/2n) is reduced to O(n).
# Space complexity: O(1/2n) because there will be 1/2 n number of function calls placed on the sytem call stack. O(1/2n) is reduced to O(n).
def is_palindrome(s, low = 0, high = (s.length - 1))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

if low < high
unless s[low] == s[high]
return false
end

return is_palindrome(s, low + 1, high - 1)
else
return true
end
end

# Time complexity: ?
# Space complexity: ?
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
end
# Time complexity: O(log n) because the function will be called log n times where n is the number of digits in the shorter number.
# Space complexity: O(log n) because there will be log n number of function calls placed on the system call stack where n is the number of digits in the shorter number.
def digit_match(n, m, place = 10, match_counter = 0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

if n == m && n == 0
return 1
end

if n < 1 || m < 1
return match_counter
else
n_remainder = n % place
m_remainder = m % place

if n_remainder == m_remainder
match_counter += 1
end
return digit_match(n - n_remainder, m - m_remainder, place * 10, match_counter, )
end
end
16 changes: 8 additions & 8 deletions test/recursion_writing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,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,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 Down