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
87 changes: 66 additions & 21 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,94 @@
# frozen_string_literal: true

# 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 +5 to 7

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 1 if n <= 1


n * factorial(n - 1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def reverse(s)
Comment on lines +15 to 17

Choose a reason for hiding this comment

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

👍 Given how you have the # frozen_string_literal: true which makes the string fixed and means the slice isn't recreating the string.

raise NotImplementedError, "Method not implemented"
return s if s.length <= 1

reversed_str = reverse(s[1..-1])
reversed_str += s[0]
reversed_str
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1)
def reverse_inplace(s)
Comment on lines +25 to 27

Choose a reason for hiding this comment

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

Just noting this is incomplete.

raise NotImplementedError, "Method not implemented"

end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def bunny(n)
Comment on lines +31 to 33

Choose a reason for hiding this comment

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

This doesn't work see below

raise NotImplementedError, "Method not implemented"
if n == 0
return 0
else
2 * bunny(n-1)
return
Comment on lines +37 to +38

Choose a reason for hiding this comment

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

Suggested change
2 * bunny(n-1)
return
return 2 + bunny(n-1)

end

return answer

end

# Time complexity: ?
# Space complexity: ?
def nested(s)
Comment on lines 45 to 47

Choose a reason for hiding this comment

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

Just noting this is incomplete.

raise NotImplementedError, "Method not implemented"
# if s == ""
# return true
# elsif s.length % 2 != 0
# return false
# else
# return nested(s)
# end
end

# Time complexity: ?
# Time complexity: O(n)
# Space complexity: ?
def search(array, value)
Comment on lines -33 to 59

Choose a reason for hiding this comment

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

⚠️ This isn't a recursive method!

raise NotImplementedError, "Method not implemented"
n = 0
length = array.length
while array[n] != value
n += 1
search(array, value)
if n = length
return false
end
end
return true
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^2)
# Space complexity: O(n^2)
def is_palindrome(s)
Comment on lines +72 to 74

Choose a reason for hiding this comment

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

👍 , but can you see any way to adjust this to be O(n)?

raise NotImplementedError, "Method not implemented"
if s.length < 2
true
elsif s[0] != s[-1]
false
else
is_palindrome(s[1..-2])
end
end

# Time complexity: ?
# Space complexity: ?
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
end
def digit_match(n, m, num_matches = 0)

Choose a reason for hiding this comment

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

Just noting this is incomplete.

# n = n.to_s
# m = m.to_s
# if n < 10 || m < 10
# return num_matches
# else
# return digit_match(n/10, m/10, num_matches = 0)
# end
end
8 changes: 4 additions & 4 deletions test/recursion_writing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
end
end

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

xdescribe "search" do
describe "search" do
it "will return false for empty array" do
# Arrange
item = "a"
Expand Down Expand Up @@ -263,7 +263,7 @@
end
end

xdescribe "is_palindrome" do
describe "is_palindrome" do
it "will return true for emptystring" do
# Arrange
string = ""
Expand Down Expand Up @@ -298,7 +298,7 @@
end
end

xdescribe "digit_match" do
describe "digit_match" do
it "returns 4 for 1072503891 and 62530841" do
# Arrange
num1 = 1072503891
Expand Down