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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n), the amount of recursive calls depends on the size of n.
# Space complexity: O(n), each recursive call is added to the 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: ?
# Time complexity: O(n), the amount of recursive calls depends on the size of the string.
# Space complexity: O(n), each recursive call is added to the stack.
def reverse(s)
Comment on lines +12 to 14

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 works, but because you create a new array with each recursive call this is O(n2) for both time/space complexity.

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

reversed_s = reverse(s[1..-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.

s[1..-1] creates a new array and copies all the individual elements over and so is O(n) by itself.

reversed_s += s[0]
return reversed_s
end

# Time complexity: ?
# Space complexity: ?
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n), the amount of recursive calls depends on the size of the string.
# Space complexity: O(n), each recursive call is added to the stack.
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.

👍 Awesome!

return s if high-low <= 1

temp = s[low]
s[low] = s[high]
s[high] = temp

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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n), the amount of recursive calls depends on the size of n.
# Space complexity: O(n), each recursive call is added to the 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: ?
# Time complexity: O(n), the amount of recursive calls depends on the size of the string.
# Space complexity: O(n), each recursive call is added to the stack.
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.

👍
This works, but has similar issues with time/space complexity as reverse.

raise NotImplementedError, "Method not implemented"
return true if s.length == 0

if s[0] == "(" && s[-1] == ")"
return nested(s[1..s.length-2])
else
return false
end

end

# Time complexity: ?
# Space complexity: ?
def search(array, value)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(log n), the amount of searches is cut in half with every recursive call.
# Space complexity: O(n), each recursive call is added to the stack.
def search(array, value, low = 0, high = array.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.

👍 The time/space complexity is O(log n) in both.

This also assumes that the array is sorted.

mid = (high + low) / 2
return true if array[mid] == value
return false if high < low

if array[mid] > value
return search(array, value, low, high = mid-1)
else
return search(array, value, low = mid + 1, high)
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n), the amount of recursive calls depends on the size of the string.
# Space complexity: O(n), each recursive call is added to the stack.
def is_palindrome(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 works, but because you create a new array with each recursive call this is O(n2) for both time/space complexity.

raise NotImplementedError, "Method not implemented"
return true if s.length == 1 || s.length == 0

if s[0] == s[-1]
return is_palindrome(s[1..-2])
else
return false
end

end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n), the amount of recursive calls depends on the size either of n or m.
# Space complexity: O(n), each recursive call is added to the stack.
def digit_match(n, m)

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 works, but because you create a new array with each recursive call this is O(n2) for both time/space complexity.

raise NotImplementedError, "Method not implemented"
end
n = n.to_s
m = m.to_s

return 0 if n[-1] == nil || m[-1] == nil

if n[-1] == m[-1]
return 1 + digit_match(n[0..-2], m[0..-2])
else
return digit_match(n[0..-2], m[0..-2])
end
end
Loading